2 * Copyright © 2016 Orange 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.networkmodel;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.stream.Collectors;
18 import javax.annotation.Nonnull;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.binding.api.MountPoint;
26 import org.opendaylight.mdsal.binding.api.NotificationService;
27 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
28 import org.opendaylight.transportpce.common.StringConstants;
29 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
30 import org.opendaylight.transportpce.networkmodel.dto.NodeRegistration;
31 import org.opendaylight.transportpce.networkmodel.dto.NodeRegistration22;
32 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener;
33 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener221;
34 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener;
35 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener221;
36 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener;
37 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener221;
38 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener;
39 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener221;
40 import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
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.tca.rev161014.OrgOpenroadmTcaListener;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
58 private static final Logger LOG = LoggerFactory.getLogger(NetConfTopologyListener.class);
60 private final NetworkModelService networkModelService;
61 private final DataBroker dataBroker;
62 private final DeviceTransactionManager deviceTransactionManager;
63 private final Map<String, NodeRegistration> registrations;
64 private final Map<String, NodeRegistration22> registrations22;
66 public NetConfTopologyListener(final NetworkModelService networkModelService, final DataBroker dataBroker,
67 DeviceTransactionManager deviceTransactionManager) {
68 this.networkModelService = networkModelService;
69 this.dataBroker = dataBroker;
70 this.deviceTransactionManager = deviceTransactionManager;
71 this.registrations = new ConcurrentHashMap<>();
72 this.registrations22 = new ConcurrentHashMap<>();
75 private void onDeviceConnected(final String nodeId, String openRoadmVersion) {
76 LOG.info("onDeviceConnected: {}", nodeId);
77 LOG.info(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1);
78 LOG.info(openRoadmVersion);
79 Optional<MountPoint> mountPointOpt = this.deviceTransactionManager.getDeviceMountPoint(nodeId);
80 MountPoint mountPoint;
81 if (mountPointOpt.isPresent()) {
82 mountPoint = mountPointOpt.get();
84 LOG.error("Failed to get mount point for node {}", nodeId);
88 final Optional<NotificationService> notificationService =
89 mountPoint.getService(NotificationService.class);
90 if (!notificationService.isPresent()) {
91 LOG.error("Failed to get RpcService for node {}", nodeId);
95 if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
97 final OrgOpenroadmAlarmListener alarmListener = new AlarmNotificationListener(this.dataBroker);
98 LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
99 final ListenerRegistration<OrgOpenroadmAlarmListener> accessAlarmNotificationListenerRegistration =
100 notificationService.get().registerNotificationListener(alarmListener);
102 final OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener();
103 LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
104 final ListenerRegistration<OrgOpenroadmDeOperationsListener>
105 accessDeOperationasNotificationListenerRegistration =
106 notificationService.get().registerNotificationListener(deOperationsListener);
108 final OrgOpenroadmDeviceListener deviceListener = new DeviceListener();
109 LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
110 final ListenerRegistration<OrgOpenroadmDeviceListener> accessDeviceNotificationListenerRegistration =
111 notificationService.get().registerNotificationListener(deviceListener);
113 TcaListener tcaListener = new TcaListener();
114 LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
115 final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
116 notificationService.get().registerNotificationListener(tcaListener);
118 String streamName = "NETCONF";
120 if (streamName == null) {
121 streamName = "OPENROADM";
124 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
125 if (service.isPresent()) {
126 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
127 if (rpcService == null) {
128 LOG.error("Failed to get RpcService for node {}", nodeId);
130 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
131 new CreateSubscriptionInputBuilder();
132 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
133 LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
134 rpcService.createSubscription(createSubscriptionInputBuilder.build());
137 LOG.error("Failed to get RpcService for node {}", nodeId);
139 NodeRegistration nodeRegistration = new NodeRegistration(nodeId,
140 accessAlarmNotificationListenerRegistration,
141 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
142 null, accessTcaNotificationListenerRegistration);
143 registrations.put(nodeId, nodeRegistration);
145 } else if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_2_2_1)) {
146 final org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener
147 alarmListener = new AlarmNotificationListener221(dataBroker);
148 LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
149 final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019
150 .OrgOpenroadmAlarmListener> accessAlarmNotificationListenerRegistration =
151 notificationService.get().registerNotificationListener(alarmListener);
153 final org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
154 .OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener221();
155 LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
156 final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
157 .OrgOpenroadmDeOperationsListener> accessDeOperationasNotificationListenerRegistration =
158 notificationService.get().registerNotificationListener(deOperationsListener);
160 final org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceListener
161 deviceListener = new DeviceListener221();
162 LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
163 final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
164 .OrgOpenroadmDeviceListener> accessDeviceNotificationListenerRegistration =
165 notificationService.get().registerNotificationListener(deviceListener);
167 final org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019.OrgOpenroadmTcaListener
168 tcaListener = new TcaListener221();
169 LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
170 final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019
171 .OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
172 notificationService.get().registerNotificationListener(tcaListener);
175 String streamName = "NETCONF";
176 if (streamName == null) {
177 streamName = "OPENROADM";
179 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
180 if (service.isPresent()) {
181 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
182 if (rpcService == null) {
183 LOG.error("Failed to get RpcService for node {}", nodeId);
185 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
186 new CreateSubscriptionInputBuilder();
187 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
188 LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
189 rpcService.createSubscription(createSubscriptionInputBuilder.build());
192 LOG.error("Failed to get RpcService for node {}", nodeId);
194 NodeRegistration22 nodeRegistration22 = new NodeRegistration22(nodeId,
195 accessAlarmNotificationListenerRegistration,
196 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
197 null, accessTcaNotificationListenerRegistration);
198 registrations22.put(nodeId, nodeRegistration22);
204 private void onDeviceDisConnected(final String nodeId) {
205 LOG.info("onDeviceDisConnected: {}", nodeId);
206 NodeRegistration nodeRegistration = this.registrations.remove(nodeId);
207 if (nodeRegistration != null) {
208 nodeRegistration.getAccessAlarmNotificationListenerRegistration().close();
209 nodeRegistration.getAccessDeOperationasNotificationListenerRegistration().close();
210 nodeRegistration.getAccessDeviceNotificationListenerRegistration().close();
211 nodeRegistration.getAccessTcaNotificationListenerRegistration().close();
216 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
217 LOG.info("onDataTreeChanged");
218 for (DataTreeModification<Node> change : changes) {
219 DataObjectModification<Node> rootNode = change.getRootNode();
220 if ((rootNode.getDataAfter() == null) && (rootNode.getModificationType() != ModificationType.DELETE)) {
221 LOG.error("rootNode.getDataAfter is null : Node not connected via Netconf protocol");
224 if (rootNode.getModificationType() == ModificationType.DELETE) {
225 if (rootNode.getDataBefore() != null) {
226 String nodeId = rootNode.getDataBefore().key().getNodeId().getValue();
227 LOG.info("Node {} deleted", nodeId);
228 this.networkModelService.deleteOpenRoadmnode(nodeId);
229 onDeviceDisConnected(nodeId);
231 LOG.error("rootNode.getDataBefore is null !");
235 String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
236 NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
238 if ((netconfNode != null) && !StringConstants.DEFAULT_NETCONF_NODEID.equals(nodeId)) {
239 switch (rootNode.getModificationType()) {
241 LOG.info("Node added: {}", nodeId);
243 case SUBTREE_MODIFIED:
244 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
245 netconfNode.getConnectionStatus();
247 List<AvailableCapability> deviceCapabilities = netconfNode.getAvailableCapabilities()
248 .getAvailableCapability().stream().filter(cp -> cp.getCapability()
249 .contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).collect(Collectors.toList());
250 if (!deviceCapabilities.isEmpty()) {
251 Collections.sort(deviceCapabilities, (cp0, cp1) -> cp1.getCapability()
252 .compareTo(cp0.getCapability()));
253 LOG.info("OpenROADM node detected: {} {}", nodeId, connectionStatus.name());
254 switch (connectionStatus) {
256 this.networkModelService.createOpenRoadmNode(nodeId, deviceCapabilities.get(0)
258 onDeviceConnected(nodeId,deviceCapabilities.get(0).getCapability());
261 case UnableToConnect:
262 this.networkModelService.setOpenRoadmNodeStatus(nodeId, connectionStatus);
263 onDeviceDisConnected(nodeId);
266 LOG.warn("Unsupported device state {}", connectionStatus.getName());
271 } catch (NullPointerException e) {
272 LOG.error("Cannot get available Capabilities");
276 LOG.warn("Unexpected connection status : {}", rootNode.getModificationType());
284 /*private String getSupportedStream(String nodeId) {
285 InstanceIdentifier<Streams> streamsIID = InstanceIdentifier.create(Netconf.class).child(Streams.class);
287 Optional<Streams> ordmInfoObject =
288 this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
289 streamsIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
290 if (!ordmInfoObject.isPresent()) {
291 LOG.error("Get Stream RPC is not supported");
294 for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf
295 .streams.Stream strm : ordmInfoObject.get().getStream()) {
297 if ("OPENROADM".equalsIgnoreCase(strm.getName().getValue())) {
298 return strm.getName().getValue().toUpperCase();
302 } catch (NullPointerException ex) {
303 LOG.error("NullPointerException thrown while getting Info from a non Open ROADM device {}", nodeId);