b774337f1c619994daa455c0bd7d57f75814a0a8
[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.rev220922.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     public void onChangeNotification(ChangeNotification notification) {
54         LOG.debug("device71 notification received = {}", notification);
55         if (notification.getEdit() == null) {
56             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
57             return;
58         }
59         Map<Uint16, List<InstanceIdentifier<PortList>>> nbliidMap = new HashMap<>();
60         InstanceIdentifier<OduSwitchingPools> ospIID = null;
61         for (Edit edit : notification.getEdit()) {
62             if (edit.getTarget() == null) {
63                 continue;
64             }
65             // 1. Detect the org-openroadm-device object modified
66             LinkedList<PathArgument> path = new LinkedList<>();
67             switch (edit.getTarget().getTargetType().getSimpleName()) {
68                 case "Ports":
69                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
70                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
71                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
72                     path.removeLast();
73                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
74                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
75                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
76                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
77                     if (oldMapping == null) {
78                         return;
79                     }
80                     Runnable handleNetconfEvent = new Runnable() {
81                         @Override
82                         public void run() {
83                             portMapping.updateMapping(nodeId, oldMapping);
84                             LOG.info("{} : mapping data for {} updated", nodeId,
85                                 oldMapping.getLogicalConnectionPoint());
86                         }
87                     };
88                     Thread thread = new Thread(handleNetconfEvent);
89                     thread.start();
90                     break;
91                 case "OduSwitchingPools":
92                     LOG.info("odu-switching-pools modified on device {}", nodeId);
93                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
94                     ospIID = InstanceIdentifier.unsafeOf(path);
95                     break;
96                 case "PortList":
97                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
98                     InstanceIdentifier<PortList> plIID = InstanceIdentifier.unsafeOf(path);
99                     path.removeLast();
100                     InstanceIdentifier<NonBlockingList> nblIID = InstanceIdentifier.unsafeOf(path);
101                     Uint16 nblNb = InstanceIdentifier.keyOf(nblIID).getNblNumber();
102                     List<InstanceIdentifier<PortList>> iidList = nbliidMap.containsKey(nblNb)
103                         ? nbliidMap.get(nblNb) : new ArrayList<>();
104                     iidList.add(plIID);
105                     nbliidMap.put(nblNb, iidList);
106                     break;
107                 default:
108                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
109                     break;
110             }
111         }
112         if (!nbliidMap.isEmpty() && ospIID != null) {
113             InstanceIdentifier<OduSwitchingPools> id = ospIID;
114             Runnable handleNetconfEvent = new Runnable() {
115                 @Override
116                 public void run() {
117                     portMapping.updatePortMappingWithOduSwitchingPools(nodeId, id, nbliidMap);
118                     LOG.info("{} : swiching-pool data updated", nodeId);
119                 }
120             };
121             Thread thread = new Thread(handleNetconfEvent);
122             thread.start();
123         }
124     }
125
126     @Override
127     public void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
128     }
129
130     /**
131      * Callback for otdr-scan-result.
132      *
133      * @param notification
134      *            OtdrScanResult object
135      */
136     @Override
137     public void onOtdrScanResult(OtdrScanResult notification) {
138         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
139     }
140
141 }