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.device.rev221225.ConnectionOper.ConnectionStatus;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
28 * This listener should be registered on a netconf topology node.
30 public class DeviceListener implements DataTreeChangeListener<Node> {
32 private static final Logger LOG = LoggerFactory.getLogger(DeviceListener.class);
33 private final DeviceInventory deviceInventory;
36 * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
38 * @param deviceInventory reference to the {@link DeviceInventory}
40 public DeviceListener(DeviceInventory deviceInventory) {
41 this.deviceInventory = deviceInventory;
45 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
46 //LOG.debug("testing np1: {}", changes.toString());
47 String openROADMversion = "";
48 List<DataTreeModification<Node>> changesWithoutDefaultNetconfNode = getRealDevicesOnly(changes);
49 for (DataTreeModification<Node> device : changesWithoutDefaultNetconfNode) {
50 String nodeId = device.getRootNode().getDataAfter().key().getNodeId().getValue();
51 NetconfNode netconfNode = device.getRootNode().getDataAfter().augmentation(NetconfNode.class);
52 ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
53 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
54 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
56 LOG.debug("DL Modification Type {}", device.getRootNode().getModificationType().toString());
57 LOG.debug("DL Capability Count {}", count);
58 LOG.debug("DL Connection Status {}", connectionStatus);
59 LOG.debug("DL device.getRootNode().getDataBefore() {}", device.getRootNode().getDataBefore());
60 LOG.debug("DL device.getRootNode().getDataAfter() {}", device.getRootNode().getDataAfter());
62 if (isCreate(device)) {
63 LOG.info("Node {} was created", nodeId);
65 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
66 } catch (InterruptedException | ExecutionException e) {
67 LOG.error("something wrong when creating node {}", nodeId, e);
69 } else if (isDelete(device)) {
70 LOG.info("Node {} was deleted", nodeId);
76 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
77 * If the changed node has.
79 * @param nodeId device id
80 * @param netconfNode netconf node
81 * @throws InterruptedException may be thrown if there is a problem getting the device from
83 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
85 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
86 throws InterruptedException, ExecutionException {
87 ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
89 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
90 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
94 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
97 if (ConnectionStatus.Connected.equals(connectionStatus)) {
98 LOG.info("DL The device is in {} state", connectionStatus);
99 deviceInventory.initializeDevice(nodeId, openROADMversion);
100 } else if (ConnectionStatus.Connecting.equals(connectionStatus)
101 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
102 LOG.info("DL The device is in {} state", connectionStatus);
104 LOG.warn("DL Invalid connection status {}", connectionStatus);
110 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
113 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
114 return changes.stream()
115 .filter(change -> (change.getRootNode().getDataAfter() != null
116 && !StringConstants.DEFAULT_NETCONF_NODEID
117 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
118 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
119 || (change.getRootNode().getDataBefore() != null
120 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
121 change.getRootNode().getDataBefore().key().getNodeId().getValue())
122 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
124 )).collect(Collectors.toList());
128 * In the filtered collection checks if the change is a new write.
131 private static boolean isCreate(DataTreeModification<Node> change) {
132 return change.getRootNode().getModificationType().toString().equalsIgnoreCase("WRITE");
136 * In the filtered collection checks if the node was deleted.
139 private static boolean isDelete(DataTreeModification<Node> change) {
140 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
141 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());