Remove netconf from commons/opendaylight pom
[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 java.util.Dictionary;
12 import java.util.Hashtable;
13 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
14 import org.opendaylight.controller.netconf.api.util.NetconfConstants;
15 import org.opendaylight.controller.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, ConfigSubsystemFacadeFactory> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory>() {
34
35             @Override
36             public ConfigSubsystemFacadeFactory addingService(ServiceReference<ConfigSubsystemFacadeFactory> reference) {
37                 LOG.debug("Got addingService(SchemaContextProvider) event");
38                 // Yang store service should not be registered multiple times
39                 ConfigSubsystemFacadeFactory configSubsystemFacade = reference.getBundle().getBundleContext().getService(reference);
40                 osgiRegistration = startNetconfServiceFactory(configSubsystemFacade, context);
41                 return configSubsystemFacade;
42             }
43
44             @Override
45             public void modifiedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
46                 LOG.warn("Config manager facade was modified unexpectedly");
47             }
48
49             @Override
50             public void removedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
51                 LOG.warn("Config manager facade was removed unexpectedly");
52             }
53         };
54
55         ServiceTracker<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaContextProviderServiceTracker =
56                 new ServiceTracker<>(context, ConfigSubsystemFacadeFactory.class, schemaServiceTrackerCustomizer);
57         schemaContextProviderServiceTracker.open();
58     }
59
60     @Override
61     public void stop(final BundleContext bundleContext) throws Exception {
62         if (osgiRegistration != null) {
63             osgiRegistration.unregister();
64         }
65     }
66
67     private ServiceRegistration<NetconfOperationServiceFactory> startNetconfServiceFactory(final ConfigSubsystemFacadeFactory configSubsystemFacade, final BundleContext context) {
68         final NetconfOperationServiceFactoryImpl netconfOperationServiceFactory = new NetconfOperationServiceFactoryImpl(configSubsystemFacade);
69         // Add properties to autowire with netconf-impl instance for cfg subsystem
70         final Dictionary<String, String> properties = new Hashtable<>();
71         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
72         return context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
73     }
74
75 }