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 org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
12 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
13 import org.osgi.framework.BundleActivator;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.framework.ServiceRegistration;
17 import org.osgi.util.tracker.ServiceTracker;
18 import org.osgi.util.tracker.ServiceTrackerCustomizer;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.util.Dictionary;
23 import java.util.Hashtable;
25 import static com.google.common.base.Preconditions.checkState;
27 public class Activator implements BundleActivator {
29 private static final Logger logger = LoggerFactory.getLogger(Activator.class);
31 private BundleContext context;
32 private ServiceRegistration osgiRegistration;
33 private ConfigRegistryLookupThread configRegistryLookup = null;
36 public void start(final BundleContext context) throws Exception {
37 this.context = context;
39 ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
41 public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
42 logger.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
43 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
45 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
47 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(schemaContextProvider);
48 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
49 configRegistryLookup.start();
50 return configRegistryLookup;
54 public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
55 logger.debug("Got modifiedService(SchemaContextProvider) event");
56 configRegistryLookup.yangStoreService.refresh();
61 public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
62 configRegistryLookup.interrupt();
63 if (osgiRegistration != null) {
64 osgiRegistration.unregister();
66 osgiRegistration = null;
67 Activator.this.configRegistryLookup = null;
71 ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
72 listenerTracker.open();
76 public void stop(BundleContext context) throws Exception {
77 if (configRegistryLookup != null) {
78 configRegistryLookup.interrupt();
82 private class ConfigRegistryLookupThread extends Thread {
83 private final YangStoreServiceImpl yangStoreService;
85 private ConfigRegistryLookupThread(YangStoreServiceImpl yangStoreService) {
86 super("config-registry-lookup");
87 this.yangStoreService = yangStoreService;
92 NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
93 logger.debug("Registering into OSGi");
94 Dictionary<String, String> properties = new Hashtable<>();
95 properties.put("name", "config-netconf-connector");
96 osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);