967bd08914e17734628387badc80fe921e770be9
[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.netconf.api.capability.BasicCapability;
18 import org.opendaylight.netconf.api.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) {
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<>());
51
52         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
53
54             private final Set<Capability> capabilities =
55                     Collections.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.emptySet());
65                 return () -> listener.onCapabilitiesChanged(Collections.emptySet(), capabilities);
66             }
67
68             @Override
69             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
70                 return new NetconfOperationService() {
71
72                     private final CreateSubscription createSubscription =
73                             new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
74
75                     @Override
76                     public Set<NetconfOperation> getNetconfOperations() {
77                         return Sets.newHashSet(
78                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
79                                 createSubscription);
80                     }
81
82                     @Override
83                     public void close() {
84                         createSubscription.close();
85                     }
86                 };
87             }
88         };
89
90         final Dictionary<String, String> properties = new Hashtable<>();
91         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
92         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class,
93                 netconfOperationServiceFactory, properties);
94     }
95
96     @Override
97     public void stop(final BundleContext context) {
98         if (netconfNotificationCollectorServiceRegistration != null) {
99             netconfNotificationCollectorServiceRegistration.unregister();
100             netconfNotificationCollectorServiceRegistration = null;
101         }
102         if (netconfNotificationManager != null) {
103             netconfNotificationManager.close();
104         }
105         if (operationaServiceRegistration != null) {
106             operationaServiceRegistration.unregister();
107             operationaServiceRegistration = null;
108         }
109     }
110
111     @VisibleForTesting
112     NetconfNotificationManager getNetconfNotificationManager() {
113         return netconfNotificationManager;
114     }
115 }