Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[controller.git] / opendaylight / netconf / netconf-monitoring / src / main / java / org / opendaylight / controller / netconf / monitoring / osgi / NetconfMonitoringServiceTracker.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 package org.opendaylight.controller.netconf.monitoring.osgi;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Dictionary;
12 import java.util.Hashtable;
13 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
14 import org.opendaylight.controller.netconf.api.util.NetconfConstants;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.framework.ServiceRegistration;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class NetconfMonitoringServiceTracker extends ServiceTracker<NetconfMonitoringService, NetconfMonitoringService> {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceTracker.class);
26
27     private ServiceRegistration<NetconfOperationServiceFactory> reg;
28     private NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory factory;
29
30     NetconfMonitoringServiceTracker(final BundleContext context) {
31         super(context, NetconfMonitoringService.class, null);
32     }
33
34     @Override
35     public NetconfMonitoringService addingService(final ServiceReference<NetconfMonitoringService> reference) {
36         Preconditions.checkState(reg == null, "Monitoring service was already added");
37
38         final NetconfMonitoringService netconfMonitoringService = super.addingService(reference);
39
40         final NetconfMonitoringOperationService operationService = new NetconfMonitoringOperationService(
41                 netconfMonitoringService);
42         factory = new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
43                 operationService);
44
45         Dictionary<String, String> properties = new Hashtable<>();
46         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
47         reg = context.registerService(NetconfOperationServiceFactory.class, factory, properties);
48
49         return netconfMonitoringService;
50     }
51
52     @Override
53     public void removedService(final ServiceReference<NetconfMonitoringService> reference,
54             final NetconfMonitoringService netconfMonitoringService) {
55         if(reg!=null) {
56             try {
57                 reg.unregister();
58             } catch (final Exception e) {
59                 LOG.warn("Ignoring exception while unregistering {}", reg, e);
60             }
61         }
62         if(factory!=null) {
63             factory.close();
64         }
65     }
66
67 }