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