2 * Copyright © 2017 AT&T 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
8 package org.opendaylight.transportpce.renderer;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.stream.Collectors;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
30 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
31 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
34 import org.opendaylight.transportpce.renderer.listeners.AlarmNotificationListener;
35 import org.opendaylight.transportpce.renderer.listeners.DeOperationsListener;
36 import org.opendaylight.transportpce.renderer.listeners.DeviceListener;
37 import org.opendaylight.transportpce.renderer.listeners.LldpListener;
38 import org.opendaylight.transportpce.renderer.listeners.TcaListener;
39 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.AlarmNotification;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.OrgOpenroadmDeOperationsListener;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.OrgOpenroadmLldpListener;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev161014.OrgOpenroadmTcaListener;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
69 private final DataBroker dataBroker;
70 private final MountPointService mountService;
71 private static final Logger LOG = LoggerFactory.getLogger(RendererNotificationsImpl.class);
72 private ListenerRegistration<RendererNotificationsImpl> dataTreeChangeListenerRegistration;
74 private final Set<String> currentMountedDevice;
75 public static final InstanceIdentifier<Topology> NETCONF_TOPO_IID = InstanceIdentifier.create(NetworkTopology.class)
76 .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
78 LoadingCache<String, KeyedInstanceIdentifier<Node, NodeKey>> mountIds = CacheBuilder.newBuilder().maximumSize(20)
79 .build(new CacheLoader<String, KeyedInstanceIdentifier<Node, NodeKey>>() {
81 public KeyedInstanceIdentifier<Node, NodeKey> load(final String key) {
82 return NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(key)));
86 public RendererNotificationsImpl(final DataBroker dataBroker, final MountPointService mountService,
87 Set<String> currentMountedDevice) {
88 this.dataBroker = dataBroker;
89 this.mountService = mountService;
90 this.currentMountedDevice = currentMountedDevice;
91 if (mountService == null) {
92 LOG.error("Mount service is null");
95 if (dataBroker != null) {
96 this.dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
97 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NETCONF_TOPO_IID.child(Node.class)), this);
101 private void registerNotificationListener(final NodeId nodeId) {
102 final Optional<MountPoint> mountPoint;
104 // Get mount point for specified device
105 mountPoint = mountService.getMountPoint(mountIds.get(nodeId.getValue()));
106 if (!mountPoint.isPresent()) {
107 LOG.error("Mount point for node {} doesn't exist", nodeId.getValue());
109 } catch (ExecutionException e) {
110 throw new IllegalArgumentException(e);
113 // Register notification service
114 final Optional<NotificationService> notificationService = mountPoint.get().getService(
115 NotificationService.class);
116 if (!notificationService.isPresent()) {
117 LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
120 final OrgOpenroadmAlarmListener alarmListener;
121 alarmListener = new AlarmNotificationListener();
122 LOG.info("Registering notification listener on {} for node: {}", AlarmNotification.QNAME, nodeId);
123 // Register notification listener
124 final ListenerRegistration<OrgOpenroadmAlarmListener>
125 accessAlarmNotificationListenerRegistration =
126 notificationService.get().registerNotificationListener(alarmListener);
128 final OrgOpenroadmDeOperationsListener deOperationsListener;
129 deOperationsListener = new DeOperationsListener();
130 LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
131 // Register notification listener
132 final ListenerRegistration<OrgOpenroadmDeOperationsListener>
133 accessDeOperationasNotificationListenerRegistration =
134 notificationService.get().registerNotificationListener(deOperationsListener);
136 final OrgOpenroadmDeviceListener deviceListener;
137 deviceListener = new DeviceListener();
138 LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
139 // Register notification listener
140 final ListenerRegistration<OrgOpenroadmDeviceListener>
141 accessDeviceNotificationListenerRegistration = notificationService.get()
142 .registerNotificationListener(deviceListener);
144 final OrgOpenroadmLldpListener lldpListener;
145 lldpListener = new LldpListener();
146 LOG.info("Registering notification listener on OrgOpenroadmLldpListener for node: {}", nodeId);
147 // Register notification listener
148 final ListenerRegistration<OrgOpenroadmLldpListener> accessLldpNotificationListenerRegistration =
149 notificationService.get().registerNotificationListener(lldpListener);
151 final OrgOpenroadmTcaListener tcaListener;
152 tcaListener = new TcaListener();
153 LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
154 // Register notification listener
155 final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
156 notificationService.get().registerNotificationListener(tcaListener);
158 // Listening to NETCONF datastream
159 final String streamName = "NETCONF";
160 final Optional<RpcConsumerRegistry> service = mountPoint.get().getService(RpcConsumerRegistry.class);
161 if (!service.isPresent()) {
162 LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
164 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
165 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
166 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
167 LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
168 final Future<RpcResult<Void>> subscription = rpcService.createSubscription(createSubscriptionInputBuilder
172 public void close() {
173 LOG.info("RenderernotificationsImpl Closed");
174 // Clean up the Data Change Listener registration
175 if (dataTreeChangeListenerRegistration != null) {
176 dataTreeChangeListenerRegistration.close();
181 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
183 for (DataTreeModification<Node> change : changes) {
185 DataObjectModification<Node> rootNode = change.getRootNode();
186 String nodeId = rootNode.getDataAfter().getKey().getNodeId().getValue();
187 NetconfNode nnode = Preconditions.checkNotNull(rootNode.getDataAfter().getAugmentation(NetconfNode.class),
188 "Node not connected via Netconf protocol");
191 if (rootNode.getModificationType() == ModificationType.WRITE) {
192 LOG.info("Node added " + nodeId);
194 } else if (rootNode.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
196 LOG.info("Node modified " + nodeId);
197 ConnectionStatus csts = nnode.getConnectionStatus();
201 LOG.info("NETCONF Node: {} is fully connected", nodeId);
202 List<String> capabilities = nnode.getAvailableCapabilities().getAvailableCapability()
203 .stream().map(cp -> cp.getCapability()).collect(Collectors.toList());
204 LOG.info("Capabilities: {}", capabilities);
206 * TODO: check for required
207 * capabilities to listen for notifications
208 * registerNotificationListener(rootNode.
209 * getDataAfter(). getNodeId());
211 currentMountedDevice.add(nodeId);
212 new PortMapping(dataBroker, mountService, nodeId).createMappingData();
216 LOG.info("NETCONF Node: {} was disconnected", nodeId);
219 case UnableToConnect: {
220 LOG.info("NETCONF Node: {} connection failed", nodeId);
224 LOG.warn("Unexpected connection status " + csts.getName());
226 } else if (rootNode.getModificationType() == ModificationType.DELETE) {
227 LOG.info("Node removed " + nodeId);
228 currentMountedDevice.remove(nodeId);