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