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 static com.google.common.base.Preconditions.checkState;
24 public class Activator implements BundleActivator {
26 private static final Logger logger = LoggerFactory.getLogger(Activator.class);
28 private BundleContext context;
29 private ServiceRegistration osgiRegistration;
30 private ConfigRegistryLookupThread configRegistryLookup = null;
33 public void start(final BundleContext context) throws Exception {
34 this.context = context;
36 ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
38 public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
39 logger.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
40 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
42 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
44 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(schemaContextProvider);
45 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
46 configRegistryLookup.start();
47 return configRegistryLookup;
51 public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
52 logger.debug("Got modifiedService(SchemaContextProvider) event");
53 configRegistryLookup.yangStoreService.refresh();
58 public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
59 configRegistryLookup.interrupt();
60 if (osgiRegistration != null) {
61 osgiRegistration.unregister();
63 osgiRegistration = null;
64 Activator.this.configRegistryLookup = null;
68 ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
69 listenerTracker.open();
73 public void stop(BundleContext context) throws Exception {
74 if (configRegistryLookup != null) {
75 configRegistryLookup.interrupt();
79 private class ConfigRegistryLookupThread extends Thread {
80 private final YangStoreServiceImpl yangStoreService;
82 private ConfigRegistryLookupThread(YangStoreServiceImpl yangStoreService) {
83 super("config-registry-lookup");
84 this.yangStoreService = yangStoreService;
89 NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
90 logger.debug("Registering into OSGi");
91 osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, null);