Added feature for sal-mdsal-xsql
[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 org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
12 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.framework.ServiceRegistration;
16 import org.osgi.util.tracker.ServiceTracker;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.util.Dictionary;
21 import java.util.Hashtable;
22
23 public class NetconfMonitoringServiceTracker extends ServiceTracker<NetconfMonitoringService, NetconfMonitoringService> {
24
25     private static final Logger logger = LoggerFactory.getLogger(NetconfMonitoringServiceTracker.class);
26
27     private ServiceRegistration<NetconfOperationServiceFactory> reg;
28
29     NetconfMonitoringServiceTracker(BundleContext context) {
30         super(context, NetconfMonitoringService.class, null);
31     }
32
33     @Override
34     public NetconfMonitoringService addingService(ServiceReference<NetconfMonitoringService> reference) {
35         Preconditions.checkState(reg == null, "Monitoring service was already added");
36
37         NetconfMonitoringService netconfMonitoringService = super.addingService(reference);
38
39         final NetconfMonitoringOperationService operationService = new NetconfMonitoringOperationService(
40                 netconfMonitoringService);
41         NetconfOperationServiceFactory factory = new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
42                 operationService);
43
44         Dictionary<String, ?> props = new Hashtable<>();
45         reg = context.registerService(NetconfOperationServiceFactory.class, factory, props);
46
47         return netconfMonitoringService;
48     }
49
50     @Override
51     public void removedService(ServiceReference<NetconfMonitoringService> reference,
52             NetconfMonitoringService netconfMonitoringService) {
53         if(reg!=null) {
54             try {
55                 reg.unregister();
56             } catch (Exception e) {
57                 logger.warn("Ignoring exception while unregistering {}", reg, e);
58             }
59         }
60     }
61
62 }