Switch to MD-SAL APIs
[openflowplugin.git] / applications / lldp-speaker / src / main / java / org / opendaylight / openflowplugin / applications / lldpspeaker / NodeConnectorInventoryEventTranslator.java
index 835dc97e5140ed0628cd3af2040db8b1b2bf73ad..0e5c97f4a61e49ce70185b527422a8a8e3f6e7be 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.applications.lldpspeaker;
 
 import com.google.common.collect.ImmutableSet;
@@ -14,13 +13,13 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Callable;
 import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
@@ -41,52 +40,41 @@ import org.slf4j.LoggerFactory;
  * and update LLDPSpeaker and topology.
  */
 public class NodeConnectorInventoryEventTranslator<T extends DataObject>
-        implements DataTreeChangeListener<T>, AutoCloseable {
+        implements ClusteredDataTreeChangeListener<T>, AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(NodeConnectorInventoryEventTranslator.class);
 
-    private static final InstanceIdentifier<State> II_TO_STATE
-        = InstanceIdentifier.builder(Nodes.class)
-            .child(Node.class)
-            .child(NodeConnector.class)
-            .augmentation(FlowCapableNodeConnector.class)
-            .child(State.class)
-            .build();
+    private static final InstanceIdentifier<State> II_TO_STATE = InstanceIdentifier.builder(Nodes.class)
+            .child(Node.class).child(NodeConnector.class).augmentation(FlowCapableNodeConnector.class)
+            .child(State.class).build();
 
     private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
-        = InstanceIdentifier.builder(Nodes.class)
-            .child(Node.class)
-            .child(NodeConnector.class)
-            .augmentation(FlowCapableNodeConnector.class)
-            .build();
+            = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class)
+            .augmentation(FlowCapableNodeConnector.class).build();
 
     private static final long STARTUP_LOOP_TICK = 500L;
     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
-    private static final Logger LOG = LoggerFactory.getLogger(NodeConnectorInventoryEventTranslator.class);
 
-    private final ListenerRegistration<DataTreeChangeListener> dataChangeListenerRegistration;
+    private final ListenerRegistration<DataTreeChangeListener> listenerOnPortRegistration;
     private final ListenerRegistration<DataTreeChangeListener> listenerOnPortStateRegistration;
     private final Set<NodeConnectorEventsObserver> observers;
-    private final Map<InstanceIdentifier<?>,FlowCapableNodeConnector> iiToDownFlowCapableNodeConnectors = new HashMap<>();
+    private final Map<InstanceIdentifier<?>, FlowCapableNodeConnector> iiToDownFlowCapableNodeConnectors
+            = new HashMap<>();
 
