Finish bumping to yangtools 2.1.8
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / osgi / NetconfOperationServiceFactoryTracker.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.impl.osgi;
9
10 import org.opendaylight.netconf.api.util.NetconfConstants;
11 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
12 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.util.tracker.ServiceTracker;
16
17 class NetconfOperationServiceFactoryTracker extends
18         ServiceTracker<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
19     private final NetconfOperationServiceFactoryListener factoriesListener;
20
21     NetconfOperationServiceFactoryTracker(final BundleContext context,
22                                           final NetconfOperationServiceFactoryListener factoriesListener) {
23         super(context, NetconfOperationServiceFactory.class, null);
24         this.factoriesListener = factoriesListener;
25     }
26
27     @Override
28     public NetconfOperationServiceFactory addingService(
29             final ServiceReference<NetconfOperationServiceFactory> reference) {
30         Object property = reference.getProperty(NetconfConstants.SERVICE_NAME);
31         if (property != null && isMarkedForConfig(property)) {
32             NetconfOperationServiceFactory netconfOperationServiceFactory = super.addingService(reference);
33             factoriesListener.onAddNetconfOperationServiceFactory(netconfOperationServiceFactory);
34             return netconfOperationServiceFactory;
35         }
36
37         return null;
38     }
39
40     @Override
41     public void removedService(final ServiceReference<NetconfOperationServiceFactory> reference,
42                                final NetconfOperationServiceFactory netconfOperationServiceFactory) {
43         if (netconfOperationServiceFactory != null) {
44             factoriesListener.onRemoveNetconfOperationServiceFactory(netconfOperationServiceFactory);
45         }
46     }
47
48     private static boolean isMarkedForConfig(final Object property) {
49         return NetconfConstants.CONFIG_SERVICE_MARKERS.contains(property);
50     }
51
52 }