Fix netconf-connector-config groupId
[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.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.netconf.api.monitoring.CapabilityListener;
19 import org.opendaylight.netconf.api.util.NetconfConstants;
20 import org.opendaylight.netconf.mapping.api.NetconfOperation;
21 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
22 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
23 import org.opendaylight.netconf.notifications.NetconfNotification;
24 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
25 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
26 import org.opendaylight.netconf.notifications.impl.ops.CreateSubscription;
27 import org.opendaylight.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         // Add properties to autowire with netconf-impl instance for cfg subsystem
46         final Dictionary<String, String> props = new Hashtable<>();
47         props.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_NOTIFICATION);
48         netconfNotificationCollectorServiceRegistration = context.registerService(NetconfNotificationCollector.class, netconfNotificationManager, new Hashtable<String, Object>());
49
50         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
51
52             private final Set<Capability> capabilities = Collections.<Capability>singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
53
54             @Override
55             public Set<Capability> getCapabilities() {
56                 return capabilities;
57             }
58
59             @Override
60             public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
61                 listener.onCapabilitiesChanged(capabilities, Collections.<Capability>emptySet());
62                 return new AutoCloseable() {
63                     @Override
64                     public void close() {
65                         listener.onCapabilitiesChanged(Collections.<Capability>emptySet(), capabilities);
66                     }
67                 };
68             }
69
70             @Override
71             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
72                 return new NetconfOperationService() {
73
74                     private final CreateSubscription createSubscription = new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
75
76                     @Override
77                     public Set<NetconfOperation> getNetconfOperations() {
78                         return Sets.<NetconfOperation>newHashSet(
79                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
80                                 createSubscription);
81                     }
82
83                     @Override
84                     public void close() {
85                         createSubscription.close();
86                     }
87                 };
88             }
89         };
90
91         final Dictionary<String, String> properties = new Hashtable<>();
92         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
93         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
94     }
95
96     @Override
97     public void stop(final BundleContext context) throws Exception {
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 }