5642cc7188788fd74a7aeccf7ca8ffa97abd009e
[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.config.yang.store.api.YangStoreService;
12 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
13 import org.osgi.framework.BundleActivator;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.ServiceRegistration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.Hashtable;
20
21 import static com.google.common.base.Preconditions.checkState;
22
23 public class Activator implements BundleActivator, YangStoreServiceTracker.YangStoreTrackerListener {
24
25     private static final Logger logger = LoggerFactory.getLogger(Activator.class);
26
27     private BundleContext context;
28     private ServiceRegistration osgiRegistration;
29     private ConfigRegistryLookupThread configRegistryLookup = null;
30
31     @Override
32     public void start(BundleContext context) throws Exception {
33         this.context = context;
34         YangStoreServiceTracker tracker = new YangStoreServiceTracker(context, this);
35         tracker.open();
36     }
37
38     @Override
39     public void stop(BundleContext context) throws Exception {
40         if (configRegistryLookup != null) {
41             configRegistryLookup.interrupt();
42         }
43     }
44
45     @Override
46     public synchronized void onYangStoreAdded(YangStoreService yangStoreService) {
47         checkState(configRegistryLookup  == null, "More than one onYangStoreAdded received");
48         configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
49         configRegistryLookup.start();
50     }
51
52     @Override
53     public synchronized void onYangStoreRemoved() {
54         configRegistryLookup.interrupt();
55         if (osgiRegistration != null) {
56             osgiRegistration.unregister();
57         }
58         osgiRegistration = null;
59         configRegistryLookup = null;
60     }
61
62     private class ConfigRegistryLookupThread extends Thread {
63         private final YangStoreService yangStoreService;
64
65         private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
66             super("config-registry-lookup");
67             this.yangStoreService = yangStoreService;
68         }
69
70         @Override
71         public void run() {
72             NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
73             logger.debug("Registering into OSGi");
74             osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
75                     new Hashtable<String, Object>());
76         }
77     }
78 }