Action redesign: use the new ActionInfo Genius classes
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclNodeListener.java
index b1a5fa96598eefb9560b2ec1575a2da3117fda38..10318917ec5345165ff49bdf0260403530e0eceb 100644 (file)
@@ -12,14 +12,12 @@ import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
 import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
-import org.opendaylight.genius.mdsalutil.ActionType;
 import org.opendaylight.genius.mdsalutil.FlowEntity;
 import org.opendaylight.genius.mdsalutil.InstructionInfo;
 import org.opendaylight.genius.mdsalutil.InstructionType;
@@ -30,9 +28,10 @@ import org.opendaylight.genius.mdsalutil.MatchInfoBase;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
+import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
+import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.genius.mdsalutil.packet.IPProtocols;
-import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
@@ -52,26 +51,14 @@ import org.slf4j.LoggerFactory;
 public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, AclNodeListener>
         implements AutoCloseable {
 
-    /** The Constant LOG. */
     private static final Logger LOG = LoggerFactory.getLogger(AclNodeListener.class);
 
-    /** The mdsal manager. */
     private final IMdsalApiManager mdsalManager;
-
-    /** The data broker. */
+    private final AclserviceConfig config;
     private final DataBroker dataBroker;
 
     private SecurityGroupMode securityGroupMode = null;
 
-    private AclserviceConfig config;
-
-    /**
-     * Instantiates a new acl node listener.
-     *
-     * @param mdsalManager the mdsal manager
-     * @param dataBroker the data broker
-     * @param config aclservice configuration
-     */
     @Inject
     public AclNodeListener(final IMdsalApiManager mdsalManager, DataBroker dataBroker, AclserviceConfig config) {
         super(FlowCapableNode.class, AclNodeListener.class);
@@ -81,8 +68,9 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         this.config = config;
     }
 
+    @Override
     @PostConstruct
-    public void start() {
+    public void init() {
         LOG.info("{} start", getClass().getSimpleName());
         if (config != null) {
             this.securityGroupMode = config.getSecurityGroupMode();
@@ -91,12 +79,6 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         LOG.info("AclserviceConfig: {}", this.config);
     }
 
-    @PreDestroy
-    @Override
-    public void close() throws Exception {
-        super.close();
-    }
-
     @Override
     protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
@@ -111,15 +93,11 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
     protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
             FlowCapableNode dataObjectModificationAfter) {
         // do nothing
-
     }
 
     @Override
     protected void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
         LOG.trace("FlowCapableNode Added: key: {}", key);
-        if (!AclClusterUtil.isEntityOwner()) {
-            return;
-        }
         NodeKey nodeKey = key.firstKeyOf(Node.class);
         BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
         createTableDefaultEntries(dpnId);
@@ -161,7 +139,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         List<MatchInfo> mkMatches = new ArrayList<>();
         List<InstructionInfo> mkInstructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        actionsInfos.add(new ActionDrop());
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_ACL_TABLE,
@@ -180,10 +158,8 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
     private void addLearnEgressAclTableMissFlow(BigInteger dpId) {
         List<InstructionInfo> mkInstructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit,
-                new String[] {Short.toString(NwConstants.EGRESS_LEARN_TABLE) }));
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit,
-                new String[] {Short.toString(NwConstants.EGRESS_LEARN2_TABLE) }));
+        actionsInfos.add(new ActionNxResubmit(NwConstants.EGRESS_LEARN_TABLE));
+        actionsInfos.add(new ActionNxResubmit(NwConstants.EGRESS_LEARN2_TABLE));
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         List<MatchInfo> mkMatches = new ArrayList<>();
@@ -196,28 +172,22 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         mkMatches = new ArrayList<>();
         mkInstructions = new ArrayList<>();
         actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        actionsInfos.add(new ActionDrop());
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
-        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_LEARN_TABLE,
-                "LEARN-" + getTableMissFlowId(NwConstants.EGRESS_LEARN_TABLE), 0,
-                "Egress Learn ACL Table Miss Flow", 0, 0,
-                AclConstants.COOKIE_ACL_BASE, mkMatches, mkInstructions);
-        mdsalManager.installFlow(flowEntity);
-
-        flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_LEARN2_TABLE,
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_LEARN2_TABLE,
                 "LEARN-" + getTableMissFlowId(NwConstants.EGRESS_LEARN2_TABLE), 0,
                 "Egress Learn2 ACL Table Miss Flow", 0, 0,
                 AclConstants.COOKIE_ACL_BASE, mkMatches, mkInstructions);
         mdsalManager.installFlow(flowEntity);
 
         List<NxMatchInfo> nxMkMatches = new ArrayList<>();
-        nxMkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_6,
+        nxMkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_5,
                 new long[] {Long.valueOf(AclConstants.LEARN_MATCH_REG_VALUE)}));
 
         short dispatcherTableId = NwConstants.EGRESS_LPORT_DISPATCHER_TABLE;
         List<InstructionInfo> instructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_LEARN2_TABLE,
