Merge changes I8e90a7d5,I25249fe8
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / Activator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
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;
21
22 import java.util.Dictionary;
23 import java.util.Hashtable;
24
25 import static com.google.common.base.Preconditions.checkState;
26
27 public class Activator implements BundleActivator {
28
29     private static final Logger logger = LoggerFactory.getLogger(Activator.class);
30
31     private BundleContext context;
32     private ServiceRegistration osgiRegistration;
33     private ConfigRegistryLookupThread configRegistryLookup = null;
34
35     @Override
36     public void start(final BundleContext context) throws Exception {
37         this.context = context;
38
39         ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
40             @Override
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");
44
45                 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
46
47                 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(schemaContextProvider);
48                 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
49                 configRegistryLookup.start();
50                 return configRegistryLookup;
51             }
52
53             @Override
54             public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
55                 logger.debug("Got modifiedService(SchemaContextProvider) event");
56                 configRegistryLookup.yangStoreService.refresh();
57
58             }
59
60             @Override
61             public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
62                 configRegistryLookup.interrupt();
63                 if (osgiRegistration != null) {
64                     osgiRegistration.unregister();
65                 }
66                 osgiRegistration = null;
67                 Activator.this.configRegistryLookup = null;
68             }
69         };
70
71         ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
72         listenerTracker.open();
73     }
74
75     @Override
76     public void stop(BundleContext context) {
77         if (configRegistryLookup != null) {
78             configRegistryLookup.interrupt();
79         }
80     }
81
82     private class ConfigRegistryLookupThread extends Thread {
83         private final YangStoreServiceImpl yangStoreService;
84
85         private ConfigRegistryLookupThread(YangStoreServiceImpl yangStoreService) {
86             super("config-registry-lookup");
87             this.yangStoreService = yangStoreService;
88         }
89
90         @Override
91         public void run() {
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);
97         }
98     }
99 }