2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.inventory.listener;
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;
28 * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
29 * This listener should be registered on a netconf topology node.
31 public class DeviceListener implements DataTreeChangeListener<Node> {
33 private static final Logger LOG = LoggerFactory.getLogger(DeviceListener.class);
34 private final DeviceInventory deviceInventory;
37 * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
39 * @param deviceInventory reference to the {@link DeviceInventory}
41 public DeviceListener(DeviceInventory deviceInventory) {
42 this.deviceInventory = deviceInventory;
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))
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());
68 if (isCreate(device)) {
69 LOG.info("Node {} was created", nodeId);
71 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
72 } catch (InterruptedException | ExecutionException e) {
73 LOG.error(e.getMessage(), e);
75 } else if (isDelete(device)) {
76 LOG.info("Node {} was deleted", nodeId);
82 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
83 * If the changed node has.
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
89 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
91 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
92 throws InterruptedException, ExecutionException {
93 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
95 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
96 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
100 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
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);
110 LOG.warn("DL Invalid connection status {}", connectionStatus);
116 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
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
130 )).collect(Collectors.toList());
134 * In the filtered collection checks if the change is a new write.
137 private static boolean isCreate(DataTreeModification<Node> change) {
138 return change.getRootNode().getModificationType().toString().equalsIgnoreCase("WRITE");
142 * In the filtered collection checks if the modification is update.
145 private static boolean isUpdate(DataTreeModification<Node> change) {
146 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
150 * In the filtered collection checks if the node was deleted.
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());