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.node.topology.rev150114.NetconfNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * This class implements the {@link DataTreeChangeListener} on a {@link Node}.
29 * This listener should be registered on a netconf topology node.
31 public class DeviceListener implements DataTreeChangeListener<Node> {
33 private static final Logger LOG = LoggerFactory.getLogger(DeviceListener.class);
34 private final DeviceInventory deviceInventory;
37 * Default constructor invoked by blueprint injects {@link DeviceInventory} as a persistence layer.
39 * @param deviceInventory reference to the {@link DeviceInventory}
41 public DeviceListener(DeviceInventory deviceInventory) {
42 this.deviceInventory = deviceInventory;
46 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 String nodeId = device.getRootNode().getDataAfter().key().getNodeId().getValue();
52 NetconfNode netconfNode = device.getRootNode().getDataAfter().augmentation(NetconfNode.class);
53 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus =
54 netconfNode.getConnectionStatus();
55 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
56 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
58 LOG.debug("DL Modification Type {}", device.getRootNode().getModificationType().toString());
59 LOG.debug("DL Capability Count {}", count);
60 LOG.debug("DL Connection Status {}", connectionStatus);
61 LOG.debug("DL device.getRootNode().getDataBefore() {}", device.getRootNode().getDataBefore());
62 LOG.debug("DL device.getRootNode().getDataAfter() {}", device.getRootNode().getDataAfter());
64 if (isCreate(device)) {
65 LOG.info("Node {} was created", nodeId);
67 processModifiedSubtree(nodeId, netconfNode, openROADMversion);
68 } catch (InterruptedException | ExecutionException e) {
69 LOG.error("something wrong when creating node {}", nodeId, e);
71 } else if (isDelete(device)) {
72 LOG.info("Node {} was deleted", nodeId);
78 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case.
79 * If the changed node has.
81 * @param nodeId device id
82 * @param netconfNode netconf node
83 * @throws InterruptedException may be thrown if there is a problem getting the device from
85 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
87 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode, String openROADMversion)
88 throws InterruptedException, ExecutionException {
89 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
91 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
92 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME))
96 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
99 if (ConnectionStatus.Connected.equals(connectionStatus)) {
100 LOG.info("DL 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("DL The device is in {} state", connectionStatus);
106 LOG.warn("DL Invalid connection status {}", connectionStatus);
112 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided {@link Collection}.
115 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
116 return changes.stream()
117 .filter(change -> (change.getRootNode().getDataAfter() != null
118 && !StringConstants.DEFAULT_NETCONF_NODEID
119 .equalsIgnoreCase(change.getRootNode().getDataAfter().key().getNodeId().getValue())
120 && change.getRootNode().getDataAfter().augmentation(NetconfNode.class) != null)
121 || (change.getRootNode().getDataBefore() != null
122 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
123 change.getRootNode().getDataBefore().key().getNodeId().getValue())
124 && change.getRootNode().getDataBefore().augmentation(NetconfNode.class) != null
126 )).collect(Collectors.toList());
130 * In the filtered collection checks if the change is a new write.
133 private static boolean isCreate(DataTreeModification<Node> change) {
134 return change.getRootNode().getModificationType().toString().equalsIgnoreCase("WRITE");
138 * In the filtered collection checks if the node was deleted.
141 private static boolean isDelete(DataTreeModification<Node> change) {
142 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
143 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());