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.collect.Sets;
12 import java.util.Collections;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
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;
34 public class Activator implements BundleActivator {
36 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
38 private ServiceRegistration<NetconfNotificationCollector> netconfNotificationCollectorServiceRegistration;
39 private ServiceRegistration<NetconfOperationServiceFactory> operationaServiceRegistration;
40 private NetconfNotificationManager netconfNotificationManager;
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>());
50 final NetconfOperationServiceFactory netconfOperationServiceFactory = new NetconfOperationServiceFactory() {
52 private final Set<Capability> capabilities = Collections.<Capability>singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
55 public Set<Capability> getCapabilities() {
60 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
61 listener.onCapabilitiesChanged(capabilities, Collections.<Capability>emptySet());
62 return new AutoCloseable() {
65 listener.onCapabilitiesChanged(Collections.<Capability>emptySet(), capabilities);
71 public NetconfOperationService createService(final String netconfSessionIdForReporting) {
72 return new NetconfOperationService() {
74 private final CreateSubscription createSubscription = new CreateSubscription(netconfSessionIdForReporting, netconfNotificationManager);
77 public Set<NetconfOperation> getNetconfOperations() {
78 return Sets.<NetconfOperation>newHashSet(
79 new Get(netconfSessionIdForReporting, netconfNotificationManager),
85 createSubscription.close();
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);
97 public void stop(final BundleContext context) throws Exception {
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;