Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[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.base.Optional;
12 import com.google.common.collect.Sets;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Dictionary;
16 import java.util.Hashtable;
17 import java.util.Set;
18 import org.opendaylight.controller.netconf.api.Capability;
19 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
20 import org.opendaylight.controller.netconf.api.util.NetconfConstants;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
22 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
24 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
25 import org.opendaylight.controller.netconf.notifications.NetconfNotificationCollector;
26 import org.opendaylight.controller.netconf.notifications.impl.NetconfNotificationManager;
27 import org.opendaylight.controller.netconf.notifications.impl.ops.CreateSubscription;
28 import org.opendaylight.controller.netconf.notifications.impl.ops.Get;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceRegistration;
32
33 public class Activator implements BundleActivator {
34
35     private ServiceRegistration<NetconfNotificationCollector> netconfNotificationCollectorServiceRegistration;
36     private ServiceRegistration<NetconfOperationServiceFactory> operationaServiceRegistration;
37     private NetconfNotificationManager netconfNotificationManager;
38
39     @Override
40     public void start(final BundleContext context) throws Exception {
41         netconfNotificationManager = new NetconfNotificationManager();
42         netconfNotificationCollectorServiceRegistration = context.registerService(NetconfNotificationCollector.class, netconfNotificationManager, new Hashtable<String, Object>());
43
44         final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
45
46             private final Set<Capability> capabilities = Collections.<Capability>singleton(new NotificationsCapability());
47
48             @Override
49             public Set<Capability> getCapabilities() {
50                 return capabilities;
51             }
52
53             @Override
54             public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
55                 listener.onCapabilitiesAdded(capabilities);
56                 return new AutoCloseable() {
57                     @Override
58                     public void close() {
59                         listener.onCapabilitiesRemoved(capabilities);
60                     }
61                 };
62             }
63
64             @Override
65             public NetconfOperationService createService(final String netconfSessionIdForReporting) {
66                 return new NetconfOperationService() {
67
68                     private final CreateSubscription createSubscription = new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
69
70
71                     @Override
72                     public Set<NetconfOperation> getNetconfOperations() {
73                         return Sets.<NetconfOperation>newHashSet(
74                                 new Get(netconfSessionIdForReporting, netconfNotificationManager),
75                                 createSubscription);
76                     }
77
78                     @Override
79                     public void close() {
80                         createSubscription.close();
81                     }
82                 };
83             }
84         };
85
86         final Dictionary<String, String> properties = new Hashtable<>();
87         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
88         operationaServiceRegistration = context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
89
90     }
91
92     @Override
93     public void stop(final BundleContext context) throws Exception {
94         if(netconfNotificationCollectorServiceRegistration != null) {
95             netconfNotificationCollectorServiceRegistration.unregister();
96             netconfNotificationCollectorServiceRegistration = null;
97         }
98         if (netconfNotificationManager != null) {
99             netconfNotificationManager.close();
100         }
101         if (operationaServiceRegistration != null) {
102             operationaServiceRegistration.unregister();
103             operationaServiceRegistration = null;
104         }
105     }
106
107     private class NotificationsCapability implements Capability {
108         @Override
109         public String getCapabilityUri() {
110             return NetconfNotification.NOTIFICATION_NAMESPACE;
111         }
112
113         @Override
114         public Optional<String> getModuleNamespace() {
115             return Optional.absent();
116         }
117
118         @Override
119         public Optional<String> getModuleName() {
120             return Optional.absent();
121         }
122
123         @Override
124         public Optional<String> getRevision() {
125             return Optional.absent();
126         }
127
128         @Override
129         public Optional<String> getCapabilitySchema() {
130             return Optional.absent();
131         }
132
133         @Override
134         public Collection<String> getLocation() {
135             return Collections.emptyList();
136         }
137     }
138 }