BUG-4367: Use SchemaSourceProvider to retrieve sources for yang
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / osgi / YangStoreActivator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.facade.xml.osgi;
10
11 import com.google.common.base.Preconditions;
12 import java.lang.management.ManagementFactory;
13 import java.util.Hashtable;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import javax.management.MBeanServer;
16 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
17 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
18 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
20 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
22 import org.osgi.framework.BundleActivator;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceReference;
25 import org.osgi.framework.ServiceRegistration;
26 import org.osgi.util.tracker.ServiceTracker;
27 import org.osgi.util.tracker.ServiceTrackerCustomizer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Start yang store service and the XML config manager facade
33  */
34 public class YangStoreActivator implements BundleActivator {
35
36     private static final Logger LOG = LoggerFactory.getLogger(YangStoreActivator.class);
37
38     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
39
40     private ServiceRegistration<YangStoreService> yangStoreServiceServiceRegistration;
41     private ConfigRegistryLookupThread configRegistryLookup = null;
42     private BundleContext context;
43     private ServiceRegistration<ConfigSubsystemFacadeFactory> osgiRegistrayion;
44
45     @Override
46     public void start(final BundleContext context) throws Exception {
47         LOG.debug("ConfigPersister starting");
48         this.context = context;
49
50         ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService>() {
51
52             private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
53
54             @Override
55             public YangStoreService addingService(ServiceReference<SchemaContextProvider> reference) {
56                 LOG.debug("Got addingService(SchemaContextProvider) event");
57                 if(reference.getProperty(SchemaSourceProvider.class.getName()) == null &&
58                     reference.getProperty(BindingRuntimeContext.class.getName()) == null) {
59                     LOG.debug("SchemaContextProvider not from config-manager. Ignoring");
60                     return null;
61                 }
62
63                 // Yang store service should not be registered multiple times
64                 if(!alreadyStarted.compareAndSet(false, true)) {
65                     LOG.warn("Starting yang store service multiple times. Received new service {}", reference);
66                     throw new RuntimeException("Starting yang store service multiple times");
67                 }
68                 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
69                 final Object sourceProvider = Preconditions.checkNotNull(
70                     reference.getProperty(SchemaSourceProvider.class.getName()), "Source provider not found");
71                 Preconditions.checkArgument(sourceProvider instanceof SchemaSourceProvider);
72
73                 // TODO avoid cast
74                 final YangStoreService yangStoreService = new YangStoreService(schemaContextProvider,
75                     ((SchemaSourceProvider<YangTextSchemaSource>) sourceProvider));
76                 yangStoreServiceServiceRegistration = context.registerService(YangStoreService.class, yangStoreService, new Hashtable<String, Object>());
77                 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
78                 configRegistryLookup.start();
79                 return yangStoreService;
80             }
81
82             @Override
83             public void modifiedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
84                 if (service == null) {
85                     return;
86                 }
87
88                 LOG.debug("Got modifiedService(SchemaContextProvider) event");
89                 final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference
90                     .getProperty(BindingRuntimeContext.class.getName());
91                 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
92                 service.refresh(runtimeContext);
93             }
94
95             @Override
96             public void removedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
97                 if(service == null) {
98                     return;
99                 }
100
101                 LOG.debug("Got removedService(SchemaContextProvider) event");
102                 alreadyStarted.set(false);
103                 configRegistryLookup.interrupt();
104                 yangStoreServiceServiceRegistration.unregister();
105                 yangStoreServiceServiceRegistration = null;
106             }
107         };
108
109         ServiceTracker<SchemaContextProvider, YangStoreService> schemaContextProviderServiceTracker =
110                 new ServiceTracker<>(context, SchemaContextProvider.class, schemaServiceTrackerCustomizer);
111         schemaContextProviderServiceTracker.open();
112     }
113
114     @Override
115     public void stop(BundleContext context) throws Exception {
116         if(configRegistryLookup != null) {
117             configRegistryLookup.interrupt();
118         }
119         if(osgiRegistrayion != null) {
120             osgiRegistrayion.unregister();
121         }
122         if (yangStoreServiceServiceRegistration != null) {
123             yangStoreServiceServiceRegistration.unregister();
124             yangStoreServiceServiceRegistration = null;
125         }
126     }
127
128     /**
129      * Find ConfigRegistry from config manager in JMX
130      */
131     private class ConfigRegistryLookupThread extends Thread {
132         public static final int ATTEMPT_TIMEOUT_MS = 1000;
133         private static final int SILENT_ATTEMPTS = 30;
134
135         private final YangStoreService yangStoreService;
136
137         private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
138             super("config-registry-lookup");
139             this.yangStoreService = yangStoreService;
140         }
141
142         @Override
143         public void run() {
144
145             ConfigRegistryJMXClient configRegistryJMXClient;
146             ConfigRegistryJMXClient configRegistryJMXClientNoNotifications;
147             int i = 0;
148             // Config registry might not be present yet, but will be eventually
149             while(true) {
150
151                 try {
152                     configRegistryJMXClient = new ConfigRegistryJMXClient(configMBeanServer);
153                     configRegistryJMXClientNoNotifications = ConfigRegistryJMXClient.createWithoutNotifications(configMBeanServer);
154                     break;
155                 } catch (IllegalStateException e) {
156                     ++i;
157                     if (i > SILENT_ATTEMPTS) {
158                         LOG.info("JMX client not created after {} attempts, still trying", i, e);
159                     } else {
160                         LOG.debug("JMX client could not be created, reattempting, try {}", i, e);
161                     }
162                     try {
163                         Thread.sleep(ATTEMPT_TIMEOUT_MS);
164                     } catch (InterruptedException e1) {
165                         Thread.currentThread().interrupt();
166                         throw new IllegalStateException("Interrupted while reattempting connection", e1);
167                     }
168                 }
169             }
170
171             final ConfigRegistryJMXClient jmxClient = configRegistryJMXClient;
172             final ConfigRegistryJMXClient jmxClientNoNotifications = configRegistryJMXClientNoNotifications;
173             if (i > SILENT_ATTEMPTS) {
174                 LOG.info("Created JMX client after {} attempts", i);
175             } else {
176                 LOG.debug("Created JMX client after {} attempts", i);
177             }
178
179             final ConfigSubsystemFacadeFactory configSubsystemFacade =
180                     new ConfigSubsystemFacadeFactory(jmxClient, jmxClientNoNotifications, yangStoreService);
181             osgiRegistrayion = context.registerService(ConfigSubsystemFacadeFactory.class, configSubsystemFacade, new Hashtable<String, Object>());
182         }
183     }
184 }
185