Merge "Remove unused controller config dependencies"
[netconf.git] / netconf / config-netconf-connector / src / main / java / org / opendaylight / 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.netconf.confignetconfconnector.osgi;
10
11 import java.util.Dictionary;
12 import java.util.Hashtable;
13 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
14 import org.opendaylight.netconf.api.util.NetconfConstants;
15 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
16 import org.osgi.framework.BundleActivator;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceReference;
19 import org.osgi.framework.ServiceRegistration;
20 import org.osgi.util.tracker.ServiceTracker;
21 import org.osgi.util.tracker.ServiceTrackerCustomizer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class Activator implements BundleActivator {
26
27     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
28
29     private ServiceRegistration<?> osgiRegistration;
30
31     @Override
32     public void start(final BundleContext context) throws Exception {
33         ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory,
34                 ConfigSubsystemFacadeFactory> schemaServiceTrackerCustomizer =
35                 new ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory>() {
36
37             @Override
38             public ConfigSubsystemFacadeFactory addingService(
39                     ServiceReference<ConfigSubsystemFacadeFactory> reference) {
40                 LOG.debug("Got addingService(SchemaContextProvider) event");
41                 // Yang store service should not be registered multiple times
42                 ConfigSubsystemFacadeFactory configSubsystemFacade =
43                         reference.getBundle().getBundleContext().getService(reference);
44                 osgiRegistration = startNetconfServiceFactory(configSubsystemFacade, context);
45                 return configSubsystemFacade;
46             }
47
48             @Override
49             public void modifiedService(ServiceReference<ConfigSubsystemFacadeFactory> reference,
50                                         ConfigSubsystemFacadeFactory service) {
51                 LOG.warn("Config manager facade was modified unexpectedly");
52             }
53
54             @Override
55             public void removedService(ServiceReference<ConfigSubsystemFacadeFactory> reference,
56                                        ConfigSubsystemFacadeFactory service) {
57                 LOG.warn("Config manager facade was removed unexpectedly");
58             }
59         };
60
61         ServiceTracker<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaContextProviderServiceTracker =
62                 new ServiceTracker<>(context, ConfigSubsystemFacadeFactory.class, schemaServiceTrackerCustomizer);
63         schemaContextProviderServiceTracker.open();
64     }
65
66     @Override
67     public void stop(final BundleContext bundleContext) throws Exception {
68         if (osgiRegistration != null) {
69             osgiRegistration.unregister();
70         }
71     }
72
73     private ServiceRegistration<NetconfOperationServiceFactory> startNetconfServiceFactory(
74             final ConfigSubsystemFacadeFactory configSubsystemFacade, final BundleContext context) {
75         final NetconfOperationServiceFactoryImpl netconfOperationServiceFactory =
76                 new NetconfOperationServiceFactoryImpl(configSubsystemFacade);
77         // Add properties to autowire with netconf-impl instance for cfg subsystem
78         final Dictionary<String, String> properties = new Hashtable<>();
79         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
80         return context.registerService(NetconfOperationServiceFactory.class,
81                 netconfOperationServiceFactory, properties);
82     }
83
84 }