upgrade openroadm models to 2.2.1
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / NetConfTopologyListener.java
1 /*
2  * Copyright © 2016 Orange 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 package org.opendaylight.transportpce.networkmodel;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Optional;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.stream.Collectors;
17
18 import javax.annotation.Nonnull;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
29 import org.opendaylight.transportpce.common.StringConstants;
30 import org.opendaylight.transportpce.common.Timeouts;
31 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
32 import org.opendaylight.transportpce.networkmodel.dto.NodeRegistration;
33 import org.opendaylight.transportpce.networkmodel.dto.NodeRegistration22;
34 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener;
35 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener221;
36 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener;
37 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener221;
38 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener;
39 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener221;
40 import org.opendaylight.transportpce.networkmodel.listeners.LldpListener;
41 import org.opendaylight.transportpce.networkmodel.listeners.LldpListener221;
42 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener;
43 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener221;
44 import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.OrgOpenroadmDeOperationsListener;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.OrgOpenroadmLldpListener;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev161014.OrgOpenroadmTcaListener;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
65
66     private static final Logger LOG = LoggerFactory.getLogger(NetConfTopologyListener.class);
67
68     private final NetworkModelService networkModelService;
69     private final R2RLinkDiscovery linkDiscovery;
70     private final DataBroker dataBroker;
71     private final DeviceTransactionManager deviceTransactionManager;
72     private final Map<String, NodeRegistration> registrations;
73     private final Map<String, NodeRegistration22> registrations22;
74
75     public NetConfTopologyListener(final NetworkModelService networkModelService, final DataBroker dataBroker,
76             final R2RLinkDiscovery linkDiscovery, DeviceTransactionManager deviceTransactionManager) {
77         this.networkModelService = networkModelService;
78         this.linkDiscovery = linkDiscovery;
79         this.dataBroker = dataBroker;
80         this.deviceTransactionManager = deviceTransactionManager;
81         this.registrations = new ConcurrentHashMap<>();
82         this.registrations22 = new ConcurrentHashMap<>();
83     }
84
85     private void onDeviceConnected(final String nodeId, String openRoadmVersion) {
86         LOG.info("onDeviceConnected: {}", nodeId);
87         LOG.info(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1);
88         LOG.info(openRoadmVersion);
89         Optional<MountPoint> mountPointOpt = this.deviceTransactionManager.getDeviceMountPoint(nodeId);
90         MountPoint mountPoint;
91         if (mountPointOpt.isPresent()) {
92             mountPoint = mountPointOpt.get();
93         } else {
94             LOG.error("Failed to get mount point for node {}", nodeId);
95             return;
96         }
97
98         final Optional<NotificationService> notificationService =
99                 mountPoint.getService(NotificationService.class).toJavaUtil();
100         if (!notificationService.isPresent()) {
101             LOG.error("Failed to get RpcService for node {}", nodeId);
102             return;
103         }
104
105         if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
106
107             final OrgOpenroadmAlarmListener alarmListener = new AlarmNotificationListener(this.dataBroker);
108             LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
109             final ListenerRegistration<OrgOpenroadmAlarmListener> accessAlarmNotificationListenerRegistration =
110                 notificationService.get().registerNotificationListener(alarmListener);
111
112             final OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener();
113             LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
114             final ListenerRegistration<OrgOpenroadmDeOperationsListener>
115                 accessDeOperationasNotificationListenerRegistration =
116                 notificationService.get().registerNotificationListener(deOperationsListener);
117
118             final OrgOpenroadmDeviceListener deviceListener = new DeviceListener();
119             LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
120             final ListenerRegistration<OrgOpenroadmDeviceListener> accessDeviceNotificationListenerRegistration =
121                 notificationService.get().registerNotificationListener(deviceListener);
122
123             final OrgOpenroadmLldpListener lldpListener = new LldpListener(this.linkDiscovery, nodeId);
124             LOG.info("Registering notification listener on OrgOpenroadmLldpListener for node: {}", nodeId);
125             final ListenerRegistration<OrgOpenroadmLldpListener> accessLldpNotificationListenerRegistration =
126                 notificationService.get().registerNotificationListener(lldpListener);
127
128             final OrgOpenroadmTcaListener tcaListener = new TcaListener();
129             LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
130             final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
131                 notificationService.get().registerNotificationListener(tcaListener);
132
133             String streamName = "NETCONF"; //getSupportedStream(nodeId);
134
135             if (streamName == null) {
136                 streamName = "OPENROADM";
137             }
138
139             final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class)
140                 .toJavaUtil();
141             if (service.isPresent()) {
142                 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
143                 if (rpcService == null) {
144                     LOG.error("Failed to get RpcService for node {}", nodeId);
145                 } else {
146                     final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
147                         new CreateSubscriptionInputBuilder();
148                     createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
149                     LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
150                     rpcService.createSubscription(createSubscriptionInputBuilder.build());
151                 }
152             } else {
153                 LOG.error("Failed to get RpcService for node {}", nodeId);
154             }
155             NodeRegistration nodeRegistration = new NodeRegistration(nodeId,
156                 accessAlarmNotificationListenerRegistration,
157                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
158                 null, accessTcaNotificationListenerRegistration);
159             registrations.put(nodeId, nodeRegistration);
160
161         } else if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_2_2_1)) {
162             final org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener
163                 alarmListener = new AlarmNotificationListener221(dataBroker);
164             LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
165             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019
166                 .OrgOpenroadmAlarmListener> accessAlarmNotificationListenerRegistration =
167                 notificationService.get().registerNotificationListener(alarmListener);
168
169             final org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
170                 .OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener221();
171             LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
172             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
173                 .OrgOpenroadmDeOperationsListener> accessDeOperationasNotificationListenerRegistration =
174                 notificationService.get().registerNotificationListener(deOperationsListener);
175
176             final org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceListener
177                 deviceListener = new DeviceListener221();
178             LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
179             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
180                 .OrgOpenroadmDeviceListener> accessDeviceNotificationListenerRegistration =
181                 notificationService.get().registerNotificationListener(deviceListener);
182
183             final org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.OrgOpenroadmLldpListener
184                 lldpListener = new LldpListener221(linkDiscovery, nodeId);
185             LOG.info("Registering notification listener on OrgOpenroadmLldpListener for node: {}", nodeId);
186             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm
187                 .lldp.rev181019.OrgOpenroadmLldpListener> accessLldpNotificationListenerRegistration =
188                 notificationService.get().registerNotificationListener(lldpListener);
189
190             final org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019.OrgOpenroadmTcaListener
191                 tcaListener = new TcaListener221();
192             LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
193             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019
194                 .OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
195                 notificationService.get().registerNotificationListener(tcaListener);
196
197
198             String streamName = "NETCONF";
199             //getSupportedStream(nodeId);
200             if (streamName == null) {
201                 streamName = "OPENROADM";
202             }
203             final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
204             if (service.isPresent()) {
205                 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
206                 if (rpcService == null) {
207                     LOG.error("Failed to get RpcService for node {}", nodeId);
208                 } else {
209                     final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
210                         new CreateSubscriptionInputBuilder();
211                     createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
212                     LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
213                     rpcService.createSubscription(createSubscriptionInputBuilder.build());
214                 }
215             } else {
216                 LOG.error("Failed to get RpcService for node {}", nodeId);
217             }
218             NodeRegistration22 nodeRegistration22 = new NodeRegistration22(nodeId,
219                 accessAlarmNotificationListenerRegistration,
220                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
221                 accessLldpNotificationListenerRegistration, accessTcaNotificationListenerRegistration);
222             registrations22.put(nodeId, nodeRegistration22);
223
224         }
225
226     }
227
228     private void onDeviceDisConnected(final String nodeId) {
229         LOG.info("onDeviceDisConnected: {}", nodeId);
230         NodeRegistration nodeRegistration = this.registrations.remove(nodeId);
231         if (nodeRegistration != null) {
232             nodeRegistration.getAccessAlarmNotificationListenerRegistration().close();
233             nodeRegistration.getAccessDeOperationasNotificationListenerRegistration().close();
234             nodeRegistration.getAccessDeviceNotificationListenerRegistration().close();
235             nodeRegistration.getAccessLldpNotificationListenerRegistration().close();
236             nodeRegistration.getAccessTcaNotificationListenerRegistration().close();
237         }
238     }
239
240     @Override
241     @SuppressWarnings("checkstyle:FallThrough")
242     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
243         LOG.info("onDataTreeChanged");
244         for (DataTreeModification<Node> change : changes) {
245             DataObjectModification<Node> rootNode = change.getRootNode();
246             if ((rootNode.getDataAfter() == null) && (rootNode.getModificationType() != ModificationType.DELETE)) {
247                 LOG.error("rootNode.getDataAfter is null : Node not connected via Netconf protocol");
248                 continue;
249             }
250             if (rootNode.getModificationType() == ModificationType.DELETE) {
251                 if (rootNode.getDataBefore() != null) {
252                     String nodeId = rootNode.getDataBefore().key().getNodeId().getValue();
253                     LOG.info("Node {} deleted", nodeId);
254                     this.networkModelService.deleteOpenROADMnode(nodeId);
255                     onDeviceDisConnected(nodeId);
256                 } else {
257                     LOG.error("rootNode.getDataBefore is null !");
258                 }
259                 continue;
260             }
261             String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
262             NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
263
264             if ((netconfNode != null) && !StringConstants.DEFAULT_NETCONF_NODEID.equals(nodeId)) {
265                 switch (rootNode.getModificationType()) {
266                     case WRITE:
267                         LOG.info("Node added: {}", nodeId);
268                     case SUBTREE_MODIFIED:
269                         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
270                                 netconfNode.getConnectionStatus();
271                         try {
272                             List<AvailableCapability> deviceCapabilities = netconfNode.getAvailableCapabilities()
273                                 .getAvailableCapability().stream().filter(cp -> cp.getCapability()
274                                 .contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).collect(Collectors.toList());
275                             if (!deviceCapabilities.isEmpty()) {
276                                 Collections.sort(deviceCapabilities, (cp0, cp1) -> cp1.getCapability()
277                                     .compareTo(cp0.getCapability()));
278                                 LOG.info("OpenROADM node detected: {} {}", nodeId, connectionStatus.name());
279                                 switch (connectionStatus) {
280                                     case Connected:
281                                         this.networkModelService.createOpenROADMnode(nodeId, deviceCapabilities.get(0)
282                                             .getCapability());
283                                         onDeviceConnected(nodeId,deviceCapabilities.get(0).getCapability());
284                                         break;
285                                     case Connecting:
286                                     case UnableToConnect:
287                                         this.networkModelService.setOpenROADMnodeStatus(nodeId, connectionStatus);
288                                         onDeviceDisConnected(nodeId);
289                                         break;
290                                     default:
291                                         LOG.warn("Unsupported device state {}", connectionStatus.getName());
292                                         break;
293                                 }
294                             }
295
296                         } catch (NullPointerException e) {
297                             LOG.error("Cannot get available Capabilities");
298                         }
299                         break;
300                     default:
301                         LOG.warn("Unexpected connection status : {}", rootNode.getModificationType());
302                         break;
303                 }
304             }
305         }
306     }
307
308
309     private String getSupportedStream(String nodeId) {
310         InstanceIdentifier<Streams> streamsIID = InstanceIdentifier.create(Netconf.class).child(Streams.class);
311         try {
312             Optional<Streams> ordmInfoObject =
313                     this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
314                             streamsIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
315             if (!ordmInfoObject.isPresent()) {
316                 LOG.error("Get Stream RPC is not supported");
317                 return "NETCONF";
318             }
319             for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf
320                         .streams.Stream strm : ordmInfoObject.get().getStream()) {
321
322                 if ("OPENROADM".equalsIgnoreCase(strm.getName().getValue())) {
323                     return strm.getName().getValue().toUpperCase();
324                 }
325             }
326             return "NETCONF";
327         } catch (NullPointerException ex) {
328             LOG.error("NullPointerException thrown while getting Info from a non Open ROADM device {}", nodeId);
329             return "NETCONF";
330         }
331     }
332 }