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.yang.model.api.SchemaContextProvider;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.ServiceTracker;
23 import org.osgi.util.tracker.ServiceTrackerCustomizer;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class Activator implements BundleActivator {
29 private static final Logger LOG = 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 LOG.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 YangStoreService yangStoreService = new YangStoreService(schemaContextProvider, context);
48 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
49 configRegistryLookup.start();
50 return configRegistryLookup;
54 public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
55 LOG.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) {
77 if (configRegistryLookup != null) {
78 configRegistryLookup.interrupt();
82 private class ConfigRegistryLookupThread extends Thread {
83 private final YangStoreService yangStoreService;
85 private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
86 super("config-registry-lookup");
87 this.yangStoreService = yangStoreService;
92 NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
93 LOG.debug("Registering into OSGi");
94 Dictionary<String, String> properties = new Hashtable<>();
95 properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
96 osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);