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.controller.md.sal.binding.api.DataObjectModification.ModificationType;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.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}. This listener should
29 * be registered on a netconf topology node.
32 public class DeviceListener implements DataTreeChangeListener<Node> {
34 private static final Logger LOG = LoggerFactory.getLogger(DeviceListener.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 DeviceListener(DeviceInventory deviceInventory) {
43 this.deviceInventory = deviceInventory;
47 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
48 List<DataTreeModification<Node>> changesWithoutDefaultNetconfNode = getRealDevicesOnly(changes);
49 for (DataTreeModification<Node> device : changesWithoutDefaultNetconfNode) {
50 String nodeId = device.getRootNode().getDataAfter().getKey().getNodeId().getValue();
51 NetconfNode netconfNode = device.getRootNode().getDataAfter().getAugmentation(NetconfNode.class);
52 if (isCreate(device) || isUpdate(device)) {
53 LOG.info("Node {} was modified", nodeId);
55 processModifiedSubtree(nodeId, netconfNode);
56 } catch (InterruptedException | ExecutionException e) {
57 LOG.error(e.getMessage(), e);
59 } else if (isDelete(device)) {
60 LOG.info("Node {} was deleted", nodeId);
66 * Handles the {@link ModificationType#SUBTREE_MODIFIED} case. If the changed node has
67 * {@link StringConstants.OPENROADM_DEVICE_MODEL_NAME} capabilities it may be persisted.
69 * @param nodeId device id
70 * @param netconfNode netconf node
71 * @throws InterruptedException may be thrown if there is a problem getting the device from
73 * @throws ExecutionException may be thrown if there is a problem getting the device from datastore
75 private void processModifiedSubtree(String nodeId, NetconfNode netconfNode)
76 throws InterruptedException, ExecutionException {
77 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
78 long count = netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
79 .filter(cp -> cp.getCapability().contains(StringConstants.OPENROADM_DEVICE_MODEL_NAME)).count();
81 LOG.info("No {} capable device was found", StringConstants.OPENROADM_DEVICE_MODEL_NAME);
84 if (ConnectionStatus.Connected.equals(connectionStatus)) {
85 deviceInventory.initializeDevice(nodeId);
86 } else if (ConnectionStatus.Connecting.equals(connectionStatus)
87 || ConnectionStatus.UnableToConnect.equals(connectionStatus)) {
88 LOG.info("The device is in {} state", connectionStatus);
90 LOG.warn("Invalid connection status {}", connectionStatus);
95 * Filters the {@link StringConstants#DEFAULT_NETCONF_NODEID} nodes from the provided
98 * @param changes a change
99 * @return {@code List<DataTreeModification<Node>>} a list of modifcations
101 private static List<DataTreeModification<Node>> getRealDevicesOnly(Collection<DataTreeModification<Node>> changes) {
102 return changes.stream()
103 .filter(change -> (change.getRootNode().getDataAfter() != null
104 && !StringConstants.DEFAULT_NETCONF_NODEID
105 .equalsIgnoreCase(change.getRootNode().getDataAfter().getKey().getNodeId().getValue())
106 && change.getRootNode().getDataAfter().getAugmentation(NetconfNode.class) != null)
107 || (change.getRootNode().getDataBefore() != null
108 && !StringConstants.DEFAULT_NETCONF_NODEID.equalsIgnoreCase(
109 change.getRootNode().getDataBefore().getKey().getNodeId().getValue())
110 && change.getRootNode().getDataBefore().getAugmentation(NetconfNode.class) != null
112 )).collect(Collectors.toList());
116 * In the filtered collection checks if the change is a new write.
118 * @param change the change
119 * @return boolean true if the change is a new write
121 private static boolean isCreate(DataTreeModification<Node> change) {
122 return change.getRootNode().getDataBefore() == null && change.getRootNode().getDataAfter() != null
123 && ModificationType.WRITE.equals(change.getRootNode().getModificationType());
127 * In the filtered collection checks if the modification is update.
129 * @param change the change
130 * @return boolean true if the modification is update
132 private static boolean isUpdate(DataTreeModification<Node> change) {
133 return ModificationType.SUBTREE_MODIFIED.equals(change.getRootNode().getModificationType());
137 * In the filtered collection checks if the node was deleted.
139 * @param change the change
140 * @return boolean true if the node was deleted
142 private static boolean isDelete(DataTreeModification<Node> change) {
143 return change.getRootNode().getDataBefore() != null && change.getRootNode().getDataAfter() == null
144 && ModificationType.DELETE.equals(change.getRootNode().getModificationType());