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