2 * Copyright (c) 2015, 2017 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.mdsal.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 final ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService> schemaServiceTrackerCustomizer =
51 new ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService>() {
53 private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
56 public YangStoreService addingService(final ServiceReference<SchemaContextProvider> reference) {
57 LOG.debug("Got addingService(SchemaContextProvider) event");
58 if (reference.getProperty(SchemaSourceProvider.class.getName()) == null
59 && reference.getProperty(BindingRuntimeContext.class.getName()) == null) {
60 LOG.debug("SchemaContextProvider not from config-manager. Ignoring");
64 // Yang store service should not be registered multiple times
65 if (!this.alreadyStarted.compareAndSet(false, true)) {
66 LOG.warn("Starting yang store service multiple times. Received new service {}", reference);
67 throw new RuntimeException("Starting yang store service multiple times");
69 final SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext()
70 .getService(reference);
71 final Object sourceProvider = Preconditions.checkNotNull(
72 reference.getProperty(SchemaSourceProvider.class.getName()), "Source provider not found");
73 Preconditions.checkArgument(sourceProvider instanceof SchemaSourceProvider);
76 final YangStoreService yangStoreService = new YangStoreService(schemaContextProvider,
77 (SchemaSourceProvider<YangTextSchemaSource>) sourceProvider);
79 final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference
80 .getProperty(BindingRuntimeContext.class.getName());
81 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
82 if (runtimeContext != null) {
83 yangStoreService.refresh(runtimeContext);
86 YangStoreActivator.this.yangStoreServiceServiceRegistration = context
87 .registerService(YangStoreService.class, yangStoreService, new Hashtable<>());
88 YangStoreActivator.this.configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
89 YangStoreActivator.this.configRegistryLookup.start();
90 return yangStoreService;
94 public void modifiedService(final ServiceReference<SchemaContextProvider> reference,
95 final YangStoreService service) {
96 if (service == null) {
100 LOG.debug("Got modifiedService(SchemaContextProvider) event");
101 final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference
102 .getProperty(BindingRuntimeContext.class.getName());
103 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
104 service.refresh(runtimeContext);
108 public void removedService(final ServiceReference<SchemaContextProvider> reference,
109 final YangStoreService service) {
110 if (service == null) {
114 LOG.debug("Got removedService(SchemaContextProvider) event");
115 this.alreadyStarted.set(false);
116 YangStoreActivator.this.configRegistryLookup.interrupt();
117 YangStoreActivator.this.yangStoreServiceServiceRegistration.unregister();
118 YangStoreActivator.this.yangStoreServiceServiceRegistration = null;
122 final ServiceTracker<SchemaContextProvider, YangStoreService> schemaContextProviderServiceTracker =
123 new ServiceTracker<>(context, SchemaContextProvider.class, schemaServiceTrackerCustomizer);
124 schemaContextProviderServiceTracker.open();
128 public void stop(final BundleContext context) throws Exception {
129 if (this.configRegistryLookup != null) {
130 this.configRegistryLookup.interrupt();
132 if (this.osgiRegistrayion != null) {
133 this.osgiRegistrayion.unregister();
135 if (this.yangStoreServiceServiceRegistration != null) {
136 this.yangStoreServiceServiceRegistration.unregister();
137 this.yangStoreServiceServiceRegistration = null;
142 * Find ConfigRegistry from config manager in JMX.
144 private class ConfigRegistryLookupThread extends Thread {
145 public static final int ATTEMPT_TIMEOUT_MS = 1000;
146 private static final int SILENT_ATTEMPTS = 30;
148 private final YangStoreService yangStoreService;
150 private ConfigRegistryLookupThread(final YangStoreService yangStoreService) {
151 super("config-registry-lookup");
152 this.yangStoreService = yangStoreService;
158 ConfigRegistryJMXClient configRegistryJMXClient;
159 ConfigRegistryJMXClient configRegistryJMXClientNoNotifications;
161 // Config registry might not be present yet, but will be eventually
165 configRegistryJMXClient = new ConfigRegistryJMXClient(YangStoreActivator.this.configMBeanServer);
166 configRegistryJMXClientNoNotifications = ConfigRegistryJMXClient
167 .createWithoutNotifications(YangStoreActivator.this.configMBeanServer);
169 } catch (final IllegalStateException e) {
171 if (index > SILENT_ATTEMPTS) {
172 LOG.info("JMX client not created after {} attempts, still trying", index, e);
174 LOG.debug("JMX client could not be created, reattempting, try {}", index, e);
177 Thread.sleep(ATTEMPT_TIMEOUT_MS);
178 } catch (final InterruptedException e1) {
179 Thread.currentThread().interrupt();
180 throw new IllegalStateException("Interrupted while reattempting connection", e1);
185 final ConfigRegistryJMXClient jmxClient = configRegistryJMXClient;
186 final ConfigRegistryJMXClient jmxClientNoNotifications = configRegistryJMXClientNoNotifications;
187 if (index > SILENT_ATTEMPTS) {
188 LOG.info("Created JMX client after {} attempts", index);
190 LOG.debug("Created JMX client after {} attempts", index);
193 final ConfigSubsystemFacadeFactory configSubsystemFacade = new ConfigSubsystemFacadeFactory(jmxClient,
194 jmxClientNoNotifications, this.yangStoreService);
195 YangStoreActivator.this.osgiRegistrayion = YangStoreActivator.this.context
196 .registerService(ConfigSubsystemFacadeFactory.class, configSubsystemFacade, new Hashtable<>());