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