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