Cleanup table-miss-enforcer
[openflowplugin.git] / applications / table-miss-enforcer / src / main / java / org / opendaylight / openflowplugin / applications / tablemissenforcer / LLDPPacketPuntEnforcer.java
index 74e264a104926c36a064ad1285212373c6925e05..4736de4491db94aaf3d0ffc52f809e6ea2b06117 100644 (file)
@@ -5,39 +5,31 @@
  * 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.tablemissenforcer;
 
-import com.google.common.base.Preconditions;
-import java.math.BigInteger;
-import java.util.ArrayList;
+import static java.util.Objects.requireNonNull;
+
 import java.util.Collection;
-import java.util.List;
-import java.util.concurrent.Future;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
-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.infrautils.utils.concurrent.JdkFutures;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
+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.api.OFConstants;
 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
@@ -46,16 +38,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.I
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -63,7 +55,7 @@ public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeC
     private static final Logger LOG = LoggerFactory.getLogger(LLDPPacketPuntEnforcer.class);
     private static final long STARTUP_LOOP_TICK = 500L;
     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
-    private static final short TABLE_ID = (short) 0;
+    private static final Uint8 TABLE_ID = Uint8.ZERO;
     private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
     private static final String DEFAULT_FLOW_ID = "42";
     private final SalFlowService flowService;
@@ -71,19 +63,18 @@ public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeC
     private final DeviceOwnershipService deviceOwnershipService;
     private ListenerRegistration<?> listenerRegistration;
 
-    public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker,
-            DeviceOwnershipService deviceOwnershipService) {
+    public LLDPPacketPuntEnforcer(final SalFlowService flowService, final DataBroker dataBroker,
+            final DeviceOwnershipService deviceOwnershipService) {
         this.flowService = flowService;
         this.dataBroker = dataBroker;
-        this.deviceOwnershipService = Preconditions.checkNotNull(deviceOwnershipService,
-                "DeviceOwnershipService can not be null");
+        this.deviceOwnershipService = requireNonNull(deviceOwnershipService, "DeviceOwnershipService can not be null");
     }
 
     @SuppressWarnings("IllegalCatch")
     public void start() {
         final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
                 .augmentation(FlowCapableNode.class);
-        final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
+        final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
                 LogicalDatastoreType.OPERATIONAL, path);
         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
         try {
@@ -102,81 +93,64 @@ public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeC
     }
 
     @Override
-    public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
+    public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
         for (DataTreeModification<FlowCapableNode> modification : modifications) {
             if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
                 String nodeId = modification.getRootPath().getRootIdentifier()
-                        .firstKeyOf(Node.class, NodeKey.class).getId().getValue();
+                        .firstKeyOf(Node.class).getId().getValue();
                 if (deviceOwnershipService.isEntityOwned(nodeId)) {
                     AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
                     addFlowInput.setNode(new NodeRef(modification.getRootPath()
                             .getRootIdentifier().firstIdentifierOf(Node.class)));
-                    final Future<RpcResult<AddFlowOutput>> resultFuture = this.flowService
-                            .addFlow(addFlowInput.build());
-                    JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
+                    LoggingFutures.addErrorLogging(this.flowService.addFlow(addFlowInput.build()), LOG, "addFlow");
                 } else {
-                    LOG.debug("Node {} is not owned by this controller, so skip adding LLDP table miss flow",
-                            nodeId);
+                    LOG.debug("Node {} is not owned by this controller, so skip adding LLDP table miss flow", nodeId);
                 }
-
             }
         }
     }
 
     static Flow createFlow() {
-        FlowBuilder flowBuilder = new FlowBuilder();
-        flowBuilder.setMatch(new MatchBuilder().build());
-        flowBuilder.setInstructions(createSendToControllerInstructions().build());
-        flowBuilder.setPriority(0);
-
-        FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
-        flowBuilder.setBarrier(Boolean.FALSE);
-        flowBuilder.setBufferId(OFConstants.OFP_NO_BUFFER);
-        BigInteger value = BigInteger.valueOf(10L);
-        flowBuilder.setCookie(new FlowCookie(value));
-        flowBuilder.setCookieMask(new FlowCookie(value));
-        flowBuilder.setHardTimeout(0);
-        flowBuilder.setIdleTimeout(0);
-        flowBuilder.setInstallHw(false);
-        flowBuilder.setStrict(false);
-        flowBuilder.setContainerName(null);
-        flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
-        flowBuilder.setId(new FlowId("12"));
-        flowBuilder.setTableId(TABLE_ID);
-        flowBuilder.withKey(key);
-        flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
-
-        return flowBuilder.build();
+        return new FlowBuilder()
+            .setMatch(new MatchBuilder().build())
+            .setInstructions(createSendToControllerInstructions().build())
+            .setPriority(Uint16.ZERO)
+            .setBarrier(Boolean.FALSE)
+            .setBufferId(OFConstants.OFP_NO_BUFFER)
+            .setCookie(new FlowCookie(Uint64.TEN))
+            .setCookieMask(new FlowCookie(Uint64.TEN))
+            .setHardTimeout(Uint16.ZERO)
+            .setIdleTimeout(Uint16.ZERO)
+            .setInstallHw(false)
+            .setStrict(false)
+            .setContainerName(null)
+            .setFlags(new FlowModFlags(false, false, false, false, true))
+            .setId(new FlowId("12"))
+            .setTableId(TABLE_ID)
+            .withKey(new FlowKey(new FlowId(DEFAULT_FLOW_ID)))
+            .setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW)
+            .build();
     }
 
     private static InstructionsBuilder createSendToControllerInstructions() {
-        final List<Action> actionList = new ArrayList<>();
-        ActionBuilder ab = new ActionBuilder();
-
-        OutputActionBuilder output = new OutputActionBuilder();
-        output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
-        Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
-        output.setOutputNodeConnector(value);
-        ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
-        ab.setOrder(0);
-        ab.withKey(new ActionKey(0));
-        actionList.add(ab.build());
-        // Create an Apply Action
-        ApplyActionsBuilder aab = new ApplyActionsBuilder();
-        aab.setAction(actionList);
-
-        // Wrap our Apply Action in an Instruction
-        InstructionBuilder ib = new InstructionBuilder();
-        ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
-        ib.setOrder(0);
-        ib.withKey(new InstructionKey(0));
-
-        // Put our Instruction in a list of Instructions
-        InstructionsBuilder isb = new InstructionsBuilder();
-        List<Instruction> instructions = new ArrayList<>();
-        instructions.add(ib.build());
-        isb.setInstruction(instructions);
-        return isb;
+        return new InstructionsBuilder()
+            .setInstruction(BindingMap.of(new InstructionBuilder()
+                // Wrap our Apply Action in an Instruction
+                .setInstruction(new ApplyActionsCaseBuilder()
+                    .setApplyActions(new ApplyActionsBuilder()
+                        // Create an Apply Action
+                        .setAction(BindingMap.of(new ActionBuilder()
+                            .setAction(new OutputActionCaseBuilder()
+                                .setOutputAction(new OutputActionBuilder()
+                                    .setMaxLength(OFConstants.OFPCML_NO_BUFFER)
+                                    .setOutputNodeConnector(new Uri(OutputPortValues.CONTROLLER.toString()))
+                                    .build())
+                                .build())
+                            .setOrder(0)
+                            .build()))
+                        .build())
+                    .build())
+                .setOrder(0)
+                .build()));
     }
-
 }