2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
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;
25 public class Activator implements BundleActivator {
27 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
29 private ServiceRegistration<?> osgiRegistration;
32 public void start(final BundleContext context) throws Exception {
33 ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory>() {
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;
45 public void modifiedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
46 LOG.warn("Config manager facade was modified unexpectedly");
50 public void removedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
51 LOG.warn("Config manager facade was removed unexpectedly");
55 ServiceTracker<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaContextProviderServiceTracker =
56 new ServiceTracker<>(context, ConfigSubsystemFacadeFactory.class, schemaServiceTrackerCustomizer);
57 schemaContextProviderServiceTracker.open();
61 public void stop(final BundleContext bundleContext) throws Exception {
62 if (osgiRegistration != null) {
63 osgiRegistration.unregister();
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);