Add 'protocol-framework/' from commit '5d015c2c5f800d136406c15fcb64fd531d4ffc26'
[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,
50                 netconfNotificationManager, new Hashtable<String, Object>());
51
52         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
53
54             private final Set<Capability> capabilities =
55                     Collections.<Capability>singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
56
57             @Override
58             public Set<Capability> getCapabilities() {
59                 return capabilities;
60             }
61
62             @Override
63             public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
64                 listener.onCapabilitiesChanged(capabilities, Collections.<Capability>emptySet());
65                 return new AutoCloseable() {
66                     @Override
67                     public void close() {
68                         listener.onCapabilitiesChanged(Collections.<Capability>emptySet(), capabilities);
69                     }
70                 };
71             }
72
73             @Override
74             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
75                 return new NetconfOperationService() {
76
77                     private final CreateSubscription createSubscription =
78                             new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
79
80                     @Override
81                     public Set<NetconfOperation> getNetconfOperations() {
82                         return Sets.<NetconfOperation>newHashSet(
83                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
84                                 createSubscription);
85                     }
86
87                     @Override
88                     public void close() {
89                         createSubscription.close();
90                     }
91                 };
92             }
93         };
94
95         final Dictionary<String, String> properties = new Hashtable<>();
96         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
97         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class,
98                 netconfOperationServiceFactory, properties);
99     }
100
101     @Override
102     public void stop(final BundleContext context) throws Exception {
103         if (netconfNotificationCollectorServiceRegistration != null) {
104             netconfNotificationCollectorServiceRegistration.unregister();
105             netconfNotificationCollectorServiceRegistration = null;
106         }
107         if (netconfNotificationManager != null) {
108             netconfNotificationManager.close();
109         }
110         if (operationaServiceRegistration != null) {
111             operationaServiceRegistration.unregister();
112             operationaServiceRegistration = null;
113         }
114     }
115
116     @VisibleForTesting
117     NetconfNotificationManager getNetconfNotificationManager() {
118         return netconfNotificationManager;
119     }
120 }