2 * Copyright (c) 2013 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.netconf.confignetconfconnector.osgi;
11 import static com.google.common.base.Preconditions.checkState;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import org.opendaylight.controller.netconf.api.util.NetconfConstants;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
17 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceReference;
22 import org.osgi.framework.ServiceRegistration;
23 import org.osgi.util.tracker.ServiceTracker;
24 import org.osgi.util.tracker.ServiceTrackerCustomizer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class Activator implements BundleActivator {
30 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
32 private BundleContext context;
33 private ServiceRegistration<?> osgiRegistration;
34 private ConfigRegistryLookupThread configRegistryLookup = null;
37 public void start(final BundleContext context) throws Exception {
38 this.context = context;
40 ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
42 public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
43 LOG.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
44 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
46 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
48 YangStoreService yangStoreService = new YangStoreService(schemaContextProvider, context);
49 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
50 configRegistryLookup.start();
51 return configRegistryLookup;
55 public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
56 LOG.debug("Got modifiedService(SchemaContextProvider) event");
57 final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference.getProperty(BindingRuntimeContext.class.getName());
58 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
59 configRegistryLookup.yangStoreService.refresh(runtimeContext);
64 public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
65 configRegistryLookup.interrupt();
66 if (osgiRegistration != null) {
67 osgiRegistration.unregister();
69 osgiRegistration = null;
70 Activator.this.configRegistryLookup = null;
74 ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
75 listenerTracker.open();
79 public void stop(BundleContext context) {
80 if (configRegistryLookup != null) {
81 configRegistryLookup.interrupt();
85 private class ConfigRegistryLookupThread extends Thread {
86 private final YangStoreService yangStoreService;
88 private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
89 super("config-registry-lookup");
90 this.yangStoreService = yangStoreService;
95 NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
96 LOG.debug("Registering into OSGi");
97 Dictionary<String, String> properties = new Hashtable<>();
98 properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
99 osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);