Merge "Migrate NetconfHelloMessage's use of Optional"
[netconf.git] / netconf / netconf-monitoring / src / main / java / org / opendaylight / 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.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.netconf.api.monitoring.NetconfMonitoringService;
14 import org.opendaylight.netconf.api.util.NetconfConstants;
15 import org.opendaylight.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,
24         NetconfMonitoringService> {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceTracker.class);
27
28     private ServiceRegistration<NetconfOperationServiceFactory> reg;
29     private NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory factory;
30
31     NetconfMonitoringServiceTracker(final BundleContext context) {
32         super(context, NetconfMonitoringService.class, null);
33     }
34
35     @Override
36     public NetconfMonitoringService addingService(final ServiceReference<NetconfMonitoringService> reference) {
37         Preconditions.checkState(reg == null, "Monitoring service was already added");
38
39         final NetconfMonitoringService netconfMonitoringService = super.addingService(reference);
40
41         final NetconfMonitoringOperationService operationService = new NetconfMonitoringOperationService(
42                 netconfMonitoringService);
43         factory = new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
44                 operationService);
45
46         Dictionary<String, String> properties = new Hashtable<>();
47         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
48         reg = context.registerService(NetconfOperationServiceFactory.class, factory, properties);
49
50         return netconfMonitoringService;
51     }
52
53     @SuppressWarnings("checkstyle:IllegalCatch")
54     @Override
55     public void removedService(final ServiceReference<NetconfMonitoringService> reference,
56                                final NetconfMonitoringService netconfMonitoringService) {
57         if (reg != null) {
58             try {
59                 reg.unregister();
60             } catch (final Exception e) {
61                 LOG.warn("Ignoring exception while unregistering {}", reg, e);
62             }
63         }
64         if (factory != null) {
65             factory.close();
66         }
67     }
68
69 }