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
8 package org.opendaylight.transportpce.inventory.listener;
10 import java.util.Collection;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.stream.Collectors;
14 import org.opendaylight.mdsal.binding.api.DataObjectModification;
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.rev231024.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 DeviceConfigListener implements DataTreeChangeListener<Node> {
32 private static final Logger LOG = LoggerFactory.getLogger(DeviceConfigListener.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 DeviceConfigListener(DeviceInventory deviceInventory) {
41 this.deviceInventory = deviceInventory;
45 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 DataObjectModification<Node> rootNode = device.getRootNode();
52 String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
54 LOG.debug("nodeId {}", nodeId);
56 NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
57 ConnectionStatus connectionStatus =
58 netconfNode.getConnectionStatus();
59 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
60 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
62 LOG.debug("DCL Modification Type {}", device.getRootNode().getModificationType().toString());
63 LOG.debug("DCL Capability Count {}", count);
64 LOG.debug("DCL Connection Status {}", connectionStatus);
65 if (isCreate(device) || isUpdate(device)) {
66 LOG.info("Node {} was modified", nodeId);
68 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
69 } catch (InterruptedException | ExecutionException e) {
70 LOG.error("something wrong when modifying node {}", nodeId, e);
72 } else if (isDelete(device)) {
73 LOG.info("Node {} was deleted", nodeId);
79 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
80 * If the changed node has.
82 * @param nodeId device id
83 * @param netconfNode netconf node
84 * @throws InterruptedException may be thrown if there is a problem getting the device from
86 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
88 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
89 throws InterruptedException, ExecutionException {
90 ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
91 /*long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
92 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).count();
94 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
97 if (ConnectionStatus.Connected.equals(connectionStatus)) {
98 LOG.info("DCL 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("DCL The device is in {} state", connectionStatus);
104 LOG.warn("DCL Invalid connection status {}", connectionStatus);
109 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
112 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
113 return changes.stream()
114 .filter(change -> (change.getRootNode().getDataAfter() != null
115 && !StringConstants.DEFAULT_NETCONF_NODEID
116 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
117 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
118 || (change.getRootNode().getDataBefore() != null
119 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
120 change.getRootNode().getDataBefore().key().getNodeId().getValue())
121 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
123 )).collect(Collectors.toList());
127 * In the filtered collection checks if the change is a new write.
130 private static boolean isCreate(DataTreeModification<Node> change) {
131 return change.getRootNode().getDataBefore() == null && change.getRootNode().getDataAfter() != null
132 && ModificationType.WRITE.equals(change.getRootNode().getModificationType());
136 * In the filtered collection checks if the modification is update.
139 private static boolean isUpdate(DataTreeModification<Node> change) {
140 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
144 * In the filtered collection checks if the node was deleted.
147 private static boolean isDelete(DataTreeModification<Node> change) {
148 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
149 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());