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.debug("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.debug("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.debug("DCL Modification Type {}", device.getRootNode().getModificationType().toString());
65 LOG.debug("DCL Capability Count {}", count);
66 LOG.debug("DCL Connection Status {}", connectionStatus);
67 if (isCreate(device) || isUpdate(device)) {
68 LOG.info("Node {} was modified", nodeId);
70 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
71 } catch (InterruptedException | ExecutionException e) {
72 LOG.error("something wrong when modifying node {}", nodeId, e);
74 } else if (isDelete(device)) {
75 LOG.info("Node {} was deleted", nodeId);
81 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
82 * If the changed node has.
84 * @param nodeId device id
85 * @param netconfNode netconf node
86 * @throws InterruptedException may be thrown if there is a problem getting the device from
88 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
90 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
91 throws InterruptedException, ExecutionException {
92 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
93 /*long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
94 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).count();
96 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
99 if (ConnectionStatus.Connected.equals(connectionStatus)) {
100 LOG.info("DCL The device is in {} state", connectionStatus);
101 deviceInventory.initializeDevice(nodeId, openROADMversion);
102 } else if (ConnectionStatus.Connecting.equals(connectionStatus)
103 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
104 LOG.info("DCL The device is in {} state", connectionStatus);
106 LOG.warn("DCL Invalid connection status {}", connectionStatus);
111 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
114 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
115 return changes.stream()
116 .filter(change -> (change.getRootNode().getDataAfter() != null
117 && !StringConstants.DEFAULT_NETCONF_NODEID
118 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
119 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
120 || (change.getRootNode().getDataBefore() != null
121 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
122 change.getRootNode().getDataBefore().key().getNodeId().getValue())
123 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
125 )).collect(Collectors.toList());
129 * In the filtered collection checks if the change is a new write.
132 private static boolean isCreate(DataTreeModification<Node> change) {
133 return change.getRootNode().getDataBefore() == null && change.getRootNode().getDataAfter() != null
134 && ModificationType.WRITE.equals(change.getRootNode().getModificationType());
138 * In the filtered collection checks if the modification is update.
141 private static boolean isUpdate(DataTreeModification<Node> change) {
142 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
146 * In the filtered collection checks if the node was deleted.
149 private static boolean isDelete(DataTreeModification<Node> change) {
150 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
151 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());