fixed FlowEntry parsing to take table ids

Change-Id: I1dc6716ee299f1de7608663135a83346800068bf
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowEntryBuilder.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowEntryBuilder.java
index fbdc9fa..c1253a2 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowEntryBuilder.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowEntryBuilder.java
@@ -33,7 +33,6 @@
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.FlowEntry.FlowEntryState;
 import org.onosproject.net.flow.FlowRule;
-import org.onosproject.net.flow.FlowRule.Type;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.openflow.controller.Dpid;
@@ -55,6 +54,7 @@
 import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
+import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
 import org.projectfloodlight.openflow.protocol.match.Match;
 import org.projectfloodlight.openflow.protocol.match.MatchField;
@@ -92,9 +92,8 @@
     public enum FlowType { STAT, REMOVED, MOD }
 
     private final FlowType type;
-    private Type tableType = FlowRule.Type.DEFAULT;
 
-    public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) {
+    public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
         this.stat = entry;
         this.match = entry.getMatch();
         this.instructions = getInstructions(entry);
@@ -102,10 +101,9 @@
         this.removed = null;
         this.flowMod = null;
         this.type = FlowType.STAT;
-        this.tableType = tableType;
     }
 
-    public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed, Type tableType) {
+    public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
         this.match = removed.getMatch();
         this.removed = removed;
 
@@ -114,11 +112,10 @@
         this.stat = null;
         this.flowMod = null;
         this.type = FlowType.REMOVED;
-        this.tableType = tableType;
 
     }
 
-    public FlowEntryBuilder(Dpid dpid, OFFlowMod fm, Type tableType) {
+    public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
         this.match = fm.getMatch();
         this.dpid = dpid;
         this.instructions = getInstructions(fm);
@@ -126,33 +123,49 @@
         this.flowMod = fm;
         this.stat = null;
         this.removed = null;
-        this.tableType = tableType;
     }
 
     public FlowEntry build(FlowEntryState... state) {
         FlowRule rule;
         switch (this.type) {
             case STAT:
-                rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
-                                      buildSelector(), buildTreatment(), stat.getPriority(),
-                                      stat.getCookie().getValue(), stat.getIdleTimeout(), false,
-                                      tableType);
+                rule = DefaultFlowRule.builder()
+                        .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
+                        .withSelector(buildSelector())
+                        .withTreatment(buildTreatment())
+                        .withPriority(stat.getPriority())
+                        .makeTemporary(stat.getIdleTimeout())
+                        .withCookie(stat.getCookie().getValue())
+                        .forTable(stat.getTableId().getValue())
+                        .build();
+
                 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
                                       stat.getDurationSec(), stat.getPacketCount().getValue(),
                                       stat.getByteCount().getValue());
             case REMOVED:
-                rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
-                                      buildSelector(), null, removed.getPriority(),
-                                      removed.getCookie().getValue(), removed.getIdleTimeout(), false,
-                                      tableType);
+                rule = DefaultFlowRule.builder()
+                        .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
+                        .withSelector(buildSelector())
+                        .withPriority(removed.getPriority())
+                        .makeTemporary(removed.getIdleTimeout())
+                        .withCookie(removed.getCookie().getValue())
+                        .forTable(removed.getTableId().getValue())
+                        .build();
+
                 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
                                       removed.getPacketCount().getValue(), removed.getByteCount().getValue());
             case MOD:
                 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
-                rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
-                                      buildSelector(), buildTreatment(), flowMod.getPriority(),
-                                      flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false,
-                                      tableType);
+                rule = DefaultFlowRule.builder()
+                        .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
+                        .withSelector(buildSelector())
+                        .withTreatment(buildTreatment())
+                        .withPriority(flowMod.getPriority())
+                        .makeTemporary(flowMod.getIdleTimeout())
+                        .withCookie(flowMod.getCookie().getValue())
+                        .forTable(flowMod.getTableId().getValue())
+                        .build();
+
                 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
             default:
                 log.error("Unknown flow type : {}", this.type);
@@ -202,7 +215,8 @@
         for (OFInstruction in : instructions) {
             switch (in.getType()) {
                 case GOTO_TABLE:
-                    builder.transition(tableType);
+                    builder.transition(((int) ((OFInstructionGotoTable) in)
+                            .getTableId().getValue()));
                     break;
                 case WRITE_METADATA:
                     break;
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
index 5b4e286..add2752 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
@@ -144,15 +144,9 @@
 
     private void applyRule(FlowRule flowRule) {
         OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
-        if (flowRule.tableId() == 0) {
-            sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
+        sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
                     Optional.empty()).buildFlowAdd());
-        } else {
-            OpenFlowSwitch.TableType type = getTableType(flowRule.tableId());
-            sw.transformAndSendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
-                                              Optional.empty()).buildFlowAdd(),
-                                              type);
-        }
+
     }
 
 
