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.mapping.api.NetconfOperationServiceFactory;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.osgi.framework.ServiceRegistration;
21 import org.osgi.util.tracker.ServiceTracker;
22 import org.osgi.util.tracker.ServiceTrackerCustomizer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class Activator implements BundleActivator {
28 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
30 private BundleContext context;
31 private ServiceRegistration<?> osgiRegistration;
32 private ConfigRegistryLookupThread configRegistryLookup = null;
35 public void start(final BundleContext context) throws Exception {
36 this.context = context;
38 ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
40 public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
41 LOG.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
42 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
44 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
46 YangStoreService yangStoreService = new YangStoreService(schemaContextProvider, context);
47 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
48 configRegistryLookup.start();
49 return configRegistryLookup;
53 public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
54 LOG.debug("Got modifiedService(SchemaContextProvider) event");
55 configRegistryLookup.yangStoreService.refresh();
60 public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
61 configRegistryLookup.interrupt();
62 if (osgiRegistration != null) {
63 osgiRegistration.unregister();
65 osgiRegistration = null;
66 Activator.this.configRegistryLookup = null;
70 ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
71 listenerTracker.open();
75 public void stop(BundleContext context) {
76 if (configRegistryLookup != null) {
77 configRegistryLookup.interrupt();
81 private class ConfigRegistryLookupThread extends Thread {
82 private final YangStoreService yangStoreService;
84 private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
85 super("config-registry-lookup");
86 this.yangStoreService = yangStoreService;
91 NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
92 LOG.debug("Registering into OSGi");
93 Dictionary<String, String> properties = new Hashtable<>();
94 properties.put("name", "config-netconf-connector");
95 osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);