Merge "Add test for generated code checking list of dependencies."
[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     ServiceRegistration osgiRegistration;
29
30     @Override
31     public void start(BundleContext context) throws Exception {
32         this.context = context;
33         YangStoreServiceTracker tracker = new YangStoreServiceTracker(context, this);
34         tracker.open();
35     }
36
37     @Override
38     public void stop(BundleContext context) throws Exception {
39     }
40
41     @Override
42     public synchronized void onYangStoreAdded(YangStoreService yangStoreService) {
43         checkState(osgiRegistration == null, "More than one onYangStoreAdded received");
44         NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
45         logger.debug("Registering into OSGi");
46         osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
47                 new Hashtable<String, Object>());
48     }
49
50     @Override
51     public synchronized void onYangStoreRemoved() {
52         osgiRegistration.unregister();
53         osgiRegistration = null;
54     }
55 }