Refactor SupportedIfCapability usage
[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.rev220316.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     @SuppressWarnings("unchecked")
44     public void onChangeNotification(ChangeNotification notification) {
45         if (notification.getEdit() == null) {
46             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
47             return;
48         }
49         for (Edit edit : notification.getEdit()) {
50             if (edit.getTarget() == null) {
51                 continue;
52             }
53             // 1. Detect the org-openroadm-device object modified
54             switch (edit.getTarget().getTargetType().getSimpleName()) {
55                 case "Ports":
56                     LinkedList<PathArgument> path = new LinkedList<>();
57                     edit.getTarget().getPathArguments().forEach(p -> path.add(p));
58                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
59                         .create(path);
60                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
61                     path.removeLast();
62                     InstanceIdentifier<CircuitPacks> cpIID = (InstanceIdentifier<CircuitPacks>) InstanceIdentifier
63                         .create(path);
64                     String cpName = InstanceIdentifier.keyOf(cpIID).getCircuitPackName();
65                     LOG.info("port {} of circruit-pack {} modified on device {}", portName, cpName, this.nodeId);
66                     Mapping oldMapping = portMapping.getMapping(nodeId, cpName, portName);
67                     if (oldMapping == null) {
68                         return;
69                     }
70                     Runnable handleNetconfEvent = new Runnable() {
71                         @Override
72                         public void run() {
73                             portMapping.updateMapping(nodeId, oldMapping);
74                             LOG.info("{} : mapping data for {} updated", nodeId,
75                                 oldMapping.getLogicalConnectionPoint());
76                         }
77                     };
78                     Thread thread = new Thread(handleNetconfEvent);
79                     thread.start();
80                     break;
81                 default:
82                     LOG.debug("modification of type {} not managed yet", edit.getTarget().getTargetType());
83                     break;
84             }
85         }
86     }
87
88     /**
89      * Callback for otdr-scan-result.
90      *
91      * @param notification OtdrScanResult object
92      */
93     @Override
94     public void onOtdrScanResult(OtdrScanResult notification) {
95         LOG.info("Notification {} received {}", OtdrScanResult.QNAME, notification);
96     }
97
98 }