@@ -231,10 +201,8 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
     private void addLearnIngressAclTableMissFlow(BigInteger dpId) {
         List<InstructionInfo> mkInstructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit,
-                new String[] {Short.toString(NwConstants.INGRESS_LEARN_TABLE) }));
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit,
-                new String[] {Short.toString(NwConstants.INGRESS_LEARN2_TABLE) }));
+        actionsInfos.add(new ActionNxResubmit(NwConstants.INGRESS_LEARN_TABLE));
+        actionsInfos.add(new ActionNxResubmit(NwConstants.INGRESS_LEARN2_TABLE));
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         List<MatchInfo> mkMatches = new ArrayList<>();
@@ -247,28 +215,22 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         mkMatches = new ArrayList<>();
         mkInstructions = new ArrayList<>();
         actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        actionsInfos.add(new ActionDrop());
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
-        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_LEARN_TABLE,
-                "LEARN-" + getTableMissFlowId(NwConstants.INGRESS_LEARN_TABLE), 0,
-                "Ingress Learn ACL Table Miss Flow", 0, 0,
-                AclConstants.COOKIE_ACL_BASE, mkMatches, mkInstructions);
-        mdsalManager.installFlow(flowEntity);
-
-        flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_LEARN2_TABLE,
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_LEARN2_TABLE,
                 "LEARN-" + getTableMissFlowId(NwConstants.INGRESS_LEARN2_TABLE), 0,
                 "Ingress Learn2 ACL Table Miss Flow", 0, 0,
                 AclConstants.COOKIE_ACL_BASE, mkMatches, mkInstructions);
         mdsalManager.installFlow(flowEntity);
 
         List<NxMatchInfo> nxMkMatches = new ArrayList<>();
-        nxMkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_6,
+        nxMkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_5,
                 new long[] {Long.valueOf(AclConstants.LEARN_MATCH_REG_VALUE)}));
 
         short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
         List<InstructionInfo> instructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_LEARN2_TABLE,
@@ -301,7 +263,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
 
         List<ActionInfo> actionsInfos = new ArrayList<>();
         List<InstructionInfo> dispatcherInstructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         dispatcherInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         nextTblFlowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE,
@@ -333,7 +295,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
 
         List<ActionInfo> actionsInfos = new ArrayList<>();
         List<InstructionInfo> instructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         nextTblFlowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE,
@@ -359,7 +321,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         synMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
 
         List<ActionInfo> dropActionsInfos = new ArrayList<>();
-        dropActionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        dropActionsInfos.add(new ActionDrop());
         List<InstructionInfo> synInstructions = new ArrayList<>();
         synInstructions.add(new InstructionInfo(InstructionType.apply_actions, dropActionsInfos));
 
@@ -398,7 +360,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
 
         List<ActionInfo> actionsInfos = new ArrayList<>();
         List<InstructionInfo> instructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         FlowEntity nextTblFlowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE,
@@ -428,7 +390,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         synMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
 
         List<ActionInfo> synActionsInfos = new ArrayList<>();
-        synActionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        synActionsInfos.add(new ActionDrop());
         List<InstructionInfo> synInstructions = new ArrayList<>();
         synInstructions.add(new InstructionInfo(InstructionType.apply_actions, synActionsInfos));
 
@@ -461,7 +423,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
 
         List<ActionInfo> actionsInfos = new ArrayList<>();
         List<InstructionInfo> dispatcherInstructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         dispatcherInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         FlowEntity nextTblFlowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE,
@@ -482,7 +444,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         List<MatchInfo> mkMatches = new ArrayList<>();
         List<InstructionInfo> mkInstructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
+        actionsInfos.add(new ActionDrop());
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
 
         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.INGRESS_ACL_TABLE,
@@ -506,7 +468,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
             .TRACKED_REL_CT_STATE, AclConstants.TRACKED_REL_CT_STATE_MASK, dispatcherTableId, tableId, write );
         programConntrackDropRule(dpnId, AclConstants.CT_STATE_NEW_PRIORITY_DROP,"Tracked_New",
             AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK, tableId, write );
-        programConntrackDropRule(dpnId, AclConstants.CT_STATE_NEW_PRIORITY_DROP, "Tracked_Invalid",
+        programConntrackDropRule(dpnId, AclConstants.CT_STATE_TRACKED_EXIST_PRIORITY, "Tracked_Invalid",
             AclConstants.TRACKED_INV_CT_STATE, AclConstants.TRACKED_INV_CT_STATE_MASK, tableId, write );
 
     }
@@ -515,12 +477,13 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
      * Adds the rule to forward the packets known packets.
      *
      * @param dpId the dpId
-     * @param lportTag the lport tag
      * @param priority the priority of the flow
      * @param flowId the flowId
      * @param conntrackState the conntrack state of the packets thats should be
      *        send
      * @param conntrackMask the conntrack mask
+     * @param dispatcherTableId the dispatcher table id
+     * @param tableId the table id
      * @param addOrRemove whether to add or remove the flow
      */
     private void programConntrackForwardRule(BigInteger dpId, Integer priority, String flowId,
@@ -540,12 +503,12 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
      * Adds the rule to drop the unknown/invalid packets .
      *
      * @param dpId the dpId
-     * @param lportTag the lport tag
      * @param priority the priority of the flow
      * @param flowId the flowId
      * @param conntrackState the conntrack state of the packets thats should be
      *        send
      * @param conntrackMask the conntrack mask
+     * @param tableId the table id
      * @param addOrRemove whether to add or remove the flow
      */
     private void programConntrackDropRule(BigInteger dpId, Integer priority, String flowId,
@@ -555,8 +518,8 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
 
         List<InstructionInfo> instructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
-        instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+        actionsInfos.add(new ActionDrop());
+        instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
         flowId = "Fixed_Conntrk_NewDrop_" + dpId + "_" + flowId + tableId;
         syncFlow(dpId, tableId, flowId, priority, "ACL", 0, 0,
                 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
@@ -566,12 +529,13 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
      * Gets the dispatcher table resubmit instructions.
      *
      * @param actionsInfos the actions infos
+     * @param dispatcherTableId the dispatcher table id
      * @return the instructions for dispatcher table resubmit
      */
     private List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos,
                                                                          short dispatcherTableId) {
         List<InstructionInfo> instructions = new ArrayList<>();
-        actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
+        actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
         instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
         return instructions;
     }