2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.notifications.impl.osgi;
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;
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;
35 public class Activator implements BundleActivator {
37 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
39 private ServiceRegistration<NetconfNotificationCollector> netconfNotificationCollectorServiceRegistration;
40 private ServiceRegistration<NetconfOperationServiceFactory> operationaServiceRegistration;
41 private NetconfNotificationManager netconfNotificationManager;
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<String, Object>());
52 final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
54 private final Set<Capability> capabilities =
55 Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
58 public Set<Capability> getCapabilities() {
63 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
64 listener.onCapabilitiesChanged(capabilities, Collections.emptySet());
65 return () -> listener.onCapabilitiesChanged(Collections.emptySet(), capabilities);
69 public NetconfOperationService createService(final String netconfSessionIdForReporting) {
70 return new NetconfOperationService() {
72 private final CreateSubscription createSubscription =
73 new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
76 public Set<NetconfOperation> getNetconfOperations() {
77 return Sets.newHashSet(
78 new Get(netconfSessionIdForReporting, netconfNotificationManager),
84 createSubscription.close();
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);
97 public void stop(final BundleContext context) {
98 if (netconfNotificationCollectorServiceRegistration != null) {
99 netconfNotificationCollectorServiceRegistration.unregister();
100 netconfNotificationCollectorServiceRegistration = null;
102 if (netconfNotificationManager != null) {
103 netconfNotificationManager.close();
105 if (operationaServiceRegistration != null) {
106 operationaServiceRegistration.unregister();
107 operationaServiceRegistration = null;
112 NetconfNotificationManager getNetconfNotificationManager() {
113 return netconfNotificationManager;