fix some sonar issues
[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.sal.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;
55
56 public class NetConfTopologyListener implements DataTreeChangeListener<Node> {
57
58     private static final Logger LOG = LoggerFactory.getLogger(NetConfTopologyListener.class);
59
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;
65
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<>();
73     }
74
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();
83         } else {
84             LOG.error("Failed to get mount point for node {}", nodeId);
85             return;
86         }
87
88         final Optional<NotificationService> notificationService =
89                 mountPoint.getService(NotificationService.class).toJavaUtil();
90         if (!notificationService.isPresent()) {
91             LOG.error("Failed to get RpcService for node {}", nodeId);
92             return;
93         }
94
95         if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
96
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);
101
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);
107
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);
112
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);
117
118             String streamName = "NETCONF";
119
120             if (streamName == null) {
121                 streamName = "OPENROADM";
122             }
123
124             final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class)
125                 .toJavaUtil();
126             if (service.isPresent()) {
127                 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
128                 if (rpcService == null) {
129                     LOG.error("Failed to get RpcService for node {}", nodeId);
130                 } else {
131                     final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
132                         new CreateSubscriptionInputBuilder();
133                     createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
134                     LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
135                     rpcService.createSubscription(createSubscriptionInputBuilder.build());
136                 }
137             } else {
138                 LOG.error("Failed to get RpcService for node {}", nodeId);
139             }
140             NodeRegistration nodeRegistration = new NodeRegistration(nodeId,
141                 accessAlarmNotificationListenerRegistration,
142                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
143                 null, accessTcaNotificationListenerRegistration);
144             registrations.put(nodeId, nodeRegistration);
145
146         } else if (openRoadmVersion.equals(StringConstants.OPENROADM_DEVICE_VERSION_2_2_1)) {
147             final org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener
148                 alarmListener = new AlarmNotificationListener221(dataBroker);
149             LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
150             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019
151                 .OrgOpenroadmAlarmListener> accessAlarmNotificationListenerRegistration =
152                 notificationService.get().registerNotificationListener(alarmListener);
153
154             final org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
155                 .OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener221();
156             LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
157             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev181019
158                 .OrgOpenroadmDeOperationsListener> accessDeOperationasNotificationListenerRegistration =
159                 notificationService.get().registerNotificationListener(deOperationsListener);
160
161             final org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceListener
162                 deviceListener = new DeviceListener221();
163             LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
164             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
165                 .OrgOpenroadmDeviceListener> accessDeviceNotificationListenerRegistration =
166                 notificationService.get().registerNotificationListener(deviceListener);
167
168             final org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019.OrgOpenroadmTcaListener
169                 tcaListener = new TcaListener221();
170             LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
171             final ListenerRegistration<org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev181019
172                 .OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
173                 notificationService.get().registerNotificationListener(tcaListener);
174
175
176             String streamName = "NETCONF";
177             if (streamName == null) {
178                 streamName = "OPENROADM";
179             }
180             final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
181             if (service.isPresent()) {
182                 final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
183                 if (rpcService == null) {
184                     LOG.error("Failed to get RpcService for node {}", nodeId);
185                 } else {
186                     final CreateSubscriptionInputBuilder createSubscriptionInputBuilder =
187                         new CreateSubscriptionInputBuilder();
188                     createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
189                     LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
190                     rpcService.createSubscription(createSubscriptionInputBuilder.build());
191                 }
192             } else {
193                 LOG.error("Failed to get RpcService for node {}", nodeId);
194             }
195             NodeRegistration22 nodeRegistration22 = new NodeRegistration22(nodeId,
196                 accessAlarmNotificationListenerRegistration,
197                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
198                 null, accessTcaNotificationListenerRegistration);
199             registrations22.put(nodeId, nodeRegistration22);
200
201         }
202
203     }
204
205     private void onDeviceDisConnected(final String nodeId) {
206         LOG.info("onDeviceDisConnected: {}", nodeId);
207         NodeRegistration nodeRegistration = this.registrations.remove(nodeId);
208         if (nodeRegistration != null) {
209             nodeRegistration.getAccessAlarmNotificationListenerRegistration().close();
210             nodeRegistration.getAccessDeOperationasNotificationListenerRegistration().close();
211             nodeRegistration.getAccessDeviceNotificationListenerRegistration().close();
212             nodeRegistration.getAccessTcaNotificationListenerRegistration().close();
213         }
214     }
215
216     @Override
217     @SuppressWarnings("checkstyle:FallThrough")
218     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
219         LOG.info("onDataTreeChanged");
220         for (DataTreeModification<Node> change : changes) {
221             DataObjectModification<Node> rootNode = change.getRootNode();
222             if ((rootNode.getDataAfter() == null) && (rootNode.getModificationType() != ModificationType.DELETE)) {
223                 LOG.error("rootNode.getDataAfter is null : Node not connected via Netconf protocol");
224                 continue;
225             }
226             if (rootNode.getModificationType() == ModificationType.DELETE) {
227                 if (rootNode.getDataBefore() != null) {
228                     String nodeId = rootNode.getDataBefore().key().getNodeId().getValue();
229                     LOG.info("Node {} deleted", nodeId);
230                     this.networkModelService.deleteOpenRoadmnode(nodeId);
231                     onDeviceDisConnected(nodeId);
232                 } else {
233                     LOG.error("rootNode.getDataBefore is null !");
234                 }
235                 continue;
236             }
237             String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
238             NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
239
240             if ((netconfNode != null) && !StringConstants.DEFAULT_NETCONF_NODEID.equals(nodeId)) {
241                 switch (rootNode.getModificationType()) {
242                     case WRITE:
243                         LOG.info("Node added: {}", nodeId);
244                     case SUBTREE_MODIFIED:
245                         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
246                                 netconfNode.getConnectionStatus();
247                         try {
248                             List<AvailableCapability> deviceCapabilities = netconfNode.getAvailableCapabilities()
249                                 .getAvailableCapability().stream().filter(cp -> cp.getCapability()
250                                 .contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).collect(Collectors.toList());
251                             if (!deviceCapabilities.isEmpty()) {
252                                 Collections.sort(deviceCapabilities, (cp0, cp1) -> cp1.getCapability()
253                                     .compareTo(cp0.getCapability()));
254                                 LOG.info("OpenROADM node detected: {} {}", nodeId, connectionStatus.name());
255                                 switch (connectionStatus) {
256                                     case Connected:
257                                         this.networkModelService.createOpenRoadmNode(nodeId, deviceCapabilities.get(0)
258                                             .getCapability());
259                                         onDeviceConnected(nodeId,deviceCapabilities.get(0).getCapability());
260                                         break;
261                                     case Connecting:
262                                     case UnableToConnect:
263                                         this.networkModelService.setOpenRoadmNodeStatus(nodeId, connectionStatus);
264                                         onDeviceDisConnected(nodeId);
265                                         break;
266                                     default:
267                                         LOG.warn("Unsupported device state {}", connectionStatus.getName());
268                                         break;
269                                 }
270                             }
271
272                         } catch (NullPointerException e) {
273                             LOG.error("Cannot get available Capabilities");
274                         }
275                         break;
276                     default:
277                         LOG.warn("Unexpected connection status : {}", rootNode.getModificationType());
278                         break;
279                 }
280             }
281         }
282     }
283
284
285     /*private String getSupportedStream(String nodeId) {
286         InstanceIdentifier<Streams> streamsIID = InstanceIdentifier.create(Netconf.class).child(Streams.class);
287         try {
288             Optional<Streams> ordmInfoObject =
289                     this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
290                             streamsIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
291             if (!ordmInfoObject.isPresent()) {
292                 LOG.error("Get Stream RPC is not supported");
293                 return "NETCONF";
294             }
295             for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf
296                         .streams.Stream strm : ordmInfoObject.get().getStream()) {
297
298                 if ("OPENROADM".equalsIgnoreCase(strm.getName().getValue())) {
299                     return strm.getName().getValue().toUpperCase();
300                 }
301             }
302             return "NETCONF";
303         } catch (NullPointerException ex) {
304             LOG.error("NullPointerException thrown while getting Info from a non Open ROADM device {}", nodeId);
305             return "NETCONF";
306         }
307     }*/
308 }