Bump upstream dependencies to Ca
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / DeviceListener710.java
1 /*
2  * Copyright © 2021 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
9 package org.opendaylight.transportpce.networkmodel.listeners;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
18 import org.opendaylight.transportpce.common.mapping.PortMapping;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.Mapping;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.ChangeNotification;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.CreateTechInfoNotification;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OtdrScanResult;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.Edit;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.OduSwitchingPools;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.odu.switching.pools.NonBlockingList;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.odu.switching.pools.non.blocking.list.PortList;
29 import org.opendaylight.yangtools.yang.binding.DataObjectStep;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint16;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class DeviceListener710 {
36
37     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener710.class);
38     private final String nodeId;
39     private final PortMapping portMapping;
40
41     public DeviceListener710(String nodeId, PortMapping portMapping) {
42         super();
43         this.nodeId = nodeId;
44         this.portMapping = portMapping;
45     }
46
47     public CompositeListener getCompositeListener() {
48         return new CompositeListener(Set.of(
49             new CompositeListener.Component<>(ChangeNotification.class, this::onChangeNotification),
50             new CompositeListener.Component<>(CreateTechInfoNotification.class, this::onCreateTechInfoNotification),
51             new CompositeListener.Component<>(OtdrScanResult.class, this::onOtdrScanResult)
52         ));
53     }
54
55     /**
56      * Callback for change-notification.
57      *
58      * @param notification
59      *            ChangeNotification object
60      */
61     void onChangeNotification(ChangeNotification notification) {
62         LOG.debug("device71 notification received = {}", notification);
63         if (notification.getEdit() == null) {
64             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
65             return;
66         }
67         Map<Uint16, List<InstanceIdentifier<PortList>>> nbliidMap = new HashMap<>();
68         InstanceIdentifier<OduSwitchingPools> ospIID = null;
69         for (Edit edit : notification.getEdit()) {
70             if (edit.getTarget() == null) {
71                 continue;
72             }
73             // 1. Detect the org-openroadm-device object modified
74             LinkedList<DataObjectStep<?>> path = new LinkedList<>();
75             switch (edit.getTarget().getTargetType().getSimpleName()) {
76                 case "Ports":
77                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
78                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
79                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
80                     path.removeLast();
81                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
82                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
83                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
84                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
85                     if (oldMapping == null) {
86                         return;
87                     }
88                     Runnable handleNetconfEvent = new Runnable() {
89                         @Override
90                         public void run() {
91                             portMapping.updateMapping(nodeId, oldMapping);
92                             LOG.info("{} : mapping data for {} updated", nodeId,
93                                 oldMapping.getLogicalConnectionPoint());
94                         }
95                     };
96                     Thread thread = new Thread(handleNetconfEvent);
97                     thread.start();
98                     break;
99                 case "OduSwitchingPools":
100                     LOG.info("odu-switching-pools modified on device {}", nodeId);
101                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
102                     ospIID = InstanceIdentifier.unsafeOf(path);
103                     break;
104                 case "PortList":
105                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
106                     InstanceIdentifier<PortList> plIID = InstanceIdentifier.unsafeOf(path);
107                     path.removeLast();
108                     InstanceIdentifier<NonBlockingList> nblIID = InstanceIdentifier.unsafeOf(path);
109                     Uint16 nblNb = InstanceIdentifier.keyOf(nblIID).getNblNumber();
110                     List<InstanceIdentifier<PortList>> iidList = nbliidMap.containsKey(nblNb)
111                         ? nbliidMap.get(nblNb) : new ArrayList<>();
112                     iidList.add(plIID);
113                     nbliidMap.put(nblNb, iidList);
114                     break;
115                 default:
116                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
117                     break;
118             }
119         }
120         if (!nbliidMap.isEmpty() && ospIID != null) {
121             InstanceIdentifier<OduSwitchingPools> id = ospIID;
122             Runnable handleNetconfEvent = new Runnable() {
123                 @Override
124                 public void run() {
125                     portMapping.updatePortMappingWithOduSwitchingPools(nodeId, id, nbliidMap);
126                     LOG.info("{} : swiching-pool data updated", nodeId);
127                 }
128             };
129             Thread thread = new Thread(handleNetconfEvent);
130             thread.start();
131         }
132     }
133
134     private void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
135     }
136
137     /**
138      * Callback for otdr-scan-result.
139      *
140      * @param notification
141      *            OtdrScanResult object
142      */
143     private void onOtdrScanResult(OtdrScanResult notification) {
144         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
145     }
146
147 }