summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/v3po2vpp/rev160406/VppStateHoneycombReaderModule.java
blob: df216c05afc7c11bf4724cce9929b011b5d381ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406;

import io.fd.honeycomb.translate.v3po.VppStateHoneycombReaderFactory;
import java.lang.management.ManagementFactory;
import javax.management.Attribute;
import javax.management.InstanceNotFoundException;
import javax.management.ObjectName;
import org.opendaylight.controller.config.api.ConflictingVersionException;
import org.opendaylight.controller.config.api.ValidationException;
import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.VppJvppImplModule;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.VppJvppImplModuleFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VppStateHoneycombReaderModule extends
    org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406.AbstractVppStateHoneycombReaderModule {

    private static final Logger LOG = LoggerFactory.getLogger(VppStateHoneycombReaderModule.class);

    public VppStateHoneycombReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
                                         org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
        super(identifier, dependencyResolver);
    }

    public VppStateHoneycombReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
                                         org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
                                         org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406.VppStateHoneycombReaderModule oldModule,
                                         java.lang.AutoCloseable oldInstance) {
        super(identifier, dependencyResolver, oldModule, oldInstance);
    }

    @Override
    public void customValidation() {
        // add custom validation form module attributes here.
    }

    @Override
    public java.lang.AutoCloseable createInstance() {
        return new VppStateHoneycombReaderFactory(getVppJvppDependency(),
                getInterfaceContextVppStateDependency(),
                getBridgeDomainContextVppStateDependency(),
                getKeepaliveExecutorDependency());
    }

    private static long reinitializationCounter;
    private static final long reinitializationLimit = 10;

    /**
     * In case we detect connection issues with VPP, reinitialize JVpp.
     */
    private static void reinitializeJVpp(final long currentAttempt) {
        // FIXME https://jira.fd.io/browse/HONEYCOMB-78 This code correctly re-initializes all the components
        // starting with jvpp, but jvpp reconnect fails. Test in a JVpp test and then from C
        LOG.info("Reinitializing JVpp, attempt: {}", currentAttempt);

        final long nextAttempt = currentAttempt + 1;
        if (nextAttempt - reinitializationCounter > reinitializationLimit) {
            LOG.error("Too many JVpp reinitialization attempts. Unable to reinitialize JVpp in {} attempts. Giving up",
                reinitializationLimit);
            throw new IllegalStateException("Too many JVpp reinitialization attempts. Unable to reinitialize JVpp in "
                + reinitializationLimit + " attempts. Giving up");
        }

        final ConfigRegistryJMXClient cfgRegistryClient =
            ConfigRegistryJMXClient.createWithoutNotifications(ManagementFactory.getPlatformMBeanServer());

        final ObjectName objectName = cfgRegistryClient.beginConfig();
        final ConfigTransactionJMXClient txClient = cfgRegistryClient.getConfigTransactionClient(objectName);

        final ObjectName jvppOn;
        try {
            final String attributeName = VppJvppImplModule.descriptionJmxAttribute.getAttributeName();
            final String factoryName = VppJvppImplModuleFactory.NAME;
            jvppOn = txClient.lookupConfigBean(factoryName, "vpp-jvpp");

            // Change configuration attribute of JVpp to trigger full reinitialization here using config subsystem
            // TODO improve this when switching from karaf in planned minimal distribution
            txClient.setAttribute(jvppOn, attributeName, new Attribute(attributeName,
                Long.toString(nextAttempt)));

            txClient.validateConfig();
            cfgRegistryClient.commitConfig(txClient.getObjectName());
            LOG.info("JVpp reinitialized successfully");
        } catch (InstanceNotFoundException | ValidationException e) {
            LOG.error("Unable to reinitialize JVpp. Honeycomb will not work properly from now on.", e);
            throw new IllegalStateException("Unable to find jvpp instance in config subsystem. Unable to reinitialize JVpp", e);
        } catch (ConflictingVersionException e) {
            LOG.debug("Conflict changes occurred, retrying", e);
            // Just retry until there's no conflicting change in progress
            reinitializeJVpp(nextAttempt);
        }

        reinitializationCounter = nextAttempt;
    }


}