+    @SuppressWarnings("IllegalCatch")
     public NodeConnectorInventoryEventTranslator(DataBroker dataBroker, NodeConnectorEventsObserver... observers) {
         this.observers = ImmutableSet.copyOf(observers);
-        final DataTreeIdentifier<T> dtiToNodeConnector =
-                new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, II_TO_FLOW_CAPABLE_NODE_CONNECTOR);
-        final DataTreeIdentifier<T> dtiToNodeConnectorState =
-                new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, II_TO_STATE);
+        final DataTreeIdentifier dtiToNodeConnector = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+                                                                                   II_TO_FLOW_CAPABLE_NODE_CONNECTOR);
+        final DataTreeIdentifier dtiToNodeConnectorState = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+                                                                                   II_TO_STATE);
         final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
         try {
-            dataChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DataTreeChangeListener>>() {
-                @Override
-                public ListenerRegistration<DataTreeChangeListener> call() throws Exception {
-                    return dataBroker.registerDataTreeChangeListener(dtiToNodeConnector, NodeConnectorInventoryEventTranslator.this);
-                }
-            });
-            listenerOnPortStateRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DataTreeChangeListener>>() {
-                @Override
-                public ListenerRegistration<DataTreeChangeListener> call() throws Exception {
-                    return dataBroker.registerDataTreeChangeListener(dtiToNodeConnectorState, NodeConnectorInventoryEventTranslator.this);
-                }
-            });
+            listenerOnPortRegistration = looper.loopUntilNoException(() ->
+                    dataBroker.registerDataTreeChangeListener(dtiToNodeConnector,
+                            NodeConnectorInventoryEventTranslator.this));
+            listenerOnPortStateRegistration = looper.loopUntilNoException(() ->
+                    dataBroker.registerDataTreeChangeListener(dtiToNodeConnectorState,
+                            NodeConnectorInventoryEventTranslator.this));
         } catch (Exception e) {
             LOG.error("DataTreeChangeListeners registration failed: {}", e);
             throw new IllegalStateException("NodeConnectorInventoryEventTranslator startup failed!", e);
@@ -96,8 +84,8 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
 
     @Override
     public void close() {
-        if (dataChangeListenerRegistration != null) {
-            dataChangeListenerRegistration.close();
+        if (listenerOnPortRegistration != null) {
+            listenerOnPortRegistration.close();
         }
         if (listenerOnPortStateRegistration != null) {
             listenerOnPortStateRegistration.close();
@@ -106,7 +94,7 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
 
     @Override
     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> modifications) {
-        for(DataTreeModification modification : modifications) {
+        for (DataTreeModification modification : modifications) {
             LOG.trace("Node connectors in inventory changed -> {}", modification.getRootNode().getModificationType());
             switch (modification.getRootNode().getModificationType()) {
                 case WRITE:
@@ -119,17 +107,18 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
                     processRemovedConnector(modification);
                     break;
                 default:
-                    throw new IllegalArgumentException("Unhandled modification type: {}" +
-                            modification.getRootNode().getModificationType());
+                    throw new IllegalArgumentException(
+                            "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
             }
         }
     }
 
     private void processAddedConnector(final DataTreeModification<T> modification) {
         final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
-        InstanceIdentifier<NodeConnector> nodeConnectorInstanceId =identifier.firstIdentifierOf(NodeConnector.class);
+        InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier.firstIdentifierOf(NodeConnector.class);
         if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
-            FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode().getDataAfter();
+            FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode()
+                    .getDataAfter();
             if (!isPortDown(flowConnector)) {
                 notifyNodeConnectorAppeared(nodeConnectorInstanceId, flowConnector);
             } else {
@@ -142,7 +131,8 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
         final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
         InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier.firstIdentifierOf(NodeConnector.class);
         if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
-            FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode().getDataAfter();
+            FlowCapableNodeConnector flowConnector = (FlowCapableNodeConnector) modification.getRootNode()
+                    .getDataAfter();
             if (isPortDown(flowConnector)) {
                 notifyNodeConnectorDisappeared(nodeConnectorInstanceId);
             } else {
@@ -153,8 +143,8 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
             if (flowNodeConnector != null) {
                 State state = (State) modification.getRootNode().getDataAfter();
                 if (!state.isLinkDown()) {
-                    FlowCapableNodeConnectorBuilder flowCapableNodeConnectorBuilder =
-                            new FlowCapableNodeConnectorBuilder(flowNodeConnector);
+                    FlowCapableNodeConnectorBuilder flowCapableNodeConnectorBuilder
+                            new FlowCapableNodeConnectorBuilder(flowNodeConnector);
                     flowCapableNodeConnectorBuilder.setState(state);
                     notifyNodeConnectorAppeared(nodeConnectorInstanceId, flowCapableNodeConnectorBuilder.build());
                     iiToDownFlowCapableNodeConnectors.remove(nodeConnectorInstanceId);
@@ -166,7 +156,8 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
     private void processRemovedConnector(final DataTreeModification<T> modification) {
         final InstanceIdentifier<T> identifier = modification.getRootPath().getRootIdentifier();
         if (compareIITail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
-            InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier.firstIdentifierOf(NodeConnector.class);
+            InstanceIdentifier<NodeConnector> nodeConnectorInstanceId = identifier
+                    .firstIdentifierOf(NodeConnector.class);
             notifyNodeConnectorDisappeared(nodeConnectorInstanceId);
         }
     }
@@ -178,8 +169,7 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
     private static boolean isPortDown(final FlowCapableNodeConnector flowCapableNodeConnector) {
         PortState portState = flowCapableNodeConnector.getState();
         PortConfig portConfig = flowCapableNodeConnector.getConfiguration();
-        return portState != null && portState.isLinkDown()
-                || portConfig != null && portConfig.isPORTDOWN();
+        return portState != null && portState.isLinkDown() || portConfig != null && portConfig.isPORTDOWN();
     }
 
     private void notifyNodeConnectorAppeared(final InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
@@ -194,5 +184,4 @@ public class NodeConnectorInventoryEventTranslator<T extends DataObject>
             observer.nodeConnectorRemoved(nodeConnectorInstanceId);
         }
     }
-
 }