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;
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;
29 * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
30 * This listener should be registered on a netconf topology node.
32 public class DeviceConfigListener implements DataTreeChangeListener<Node> {
34 private static final Logger LOG = LoggerFactory.getLogger(DeviceConfigListener.class);
35 private final DeviceInventory deviceInventory;
38 * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
40 * @param deviceInventory reference to the {@link DeviceInventory}
42 public DeviceConfigListener(DeviceInventory deviceInventory) {
43 this.deviceInventory = deviceInventory;
47 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
49 //LOG.info("################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();
56 LOG.info("################nodeId {}", nodeId);
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))
64 LOG.info("################## DCL Modification Type {}",
65 device.getRootNode().getModificationType().toString());
66 LOG.info("################## DCL Capability Count {}", count);
67 LOG.info("################## DCL Connection Status {}", connectionStatus);
68 if (isCreate(device) || isUpdate(device)) {
69 LOG.info("Node {} was modified", nodeId);
71 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
72 } catch (InterruptedException | ExecutionException e) {
73 LOG.error(e.getMessage(), e);
76 } else if (isDelete(device)) {
77 LOG.info("Node {} was deleted", nodeId);
83 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
84 * If the changed node has.
86 * @param nodeId device id
87 * @param netconfNode netconf node
88 * @throws InterruptedException may be thrown if there is a problem getting the device from
90 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
92 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
93 throws InterruptedException, ExecutionException {
94 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
95 /*long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
96 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).count();
98 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
101 if (ConnectionStatus.Connected.equals(connectionStatus)) {
102 LOG.info("DCL The device is in {} state", connectionStatus);
103 deviceInventory.initializeDevice(nodeId, openROADMversion);
104 } else if (ConnectionStatus.Connecting.equals(connectionStatus)
105 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
106 LOG.info("DCL The device is in {} state", connectionStatus);
108 LOG.warn("DCL Invalid connection status {}", connectionStatus);
113 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
116 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
117 return changes.stream()
118 .filter(change -> (change.getRootNode().getDataAfter() != null
119 && !StringConstants.DEFAULT_NETCONF_NODEID
120 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
121 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
122 || (change.getRootNode().getDataBefore() != null
123 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
124 change.getRootNode().getDataBefore().key().getNodeId().getValue())
125 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
127 )).collect(Collectors.toList());
131 * In the filtered collection checks if the change is a new write.
134 private static boolean isCreate(DataTreeModification<Node> change) {
135 return change.getRootNode().getDataBefore() == null && change.getRootNode().getDataAfter() != null
136 && ModificationType.WRITE.equals(change.getRootNode().getModificationType());
140 * In the filtered collection checks if the modification is update.
143 private static boolean isUpdate(DataTreeModification<Node> change) {
144 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
148 * In the filtered collection checks if the node was deleted.
151 private static boolean isDelete(DataTreeModification<Node> change) {
152 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
153 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());