79eaa3e689cf67f4b29a546cbc5bc70b92a8bfed
[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.rev220922.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.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
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 DeviceListener221 implements OrgOpenroadmDeviceListener {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener221.class);
30     private final String nodeId;
31     private final PortMapping portMapping;
32
33     public DeviceListener221(String nodeId, PortMapping portMapping) {
34         super();
35         this.nodeId = nodeId;
36         this.portMapping = portMapping;
37     }
38
39     /**
40      * Callback for change-notification.
41      *
42      * @param notification
43      *            ChangeNotification object
44      */
45     @Override
46     public void onChangeNotification(ChangeNotification notification) {
47         LOG.info("notification received from device {}: {}", this.nodeId, notification.toString());
48         if (notification.getEdit() == null) {
49             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
50             return;
51         }
52         for (Edit edit : notification.getEdit()) {
53             if (edit.getTarget() == null) {
54                 continue;
55             }
56             // 1. Detect the org-openroadm-device object modified
57             switch (edit.getTarget().getTargetType().getSimpleName()) {
58                 case "Ports":
59                     LinkedList<PathArgument> path = new LinkedList<>();
60                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
61                     InstanceIdentifier<Ports> portIID = InstanceIdentifier.unsafeOf(path);
62                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
63                     path.removeLast();
64                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.unsafeOf(path);
65                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
66                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
67                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
68                     if (oldMapping == null) {
69                         return;
70                     }
71                     Runnable handleNetconfEvent = new Runnable() {
72                         @Override
73                         public void run() {
74                             portMapping.updateMapping(nodeId, oldMapping);
75                             LOG.info("{} : mapping data for {} updated", nodeId,
76                                 oldMapping.getLogicalConnectionPoint());
77                         }
78                     };
79                     Thread thread = new Thread(handleNetconfEvent);
80                     thread.start();
81                     break;
82                 case "Interface":
83                     LinkedList<PathArgument> pathInter = new LinkedList<>();
84                     edit.getTarget().getPathArguments().forEach(p -> pathInter.add(p));
85                     InstanceIdentifier<Interface> interfIID = InstanceIdentifier.unsafeOf(pathInter);
86                     String interfName = InstanceIdentifier.keyOf(interfIID).getName();
87                     LOG.info("interface {} modified on device {}", interfName, this.nodeId);
88                     Mapping oldMapping2 = portMapping.getMappingFromOtsInterface(nodeId, interfName);
89                     if (oldMapping2 == null) {
90                         return;
91                     }
92                     Runnable handleNetconfEvent2 = new Runnable() {
93                         @Override
94                         public void run() {
95                             portMapping.updateMapping(nodeId, oldMapping2);
96                             LOG.info("{} : mapping data for {} updated", nodeId,
97                                 oldMapping2.getLogicalConnectionPoint());
98                         }
99                     };
100                     Thread thread2 = new Thread(handleNetconfEvent2);
101                     thread2.start();
102                     break;
103                 default:
104                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
105                     break;
106             }
107         }
108     }
109
110     @Override
111     public void onCreateTechInfoNotification(CreateTechInfoNotification notification) {
112     }
113
114     /**
115      * Callback for otdr-scan-result.
116      *
117      * @param notification
118      *            OtdrScanResult object
119      */
120     @Override
121     public void onOtdrScanResult(OtdrScanResult notification) {
122         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
123     }
124
125 }