df216c05afc7c11bf4724cce9929b011b5d381ce
[hc2vpp.git] /
1 package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406;
2
3 import io.fd.honeycomb.translate.v3po.VppStateHoneycombReaderFactory;
4 import java.lang.management.ManagementFactory;
5 import javax.management.Attribute;
6 import javax.management.InstanceNotFoundException;
7 import javax.management.ObjectName;
8 import org.opendaylight.controller.config.api.ConflictingVersionException;
9 import org.opendaylight.controller.config.api.ValidationException;
10 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
11 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
12 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.VppJvppImplModule;
13 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.VppJvppImplModuleFactory;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class VppStateHoneycombReaderModule extends
18     org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406.AbstractVppStateHoneycombReaderModule {
19
20     private static final Logger LOG = LoggerFactory.getLogger(VppStateHoneycombReaderModule.class);
21
22     public VppStateHoneycombReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
23                                          org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
24         super(identifier, dependencyResolver);
25     }
26
27     public VppStateHoneycombReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
28                                          org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
29                                          org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.v3po2vpp.rev160406.VppStateHoneycombReaderModule oldModule,
30                                          java.lang.AutoCloseable oldInstance) {
31         super(identifier, dependencyResolver, oldModule, oldInstance);
32     }
33
34     @Override
35     public void customValidation() {
36         // add custom validation form module attributes here.
37     }
38
39     @Override
40     public java.lang.AutoCloseable createInstance() {
41         return new VppStateHoneycombReaderFactory(getVppJvppDependency(),
42                 getInterfaceContextVppStateDependency(),
43                 getBridgeDomainContextVppStateDependency(),
44                 getKeepaliveExecutorDependency());
45     }
46
47     private static long reinitializationCounter;
48     private static final long reinitializationLimit = 10;
49
50     /**
51      * In case we detect connection issues with VPP, reinitialize JVpp.
52      */
53     private static void reinitializeJVpp(final long currentAttempt) {
54         // FIXME https://jira.fd.io/browse/HONEYCOMB-78 This code correctly re-initializes all the components
55         // starting with jvpp, but jvpp reconnect fails. Test in a JVpp test and then from C
56         LOG.info("Reinitializing JVpp, attempt: {}", currentAttempt);
57
58         final long nextAttempt = currentAttempt + 1;
59         if (nextAttempt - reinitializationCounter > reinitializationLimit) {
60             LOG.error("Too many JVpp reinitialization attempts. Unable to reinitialize JVpp in {} attempts. Giving up",
61                 reinitializationLimit);
62             throw new IllegalStateException("Too many JVpp reinitialization attempts. Unable to reinitialize JVpp in "
63                 + reinitializationLimit + " attempts. Giving up");
64         }
65
66         final ConfigRegistryJMXClient cfgRegistryClient =
67             ConfigRegistryJMXClient.createWithoutNotifications(ManagementFactory.getPlatformMBeanServer());
68
69         final ObjectName objectName = cfgRegistryClient.beginConfig();
70         final ConfigTransactionJMXClient txClient = cfgRegistryClient.getConfigTransactionClient(objectName);
71
72         final ObjectName jvppOn;
73         try {
74             final String attributeName = VppJvppImplModule.descriptionJmxAttribute.getAttributeName();
75             final String factoryName = VppJvppImplModuleFactory.NAME;
76             jvppOn = txClient.lookupConfigBean(factoryName, "vpp-jvpp");
77
78             // Change configuration attribute of JVpp to trigger full reinitialization here using config subsystem
79             // TODO improve this when switching from karaf in planned minimal distribution
80             txClient.setAttribute(jvppOn, attributeName, new Attribute(attributeName,
81                 Long.toString(nextAttempt)));
82
83             txClient.validateConfig();
84             cfgRegistryClient.commitConfig(txClient.getObjectName());
85             LOG.info("JVpp reinitialized successfully");
86         } catch (InstanceNotFoundException | ValidationException e) {
87             LOG.error("Unable to reinitialize JVpp. Honeycomb will not work properly from now on.", e);
88             throw new IllegalStateException("Unable to find jvpp instance in config subsystem. Unable to reinitialize JVpp", e);
89         } catch (ConflictingVersionException e) {
90             LOG.debug("Conflict changes occurred, retrying", e);
91             // Just retry until there's no conflicting change in progress
92             reinitializeJVpp(nextAttempt);
93         }
94
95         reinitializationCounter = nextAttempt;
96     }
97
98
99 }