2 * Copyright (c) 2014 Pacnet 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.openflowplugin.applications.lldpspeaker;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.Iterables;
13 import java.util.Collection;
14 import java.util.HashMap;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * NodeConnectorInventoryEventTranslator is listening for changes in inventory operational DOM tree
41 * and update LLDPSpeaker and topology.
43 public class NodeConnectorInventoryEventTranslator<T extends DataObject>
44 implements ClusteredDataTreeChangeListener<T>, AutoCloseable {
45 private static final Logger LOG = LoggerFactory.getLogger(NodeConnectorInventoryEventTranslator.class);
47 private static final InstanceIdentifier<State> II_TO_STATE = InstanceIdentifier.builder(Nodes.class)
48 .child(Node.class).child(NodeConnector.class).augmentation(FlowCapableNodeConnector.class)
49 .child(State.class).build();
51 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
52 = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class)
53 .augmentation(FlowCapableNodeConnector.class).build();
55 private static final long STARTUP_LOOP_TICK = 500L;
56 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
58 private final ListenerRegistration<DataTreeChangeListener> listenerOnPortRegistration;
59 private final ListenerRegistration<DataTreeChangeListener> listenerOnPortStateRegistration;
60 private final Set<NodeConnectorEventsObserver> observers;
61 private final Map<InstanceIdentifier<?>, FlowCapableNodeConnector> iiToDownFlowCapableNodeConnectors
64 @SuppressWarnings("IllegalCatch")
65 public NodeConnectorInventoryEventTranslator(DataBroker dataBroker, NodeConnectorEventsObserver... observers) {
66 this.observers = ImmutableSet.copyOf(observers);
67 final DataTreeIdentifier<T> dtiToNodeConnector = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
68 II_TO_FLOW_CAPABLE_NODE_CONNECTOR);
69 final DataTreeIdentifier<T> dtiToNodeConnectorState = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
71 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
73 listenerOnPortRegistration = looper.loopUntilNoException(() ->
74 dataBroker.registerDataTreeChangeListener(dtiToNodeConnector,
75 NodeConnectorInventoryEventTranslator.this));
76 listenerOnPortStateRegistration = looper.loopUntilNoException(() ->
77 dataBroker.registerDataTreeChangeListener(dtiToNodeConnectorState,
78 NodeConnectorInventoryEventTranslator.this));
79 } catch (Exception e) {
80 LOG.error("DataTreeChangeListeners registration failed: {}", e);
81 throw new IllegalStateException("NodeConnectorInventoryEventTranslator startup failed!", e);
83 LOG.info("NodeConnectorInventoryEventTranslator has started.");
88 if (listenerOnPortRegistration != null) {
89 listenerOnPortRegistration.close();
91 if (listenerOnPortStateRegistration != null) {
92 listenerOnPortStateRegistration.close();
97 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> modifications) {
98 for (DataTreeModification modification : modifications) {
99 LOG.trace("Node connectors in inventory changed -> {}", modification.getRootNode().getModificationType());
100 switch (modification.getRootNode().getModificationType()) {
102 processAddedConnector(modification);
104 case SUBTREE_MODIFIED:
105 processUpdatedConnector(modification);
108 processRemovedConnector(modification);
111 throw new IllegalArgumentException(
112 "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
117 private void processAddedConnector(final DataTreeModification<T> modification) {
118 final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
119 InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier.firstIdentifierOf(NodeConnector.class);
120 if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
121 FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode()
123 if (!isPortDown(flowConnector)) {
124 notifyNodeConnectorAppeared(nodeConnectorInstanceId, flowConnector);
126 iiToDownFlowCapableNodeConnectors.put(nodeConnectorInstanceId, flowConnector);
131 private void processUpdatedConnector(final DataTreeModification<T> modification) {
132 final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
133 InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier.firstIdentifierOf(NodeConnector.class);
134 if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
135 FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode()
137 if (isPortDown(flowConnector)) {
138 notifyNodeConnectorDisappeared(nodeConnectorInstanceId);
140 notifyNodeConnectorAppeared(nodeConnectorInstanceId, flowConnector);
142 } else if (compareIITail(identifier, II_TO_STATE)) {
143 FlowCapableNodeConnector flowNodeConnector = iiToDownFlowCapableNodeConnectors.get(nodeConnectorInstanceId);
144 if (flowNodeConnector != null) {
145 State state = (State) modification.getRootNode().getDataAfter();
146 if (!state.isLinkDown()) {
147 FlowCapableNodeConnectorBuilder flowCapableNodeConnectorBuilder
148 = new FlowCapableNodeConnectorBuilder(flowNodeConnector);
149 flowCapableNodeConnectorBuilder.setState(state);
150 notifyNodeConnectorAppeared(nodeConnectorInstanceId, flowCapableNodeConnectorBuilder.build());
151 iiToDownFlowCapableNodeConnectors.remove(nodeConnectorInstanceId);
157 private void processRemovedConnector(final DataTreeModification<T> modification) {
158 final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
159 if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
160 InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier
161 .firstIdentifierOf(NodeConnector.class);
162 notifyNodeConnectorDisappeared(nodeConnectorInstanceId);
166 private boolean compareIITail(final InstanceIdentifier<?> ii1, final InstanceIdentifier<?> ii2) {
167 return Iterables.getLast(ii1.getPathArguments()).equals(Iterables.getLast(ii2.getPathArguments()));
170 private static boolean isPortDown(final FlowCapableNodeConnector flowCapableNodeConnector) {
171 PortState portState = flowCapableNodeConnector.getState();
172 PortConfig portConfig = flowCapableNodeConnector.getConfiguration();
173 return portState != null && portState.isLinkDown() || portConfig != null && portConfig.isPORTDOWN();
176 private void notifyNodeConnectorAppeared(final InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
177 final FlowCapableNodeConnector flowConnector) {
178 for (NodeConnectorEventsObserver observer : observers) {
179 observer.nodeConnectorAdded(nodeConnectorInstanceId, flowConnector);
183 private void notifyNodeConnectorDisappeared(final InstanceIdentifier<NodeConnector> nodeConnectorInstanceId) {
184 for (NodeConnectorEventsObserver observer : observers) {
185 observer.nodeConnectorRemoved(nodeConnectorInstanceId);