Merge "sal-netconf-connector unit tests added"
[netconf.git] / netconf / netconf-notifications-impl / src / main / java / org / opendaylight / netconf / notifications / impl / osgi / Activator.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.netconf.notifications.impl.osgi;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
14 import java.util.Dictionary;
15 import java.util.Hashtable;
16 import java.util.Set;
17 import org.opendaylight.controller.config.util.capability.BasicCapability;
18 import org.opendaylight.controller.config.util.capability.Capability;
19 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
20 import org.opendaylight.netconf.api.util.NetconfConstants;
21 import org.opendaylight.netconf.mapping.api.NetconfOperation;
22 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
23 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
26 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
27 import org.opendaylight.netconf.notifications.impl.ops.CreateSubscription;
28 import org.opendaylight.netconf.notifications.impl.ops.Get;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceRegistration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class Activator implements BundleActivator {
36
37     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
38
39     private ServiceRegistration<NetconfNotificationCollector> netconfNotificationCollectorServiceRegistration;
40     private ServiceRegistration<NetconfOperationServiceFactory> operationaServiceRegistration;
41     private NetconfNotificationManager netconfNotificationManager;
42
43     @Override
44     public void start(final BundleContext context) throws Exception {
45         netconfNotificationManager = new NetconfNotificationManager();
46         // Add properties to autowire with netconf-impl instance for cfg subsystem
47         final Dictionary<String, String> props = new Hashtable<>();
48         props.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_NOTIFICATION);
49         netconfNotificationCollectorServiceRegistration = context.registerService(NetconfNotificationCollector.class, netconfNotificationManager, new Hashtable<String, Object>());
50
51         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
52
53             private final Set<Capability> capabilities = Collections.<Capability>singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
54
55             @Override
56             public Set<Capability> getCapabilities() {
57                 return capabilities;
58             }
59
60             @Override
61             public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
62                 listener.onCapabilitiesChanged(capabilities, Collections.<Capability>emptySet());
63                 return new AutoCloseable() {
64                     @Override
65                     public void close() {
66                         listener.onCapabilitiesChanged(Collections.<Capability>emptySet(), capabilities);
67                     }
68                 };
69             }
70
71             @Override
72             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
73                 return new NetconfOperationService() {
74
75                     private final CreateSubscription createSubscription = new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
76
77                     @Override
78                     public Set<NetconfOperation> getNetconfOperations() {
79                         return Sets.<NetconfOperation>newHashSet(
80                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
81                                 createSubscription);
82                     }
83
84                     @Override
85                     public void close() {
86                         createSubscription.close();
87                     }
88                 };
89             }
90         };
91
92         final Dictionary<String, String> properties = new Hashtable<>();
93         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
94         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
95     }
96
97     @Override
98     public void stop(final BundleContext context) throws Exception {
99         if(netconfNotificationCollectorServiceRegistration != null) {
100             netconfNotificationCollectorServiceRegistration.unregister();
101             netconfNotificationCollectorServiceRegistration = null;
102         }
103         if (netconfNotificationManager != null) {
104             netconfNotificationManager.close();
105         }
106         if (operationaServiceRegistration != null) {
107             operationaServiceRegistration.unregister();
108             operationaServiceRegistration = null;
109         }
110     }
111
112     @VisibleForTesting
113     NetconfNotificationManager getNetconfNotificationManager() {
114         return netconfNotificationManager;
115     }
116 }