Merge "Fix bug when creating SRG termination points"
[transportpce.git] / inventory / src / main / java / org / opendaylight / transportpce / inventory / listener / DeviceListener.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.inventory.listener;
10
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import java.util.stream.Collectors;
15 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
16 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.transportpce.common.StringConstants;
19 import org.opendaylight.transportpce.inventory.DeviceInventory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
29  * This listener should be registered on a netconf topology node.
30  */
31 public class DeviceListener implements DataTreeChangeListener<Node> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(DeviceListener.class);
34     private final DeviceInventory deviceInventory;
35
36     /**
37      * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
38      *
39      * @param deviceInventory reference to the {@link DeviceInventory}
40      */
41     public DeviceListener(DeviceInventory deviceInventory) {
42         this.deviceInventory = deviceInventory;
43     }
44
45     @Override
46     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
47         //LOG.debug("testing np1: {}", changes.toString());
48         String openROADMversion = "";
49         List<DataTreeModification<Node>> changesWithoutDefaultNetconfNode = getRealDevicesOnly(changes);
50         for (DataTreeModification<Node> device : changesWithoutDefaultNetconfNode) {
51             String nodeId = device.getRootNode().getDataAfter().key().getNodeId().getValue();
52             NetconfNode netconfNode = device.getRootNode().getDataAfter().augmentation(NetconfNode.class);
53             NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
54                     netconfNode.getConnectionStatus();
55             long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
56                     .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
57                     .count();
58             LOG.debug("DL Modification Type {}", device.getRootNode().getModificationType().toString());
59             LOG.debug("DL Capability Count {}", count);
60             LOG.debug("DL Connection Status {}", connectionStatus);
61             LOG.debug("DL device.getRootNode().getDataBefore() {}", device.getRootNode().getDataBefore());
62             LOG.debug("DL device.getRootNode().getDataAfter() {}", device.getRootNode().getDataAfter());
63
64             if (isCreate(device)) {
65                 LOG.info("Node {} was created", nodeId);
66                 try {
67                     processModifiedSubtree(nodeId, netconfNode, openROADMversion);
68                 } catch (InterruptedException | ExecutionException e) {
69                     LOG.error("something wrong when creating node {}", nodeId, e);
70                 }
71             } else if (isDelete(device)) {
72                 LOG.info("Node {} was deleted", nodeId);
73             }
74         }
75     }
76
77     /**
78      * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
79      * If the changed node has.
80      *
81      * @param nodeId      device id
82      * @param netconfNode netconf node
83      * @throws InterruptedException may be thrown if there is a problem getting the device from
84      *                              datastore
85      * @throws ExecutionException   may be thrown if there is a problem getting the device from datastore
86      */
87     private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
88             throws InterruptedException, ExecutionException {
89         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
90
91         long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
92                 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
93                 .count();
94
95         if (count < 1) {
96             LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
97             return;
98         }
99         if (ConnectionStatus.Connected.equals(connectionStatus)) {
100             LOG.info("DL The device is in {} state", connectionStatus);
101             deviceInventory.initializeDevice(nodeId, openROADMversion);
102         } else if (ConnectionStatus.Connecting.equals(connectionStatus)
103                 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
104             LOG.info("DL The device is in {} state", connectionStatus);
105         } else {
106             LOG.warn("DL Invalid connection status {}", connectionStatus);
107         }
108
109     }
110
111     /**
112      * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
113      *
114      */
115     private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
116         return changes.stream()
117                 .filter(change -> (change.getRootNode().getDataAfter() != null
118                         && !StringConstants.DEFAULT_NETCONF_NODEID
119                         .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
120                         && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
121                         || (change.getRootNode().getDataBefore() != null
122                         && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
123                         change.getRootNode().getDataBefore().key().getNodeId().getValue())
124                         && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
125
126                 )).collect(Collectors.toList());
127     }
128
129     /**
130      * In the filtered collection checks if the change is a new write.
131      *
132      */
133     private static boolean isCreate(DataTreeModification<Node> change) {
134         return change.getRootNode().getModificationType().toString().equalsIgnoreCase("WRITE");
135     }
136
137     /**
138      * In the filtered collection checks if the node was deleted.
139      *
140      */
141     private static boolean isDelete(DataTreeModification<Node> change) {
142         return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
143                 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());
144     }
145 }