explicit switch/case fallthroughs with a comment
[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.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;
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);
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             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);
129                 } else {
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());
135                 }
136             } else {
137                 LOG.error("Failed to get RpcService for node {}", nodeId);
138             }
139             NodeRegistration nodeRegistration = new NodeRegistration(nodeId,
140                 accessAlarmNotificationListenerRegistration,
141                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
142                 null, accessTcaNotificationListenerRegistration);
143             registrations.put(nodeId, nodeRegistration);
144
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);
152
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);
159
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);
166
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);
173
174
175             String streamName = "NETCONF";
176             if (streamName == null) {
177                 streamName = "OPENROADM";
178             }
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);
184                 } else {
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());
190                 }
191             } else {
192                 LOG.error("Failed to get RpcService for node {}", nodeId);
193             }
194             NodeRegistration22 nodeRegistration22 = new NodeRegistration22(nodeId,
195                 accessAlarmNotificationListenerRegistration,
196                 accessDeOperationasNotificationListenerRegistration, accessDeviceNotificationListenerRegistration,
197                 null, accessTcaNotificationListenerRegistration);
198             registrations22.put(nodeId, nodeRegistration22);
199
200         }
201
202     }
203
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();
212         }
213     }
214
215     @Override
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");
222                 continue;
223             }
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);
230                 } else {
231                     LOG.error("rootNode.getDataBefore is null !");
232                 }
233                 continue;
234             }
235             String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
236             NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
237
238             if ((netconfNode != null) && !StringConstants.DEFAULT_NETCONF_NODEID.equals(nodeId)) {
239                 switch (rootNode.getModificationType()) {
240                     case WRITE:
241                         LOG.info("Node added: {}", nodeId);
242                     //fallthrough
243                     case SUBTREE_MODIFIED:
244                         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
245                                 netconfNode.getConnectionStatus();
246                         try {
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) {
255                                     case Connected:
256                                         this.networkModelService.createOpenRoadmNode(nodeId, deviceCapabilities.get(0)
257                                             .getCapability());
258                                         onDeviceConnected(nodeId,deviceCapabilities.get(0).getCapability());
259                                         break;
260                                     case Connecting:
261                                     case UnableToConnect:
262                                         this.networkModelService.setOpenRoadmNodeStatus(nodeId, connectionStatus);
263                                         onDeviceDisConnected(nodeId);
264                                         break;
265                                     default:
266                                         LOG.warn("Unsupported device state {}", connectionStatus.getName());
267                                         break;
268                                 }
269                             }
270
271                         } catch (NullPointerException e) {
272                             LOG.error("Cannot get available Capabilities");
273                         }
274                         break;
275                     default:
276                         LOG.warn("Unexpected connection status : {}", rootNode.getModificationType());
277                         break;
278                 }
279             }
280         }
281     }
282
283
284     /*private String getSupportedStream(String nodeId) {
285         InstanceIdentifier<Streams> streamsIID = InstanceIdentifier.create(Netconf.class).child(Streams.class);
286         try {
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");
292                 return "NETCONF";
293             }
294             for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf
295                         .streams.Stream strm : ordmInfoObject.get().getStream()) {
296
297                 if ("OPENROADM".equalsIgnoreCase(strm.getName().getValue())) {
298                     return strm.getName().getValue().toUpperCase();
299                 }
300             }
301             return "NETCONF";
302         } catch (NullPointerException ex) {
303             LOG.error("NullPointerException thrown while getting Info from a non Open ROADM device {}", nodeId);
304             return "NETCONF";
305         }
306     }*/
307 }