Update portmapping with switching-pool from notif
[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.rev210426.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             // 1. Detect the org-openroadm-device object modified
64             LinkedList<PathArgument> path = new LinkedList<>();
65             switch (edit.getTarget().getTargetType().getSimpleName()) {
66                 case "Ports":
67                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
68                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
69                         .create(path);
70                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
71                     path.removeLast();
72                     InstanceIdentifier<CircuitPacks> cpIID = (InstanceIdentifier<CircuitPacks>) InstanceIdentifier
73                         .create(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<OduSwitchingPools>) InstanceIdentifier.create(path);
95                     break;
96                 case "PortList":
97                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
98                     InstanceIdentifier<PortList> plIID = (InstanceIdentifier<PortList>) InstanceIdentifier.create(path);
99                     path.removeLast();
100                     InstanceIdentifier<NonBlockingList> nblIID =
101                         (InstanceIdentifier<NonBlockingList>) InstanceIdentifier.create(path);
102                     Uint16 nblNb = InstanceIdentifier.keyOf(nblIID).getNblNumber();
103                     List<InstanceIdentifier<PortList>> iidList = nbliidMap.containsKey(nblNb)
104                         ? nbliidMap.get(nblNb) : new ArrayList<>();
105                     iidList.add(plIID);
106                     nbliidMap.put(nblNb, iidList);
107                     break;
108                 default:
109                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
110                     break;
111             }
112         }
113         if (!nbliidMap.isEmpty() && ospIID != null) {
114             InstanceIdentifier<OduSwitchingPools> id = ospIID;
115             Runnable handleNetconfEvent = new Runnable() {
116                 @Override
117                 public void run() {
118                     portMapping.updatePortMappingWithOduSwitchingPools(nodeId, id, nbliidMap);
119                     LOG.info("{} : swiching-pool data updated", nodeId);
120                 }
121             };
122             Thread thread = new Thread(handleNetconfEvent);
123             thread.start();
124         }
125     }
126
127     @Override
128     public void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
129     }
130
131     /**
132      * Callback for otdr-scan-result.
133      *
134      * @param notification
135      *            OtdrScanResult object
136      */
137     @Override
138     public void onOtdrScanResult(OtdrScanResult notification) {
139         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
140     }
141
142 }