Update portmapping YANG model
[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.Collection;
12 import java.util.LinkedList;
13 import org.opendaylight.transportpce.common.mapping.PortMapping;
14 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.ChangeNotification;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.CreateTechInfoNotification;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OrgOpenroadmDeviceListener;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OtdrScanResult;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.Edit;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class DeviceListener710 implements OrgOpenroadmDeviceListener {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener710.class);
30     private final String nodeId;
31     private final PortMapping portMapping;
32
33     public DeviceListener710(String nodeId, PortMapping portMapping) {
34         this.nodeId = nodeId;
35         this.portMapping = portMapping;
36     }
37
38     /**
39      * Callback for change-notification.
40      *
41      * @param notification
42      *            ChangeNotification object
43      */
44     @Override
45     public void onChangeNotification(ChangeNotification notification) {
46         if (notification.getEdit() == null) {
47             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
48             return;
49         }
50         for (Edit edit : notification.getEdit()) {
51             // 1. Detect the org-openroadm-device object modified
52             switch (edit.getTarget().getTargetType().getSimpleName()) {
53                 case "Ports":
54                     LinkedList<PathArgument> path = new LinkedList<>();
55                     path.addAll((Collection<? extends PathArgument>) edit.getTarget().getPathArguments());
56                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
57                         .create(path);
58                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
59                     path.removeLast();
60                     InstanceIdentifier<CircuitPacks> cpIID = (InstanceIdentifier<CircuitPacks>) InstanceIdentifier
61                         .create(path);
62                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
63                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
64                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
65                     if (oldMapping == null) {
66                         return;
67                     }
68                     Runnable handleNetconfEvent = new Runnable() {
69                         @Override
70                         public void run() {
71                             portMapping.updateMapping(nodeId, oldMapping);
72                             LOG.info("{} : mapping data for {} updated", nodeId,
73                                 oldMapping.getLogicalConnectionPoint());
74                         }
75                     };
76                     Thread thread = new Thread(handleNetconfEvent);
77                     thread.start();
78                     break;
79                 default:
80                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
81                     break;
82             }
83         }
84     }
85
86     @Override
87     public void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
88     }
89
90     /**
91      * Callback for otdr-scan-result.
92      *
93      * @param notification
94      *            OtdrScanResult object
95      */
96     @Override
97     public void onOtdrScanResult(OtdrScanResult notification) {
98         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
99     }
100
101 }