64442f743d576a55f39146ed80570443c450b51b
[controller.git] / opendaylight / netconf / netconf-notifications-impl / src / main / java / org / opendaylight / controller / 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.controller.netconf.notifications.impl.osgi;
10
11 import com.google.common.collect.Sets;
12 import java.util.Collections;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import java.util.Set;
16 import org.opendaylight.controller.netconf.api.Capability;
17 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
18 import org.opendaylight.controller.netconf.api.util.NetconfConstants;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
22 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
23 import org.opendaylight.controller.netconf.notifications.NetconfNotificationCollector;
24 import org.opendaylight.controller.netconf.notifications.impl.NetconfNotificationManager;
25 import org.opendaylight.controller.netconf.notifications.impl.ops.CreateSubscription;
26 import org.opendaylight.controller.netconf.notifications.impl.ops.Get;
27 import org.opendaylight.controller.netconf.util.capability.BasicCapability;
28 import org.osgi.framework.BundleActivator;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceRegistration;
31
32 public class Activator implements BundleActivator {
33
34     private ServiceRegistration<NetconfNotificationCollector> netconfNotificationCollectorServiceRegistration;
35     private ServiceRegistration<NetconfOperationServiceFactory> operationaServiceRegistration;
36     private NetconfNotificationManager netconfNotificationManager;
37
38     @Override
39     public void start(final BundleContext context) throws Exception {
40         netconfNotificationManager = new NetconfNotificationManager();
41         netconfNotificationCollectorServiceRegistration = context.registerService(NetconfNotificationCollector.class, netconfNotificationManager, new Hashtable<String, Object>());
42
43         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
44
45             private final Set<Capability> capabilities = Collections.<Capability>singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
46
47             @Override
48             public Set<Capability> getCapabilities() {
49                 return capabilities;
50             }
51
52             @Override
53             public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
54                 listener.onCapabilitiesAdded(capabilities);
55                 return new AutoCloseable() {
56                     @Override
57                     public void close() {
58                         listener.onCapabilitiesRemoved(capabilities);
59                     }
60                 };
61             }
62
63             @Override
64             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
65                 return new NetconfOperationService() {
66
67                     private final CreateSubscription createSubscription = new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
68
69
70                     @Override
71                     public Set<NetconfOperation> getNetconfOperations() {
72                         return Sets.<NetconfOperation>newHashSet(
73                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
74                                 createSubscription);
75                     }
76
77                     @Override
78                     public void close() {
79                         createSubscription.close();
80                     }
81                 };
82             }
83         };
84
85         final Dictionary<String, String> properties = new Hashtable<>();
86         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
87         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
88
89     }
90
91     @Override
92     public void stop(final BundleContext context) throws Exception {
93         if(netconfNotificationCollectorServiceRegistration != null) {
94             netconfNotificationCollectorServiceRegistration.unregister();
95             netconfNotificationCollectorServiceRegistration = null;
96         }
97         if (netconfNotificationManager != null) {
98             netconfNotificationManager.close();
99         }
100         if (operationaServiceRegistration != null) {
101             operationaServiceRegistration.unregister();
102             operationaServiceRegistration = null;
103         }
104     }
105 }