Don't use NotificationListener (NodeRegistration)
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / DeviceListener121.java
1 /*
2  * Copyright © 2017 AT&T 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
9 package org.opendaylight.transportpce.networkmodel.listeners;
10
11 import java.util.LinkedList;
12 import java.util.Set;
13 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
14 import org.opendaylight.transportpce.common.mapping.PortMapping;
15 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.Mapping;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.ChangeNotification;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OtdrScanResult;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.Edit;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class DeviceListener121 {
27
28     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener121.class);
29     private final String nodeId;
30     private final PortMapping portMapping;
31
32     public DeviceListener121(String nodeId, PortMapping portMapping) {
33         super();
34         this.nodeId = nodeId;
35         this.portMapping = portMapping;
36     }
37
38     public CompositeListener getCompositeListener() {
39         return new CompositeListener(Set.of(
40             new CompositeListener.Component<>(ChangeNotification.class, this::onChangeNotification),
41             new CompositeListener.Component<>(OtdrScanResult.class, this::onOtdrScanResult)
42         ));
43     }
44
45     /**
46      * Callback for change-notification.
47      *
48      * @param notification ChangeNotification object
49      */
50
51     void onChangeNotification(ChangeNotification notification) {
52         if (notification.getEdit() == null) {
53             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
54             return;
55         }
56         for (Edit edit : notification.getEdit()) {
57             if (edit.getTarget() == null) {
58                 continue;
59             }
60             // 1. Detect the org-openroadm-device object modified
61             switch (edit.getTarget().getTargetType().getSimpleName()) {
62                 case "Ports":
63                     LinkedList<PathArgument> path = new LinkedList<>();
64                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
65                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
66                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
67                     path.removeLast();
68                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
69                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
70                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
71                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
72                     if (oldMapping == null) {
73                         return;
74                     }
75                     Runnable handleNetconfEvent = new Runnable() {
76                         @Override
77                         public void run() {
78                             portMapping.updateMapping(nodeId, oldMapping);
79                             LOG.info("{} : mapping data for {} updated", nodeId,
80                                 oldMapping.getLogicalConnectionPoint());
81                         }
82                     };
83                     Thread thread = new Thread(handleNetconfEvent);
84                     thread.start();
85                     break;
86                 default:
87                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
88                     break;
89             }
90         }
91     }
92
93     /**
94      * Callback for otdr-scan-result.
95      *
96      * @param notification OtdrScanResult object
97      */
98     private void onOtdrScanResult(OtdrScanResult notification) {
99         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
100     }
101
102 }