@@ -166,13 +160,9 @@
 
     private void removeRule(FlowRule flowRule) {
         OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
-        if (flowRule.tableId() == 0) {
-            sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
+
+        sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
                     Optional.empty()).buildFlowDel());
-        } else {
-            sw.transformAndSendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
-                    Optional.empty()).buildFlowDel(), getTableType(flowRule.tableId()));
-        }
     }
 
     @Override
@@ -211,11 +201,6 @@
                               fbe.operator(), fbe);
                     continue;
                 }
-            /*if (fbe.target().tableId() == 0) {
-                sw.sendMsg(mod);
-            } else {
-                sw.transformAndSendMsg(mod, getTableType(fbe.target().tableId()));
-            }*/
             sw.sendMsg(mod);
         }
         OFBarrierRequest.Builder builder = sw.factory()
@@ -224,58 +209,6 @@
         sw.sendMsg(builder.build());
     }
 
-    private OpenFlowSwitch.TableType getTableType(int type) {
-        switch (FlowRule.Type.values()[type]) {
-
-            case DEFAULT:
-                return OpenFlowSwitch.TableType.NONE;
-            case IP:
-                return OpenFlowSwitch.TableType.IP;
-            case MPLS:
-                return OpenFlowSwitch.TableType.MPLS;
-            case ACL:
-                return OpenFlowSwitch.TableType.ACL;
-            case VLAN_MPLS:
-                return OpenFlowSwitch.TableType.VLAN_MPLS;
-            case VLAN:
-                return OpenFlowSwitch.TableType.VLAN;
-            case ETHER:
-                return OpenFlowSwitch.TableType.ETHER;
-            case COS:
-                return OpenFlowSwitch.TableType.COS;
-            case FIRST:
-                return OpenFlowSwitch.TableType.FIRST;
-            default:
-                return OpenFlowSwitch.TableType.NONE;
-        }
-    }
-
-    private FlowRule.Type getType(OpenFlowSwitch.TableType tableType) {
-        switch (tableType) {
-
-        case NONE:
-            return FlowRule.Type.DEFAULT;
-        case IP:
-            return FlowRule.Type.IP;
-        case MPLS:
-            return FlowRule.Type.MPLS;
-        case ACL:
-            return FlowRule.Type.ACL;
-        case VLAN_MPLS:
-            return FlowRule.Type.VLAN_MPLS;
-        case VLAN:
-            return FlowRule.Type.VLAN;
-        case ETHER:
-            return FlowRule.Type.ETHER;
-        case COS:
-            return FlowRule.Type.COS;
-        case FIRST:
-            return FlowRule.Type.FIRST;
-        default:
-            return FlowRule.Type.DEFAULT;
-        }
-    }
-
 
     private class InternalFlowProvider
             implements OpenFlowSwitchListener, OpenFlowEventListener {
@@ -311,8 +244,7 @@
                 case FLOW_REMOVED:
                     OFFlowRemoved removed = (OFFlowRemoved) msg;
 
-                    FlowEntry fr = new FlowEntryBuilder(dpid, removed,
-                                                        getType(sw.getTableType(removed.getTableId()))).build();
+                    FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
                     providerService.flowRemoved(fr);
                     break;
                 case STATS_REPLY:
@@ -343,8 +275,7 @@
                             OFFlowMod fm = (OFFlowMod) m;
                             InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
                             if (entry != null) {
-                                entry.appendFailure(new FlowEntryBuilder(dpid, fm,
-                                                                         getType(sw.getTableType(fm.getTableId())))
+                                entry.appendFailure(new FlowEntryBuilder(dpid, fm)
                                                                          .build());
                             } else {
                                 log.error("No matching batch for this error: {}", error);
@@ -377,9 +308,7 @@
             OpenFlowSwitch sw = controller.getSwitch(dpid);
 
             List<FlowEntry> flowEntries = replies.getEntries().stream()
-                    .map(entry -> new FlowEntryBuilder(dpid, entry,
-                                        getType(sw.getTableType(entry.getTableId())))
-                                        .build())
+                    .map(entry -> new FlowEntryBuilder(dpid, entry).build())
                     .collect(Collectors.toList());
 
             providerService.pushFlowMetrics(did, flowEntries);