9ab30acbf8c711fc746e8f79ed21f3fd8f7e014d
[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.LinkedList;
12 import org.opendaylight.transportpce.common.mapping.PortMapping;
13 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.Mapping;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.ChangeNotification;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OtdrScanResult;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.Edit;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class DeviceListener121 implements OrgOpenroadmDeviceListener {
26
27     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener121.class);
28     private final String nodeId;
29     private final PortMapping portMapping;
30
31     public DeviceListener121(String nodeId, PortMapping portMapping) {
32         super();
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             if (edit.getTarget() == null) {
50                 continue;
51             }
52             // 1. Detect the org-openroadm-device object modified
53             switch (edit.getTarget().getTargetType().getSimpleName()) {
54                 case "Ports":
55                     LinkedList<PathArgument> path = new LinkedList<>();
56                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
57                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
58                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
59                     path.removeLast();
60                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
61                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
62                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
63                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
64                     if (oldMapping == null) {
65                         return;
66                     }
67                     Runnable handleNetconfEvent = new Runnable() {
68                         @Override
69                         public void run() {
70                             portMapping.updateMapping(nodeId, oldMapping);
71                             LOG.info("{} : mapping data for {} updated", nodeId,
72                                 oldMapping.getLogicalConnectionPoint());
73                         }
74                     };
75                     Thread thread = new Thread(handleNetconfEvent);
76                     thread.start();
77                     break;
78                 default:
79                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
80                     break;
81             }
82         }
83     }
84
85     /**
86      * Callback for otdr-scan-result.
87      *
88      * @param notification OtdrScanResult object
89      */
90     @Override
91     public void onOtdrScanResult(OtdrScanResult notification) {
92         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
93     }
94
95 }