2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.osgi;
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;
32 * Start yang store service and the XML config manager facade
34 public class YangStoreActivator implements BundleActivator {
36 private static final Logger LOG = LoggerFactory.getLogger(YangStoreActivator.class);
38 private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
40 private ServiceRegistration<YangStoreService> yangStoreServiceServiceRegistration;
41 private ConfigRegistryLookupThread configRegistryLookup = null;
42 private BundleContext context;
43 private ServiceRegistration<ConfigSubsystemFacadeFactory> osgiRegistrayion;
46 public void start(final BundleContext context) throws Exception {
47 LOG.debug("ConfigPersister starting");
48 this.context = context;
50 ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService>() {
52 private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
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");
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");
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);
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;
83 public void modifiedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
84 if (service == null) {
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);
96 public void removedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
101 LOG.debug("Got removedService(SchemaContextProvider) event");
102 alreadyStarted.set(false);
103 configRegistryLookup.interrupt();
104 yangStoreServiceServiceRegistration.unregister();
105 yangStoreServiceServiceRegistration = null;
109 ServiceTracker<SchemaContextProvider, YangStoreService> schemaContextProviderServiceTracker =
110 new ServiceTracker<>(context, SchemaContextProvider.class, schemaServiceTrackerCustomizer);
111 schemaContextProviderServiceTracker.open();
115 public void stop(BundleContext context) throws Exception {
116 if(configRegistryLookup != null) {
117 configRegistryLookup.interrupt();
119 if(osgiRegistrayion != null) {
120 osgiRegistrayion.unregister();
122 if (yangStoreServiceServiceRegistration != null) {
123 yangStoreServiceServiceRegistration.unregister();
124 yangStoreServiceServiceRegistration = null;
129 * Find ConfigRegistry from config manager in JMX
131 private class ConfigRegistryLookupThread extends Thread {
132 public static final int ATTEMPT_TIMEOUT_MS = 1000;
133 private static final int SILENT_ATTEMPTS = 30;
135 private final YangStoreService yangStoreService;
137 private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
138 super("config-registry-lookup");
139 this.yangStoreService = yangStoreService;
145 ConfigRegistryJMXClient configRegistryJMXClient;
146 ConfigRegistryJMXClient configRegistryJMXClientNoNotifications;
148 // Config registry might not be present yet, but will be eventually
152 configRegistryJMXClient = new ConfigRegistryJMXClient(configMBeanServer);
153 configRegistryJMXClientNoNotifications = ConfigRegistryJMXClient.createWithoutNotifications(configMBeanServer);
155 } catch (IllegalStateException e) {
157 if (i > SILENT_ATTEMPTS) {
158 LOG.info("JMX client not created after {} attempts, still trying", i, e);
160 LOG.debug("JMX client could not be created, reattempting, try {}", i, e);
163 Thread.sleep(ATTEMPT_TIMEOUT_MS);
164 } catch (InterruptedException e1) {
165 Thread.currentThread().interrupt();
166 throw new IllegalStateException("Interrupted while reattempting connection", e1);
171 final ConfigRegistryJMXClient jmxClient = configRegistryJMXClient;
172 final ConfigRegistryJMXClient jmxClientNoNotifications = configRegistryJMXClientNoNotifications;
173 if (i > SILENT_ATTEMPTS) {
174 LOG.info("Created JMX client after {} attempts", i);
176 LOG.debug("Created JMX client after {} attempts", i);
179 final ConfigSubsystemFacadeFactory configSubsystemFacade =
180 new ConfigSubsystemFacadeFactory(jmxClient, jmxClientNoNotifications, yangStoreService);
181 osgiRegistrayion = context.registerService(ConfigSubsystemFacadeFactory.class, configSubsystemFacade, new Hashtable<String, Object>());