fix import extra separations
[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.info("DL ################## Modification Type {}",
59                 device.getRootNode().getModificationType().toString());
60             LOG.info("DL ################## Capability Count {}", count);
61             LOG.info("DL ################## Connection Status {}", connectionStatus);
62             LOG.info("DL ################## device.getRootNode().getDataBefore() {}",
63                 device.getRootNode().getDataBefore());
64             LOG.info("DL ################## device.getRootNode().getDataAfter() {}",
65                 device.getRootNode().getDataAfter());
66
67
68             if (isCreate(device)) {
69                 LOG.info("Node {} was created", nodeId);
70                 try {
71                     processModifiedSubtree(nodeId, netconfNode, openROADMversion);
72                 } catch (InterruptedException | ExecutionException e) {
73                     LOG.error(e.getMessage(), e);
74                 }
75             } else if (isDelete(device)) {
76                 LOG.info("Node {} was deleted", nodeId);
77             }
78         }
79     }
80
81     /**
82      * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
83      * If the changed node has.
84      *
85      * @param nodeId      device id
86      * @param netconfNode netconf node
87      * @throws InterruptedException may be thrown if there is a problem getting the device from
88      *                              datastore
89      * @throws ExecutionException   may be thrown if there is a problem getting the device from datastore
90      */
91     private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
92             throws InterruptedException, ExecutionException {
93         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
94
95         long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
96                 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
97                 .count();
98
99         if (count < 1) {
100             LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
101             return;
102         }
103         if (ConnectionStatus.Connected.equals(connectionStatus)) {
104             LOG.info("DL The device is in {} state", connectionStatus);
105             deviceInventory.initializeDevice(nodeId, openROADMversion);
106         } else if (ConnectionStatus.Connecting.equals(connectionStatus)
107                 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
108             LOG.info("DL The device is in {} state", connectionStatus);
109         } else {
110             LOG.warn("DL Invalid connection status {}", connectionStatus);
111         }
112
113     }
114
115     /**
116      * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
117      *
118      */
119     private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
120         return changes.stream()
121                 .filter(change -> (change.getRootNode().getDataAfter() != null
122                         && !StringConstants.DEFAULT_NETCONF_NODEID
123                         .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
124                         && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
125                         || (change.getRootNode().getDataBefore() != null
126                         && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
127                         change.getRootNode().getDataBefore().key().getNodeId().getValue())
128                         && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
129
130                 )).collect(Collectors.toList());
131     }
132
133     /**
134      * In the filtered collection checks if the change is a new write.
135      *
136      */
137     private static boolean isCreate(DataTreeModification<Node> change) {
138         return change.getRootNode().getModificationType().toString().equalsIgnoreCase("WRITE");
139     }
140
141     /**
142      * In the filtered collection checks if the modification is update.
143      *
144      */
145     private static boolean isUpdate(DataTreeModification<Node> change) {
146         return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
147     }
148
149     /**
150      * In the filtered collection checks if the node was deleted.
151      *
152      */
153     private static boolean isDelete(DataTreeModification<Node> change) {
154         return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
155                 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());
156     }
157 }