Bump upstream dependencies to Ca
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / DeviceListener221.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.rev231221.mapping.Mapping;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.ChangeNotification;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.CreateTechInfoNotification;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OtdrScanResult;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.change.notification.Edit;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
23 import org.opendaylight.yangtools.yang.binding.DataObjectStep;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class DeviceListener221 {
29
30     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener221.class);
31     private final String nodeId;
32     private final PortMapping portMapping;
33
34     public DeviceListener221(String nodeId, PortMapping portMapping) {
35         super();
36         this.nodeId = nodeId;
37         this.portMapping = portMapping;
38     }
39
40     public CompositeListener getCompositeListener() {
41         return new CompositeListener(Set.of(
42             new CompositeListener.Component<>(ChangeNotification.class, this::onChangeNotification),
43             new CompositeListener.Component<>(CreateTechInfoNotification.class, this::onCreateTechInfoNotification),
44             new CompositeListener.Component<>(OtdrScanResult.class, this::onOtdrScanResult)
45         ));
46     }
47
48     /**
49      * Callback for change-notification.
50      *
51      * @param notification
52      *            ChangeNotification object
53      */
54     void onChangeNotification(ChangeNotification notification) {
55         LOG.info("notification received from device {}: {}", this.nodeId, notification.toString());
56         if (notification.getEdit() == null) {
57             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
58             return;
59         }
60         for (Edit edit : notification.getEdit()) {
61             if (edit.getTarget() == null) {
62                 continue;
63             }
64             // 1. Detect the org-openroadm-device object modified
65             switch (edit.getTarget().getTargetType().getSimpleName()) {
66                 case "Ports":
67                     LinkedList<DataObjectStep<?>> path = new LinkedList<>();
68                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
69                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
70                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
71                     path.removeLast();
72                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
73                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
74                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
75                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
76                     if (oldMapping == null) {
77                         return;
78                     }
79                     Runnable handleNetconfEvent = new Runnable() {
80                         @Override
81                         public void run() {
82                             portMapping.updateMapping(nodeId, oldMapping);
83                             LOG.info("{} : mapping data for {} updated", nodeId,
84                                 oldMapping.getLogicalConnectionPoint());
85                         }
86                     };
87                     Thread thread = new Thread(handleNetconfEvent);
88                     thread.start();
89                     break;
90                 case "Interface":
91                     LinkedList<DataObjectStep<?>> pathInter = new LinkedList<>();
92                     edit.getTarget().getPathArguments().forEach(p -> pathInter.add(p));
93                     InstanceIdentifier<Interface> interfIID = InstanceIdentifier.unsafeOf(pathInter);
94                     String interfName = InstanceIdentifier.keyOf(interfIID).getName();
95                     LOG.info("interface {} modified on device {}", interfName, this.nodeId);
96                     Mapping oldMapping2 = portMapping.getMappingFromOtsInterface(nodeId, interfName);
97                     if (oldMapping2 == null) {
98                         return;
99                     }
100                     Runnable handleNetconfEvent2 = new Runnable() {
101                         @Override
102                         public void run() {
103                             portMapping.updateMapping(nodeId, oldMapping2);
104                             LOG.info("{} : mapping data for {} updated", nodeId,
105                                 oldMapping2.getLogicalConnectionPoint());
106                         }
107                     };
108                     Thread thread2 = new Thread(handleNetconfEvent2);
109                     thread2.start();
110                     break;
111                 default:
112                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
113                     break;
114             }
115         }
116     }
117
118     private void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
119     }
120
121     /**
122      * Callback for otdr-scan-result.
123      *
124      * @param notification
125      *            OtdrScanResult object
126      */
127     private void onOtdrScanResult(OtdrScanResult notification) {
128         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
129     }
130
131 }