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