56a5efba77caf900302893cd898a29dc7a26b858
[transportpce.git] /
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.List;
14 import java.util.Map;
15 import java.util.Set;
16 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
17 import org.opendaylight.transportpce.common.mapping.PortMapping;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev240315.mapping.Mapping;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.ChangeNotification;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.CreateTechInfoNotification;
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.binding.DataObject;
29 import org.opendaylight.yangtools.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             InstanceIdentifier<DataObject> path = InstanceIdentifier.unsafeOf(
75                     (List<? extends DataObjectStep<?>>) edit.getTarget().steps());
76             LOG.debug("Instance Identifier received = {} from node {}", path.toString(), nodeId);
77             switch (path.lastStep().type().getSimpleName()) {
78                 case "Ports":
79                     String portName = path.firstKeyOf(Ports.class).getPortName();
80                     String cpName = path.firstKeyOf(CircuitPacks.class).getCircuitPackName();
81                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
82                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
83                     if (oldMapping == null) {
84                         return;
85                     }
86                     Runnable handleNetconfEvent = new Runnable() {
87                         @Override
88                         public void run() {
89                             portMapping.updateMapping(nodeId, oldMapping);
90                             LOG.info("{} : mapping data for {} updated", nodeId,
91                                 oldMapping.getLogicalConnectionPoint());
92                         }
93                     };
94                     Thread thread = new Thread(handleNetconfEvent);
95                     thread.start();
96                     break;
97                 case "OduSwitchingPools":
98                     LOG.info("odu-switching-pools modified on device {}", nodeId);
99                     ospIID = path.firstIdentifierOf(OduSwitchingPools.class);
100                     break;
101                 case "PortList":
102                     InstanceIdentifier<PortList> plIID = path.firstIdentifierOf(PortList.class);
103                     Uint16 nblNb = path.firstKeyOf(NonBlockingList.class).getNblNumber();
104                     List<InstanceIdentifier<PortList>> iidList = nbliidMap.containsKey(nblNb)
105                         ? nbliidMap.get(nblNb) : new ArrayList<>();
106                     iidList.add(plIID);
107                     nbliidMap.put(nblNb, iidList);
108                     break;
109                 default:
110                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getClass());
111                     break;
112             }
113         }
114         if (!nbliidMap.isEmpty() && ospIID != null) {
115             InstanceIdentifier<OduSwitchingPools> id = ospIID;
116             Runnable handleNetconfEvent = new Runnable() {
117                 @Override
118                 public void run() {
119                     portMapping.updatePortMappingWithOduSwitchingPools(nodeId, id, nbliidMap);
120                     LOG.info("{} : swiching-pool data updated", nodeId);
121                 }
122             };
123             Thread thread = new Thread(handleNetconfEvent);
124             thread.start();
125         }
126     }
127
128     private void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
129     }
130
131     /**
132      * Callback for otdr-scan-result.
133      *
134      * @param notification
135      *            OtdrScanResult object
136      */
137     private void onOtdrScanResult(OtdrScanResult notification) {
138         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
139     }
140
141 }