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