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;
16 import org.opendaylight.mdsal.binding.api.DataObjectModification;
17 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
18 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.transportpce.common.StringConstants;
21 import org.opendaylight.transportpce.inventory.DeviceInventory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
31 * This listener should be registered on a netconf topology node.
33 public class DeviceConfigListener implements DataTreeChangeListener<Node> {
35 private static final Logger LOG = LoggerFactory.getLogger(DeviceConfigListener.class);
36 private final DeviceInventory deviceInventory;
39 * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
41 * @param deviceInventory reference to the {@link DeviceInventory}
43 public DeviceConfigListener(DeviceInventory deviceInventory) {
44 this.deviceInventory = deviceInventory;
48 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
50 //LOG.info("################testing np1:"+changes.toString());
51 String openROADMversion = "";
52 List<DataTreeModification<Node>> changesWithoutDefaultNetconfNode = getRealDevicesOnly(changes);
53 for (DataTreeModification<Node> device : changesWithoutDefaultNetconfNode) {
54 DataObjectModification<Node> rootNode = device.getRootNode();
55 String nodeId = rootNode.getDataAfter().key().getNodeId().getValue();
57 LOG.info("################nodeId {}", nodeId);
59 NetconfNode netconfNode = rootNode.getDataAfter().augmentation(NetconfNode.class);
60 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
61 netconfNode.getConnectionStatus();
62 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
63 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
65 LOG.info("################## DCL Modification Type {}",
66 device.getRootNode().getModificationType().toString());
67 LOG.info("################## DCL Capability Count {}", count);
68 LOG.info("################## DCL Connection Status {}", connectionStatus);
69 if (isCreate(device) || isUpdate(device)) {
70 LOG.info("Node {} was modified", nodeId);
72 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
73 } catch (InterruptedException | ExecutionException e) {
74 LOG.error(e.getMessage(), e);
77 } else if (isDelete(device)) {
78 LOG.info("Node {} was deleted", nodeId);
84 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
85 * If the changed node has.
87 * @param nodeId device id
88 * @param netconfNode netconf node
89 * @throws InterruptedException may be thrown if there is a problem getting the device from
91 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
93 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
94 throws InterruptedException, ExecutionException {
95 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
96 /*long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
97 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).count();
99 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
102 if (ConnectionStatus.Connected.equals(connectionStatus)) {
103 LOG.info("DCL The device is in {} state", connectionStatus);
104 deviceInventory.initializeDevice(nodeId, openROADMversion);
105 } else if (ConnectionStatus.Connecting.equals(connectionStatus)
106 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
107 LOG.info("DCL The device is in {} state", connectionStatus);
109 LOG.warn("DCL Invalid connection status {}", connectionStatus);
114 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
117 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
118 return changes.stream()
119 .filter(change -> (change.getRootNode().getDataAfter() != null
120 && !StringConstants.DEFAULT_NETCONF_NODEID
121 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
122 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
123 || (change.getRootNode().getDataBefore() != null
124 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
125 change.getRootNode().getDataBefore().key().getNodeId().getValue())
126 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
128 )).collect(Collectors.toList());
132 * In the filtered collection checks if the change is a new write.
135 private static boolean isCreate(DataTreeModification<Node> change) {
136 return change.getRootNode().getDataBefore() == null && change.getRootNode().getDataAfter() != null
137 && ModificationType.WRITE.equals(change.getRootNode().getModificationType());
141 * In the filtered collection checks if the modification is update.
144 private static boolean isUpdate(DataTreeModification<Node> change) {
145 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
149 * In the filtered collection checks if the node was deleted.
152 private static boolean isDelete(DataTreeModification<Node> change) {
153 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
154 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());