[ONOS-4612]Update SFC flows inline with the Official OVS NSH patch

Change-Id: If58517841096a939860d88aa78eca7cae46b9935
diff --git a/apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/FlowClassifierInstallerImplTest.java b/apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/SfcFlowRuleInstallerImplTest.java
similarity index 61%
rename from apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/FlowClassifierInstallerImplTest.java
rename to apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/SfcFlowRuleInstallerImplTest.java
index a51ea21..61ad4e8 100644
--- a/apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/FlowClassifierInstallerImplTest.java
+++ b/apps/vtn/sfcmgr/src/test/java/org/onosproject/sfc/installer/impl/SfcFlowRuleInstallerImplTest.java
@@ -18,48 +18,74 @@
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.instanceOf;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
+import static org.onosproject.net.Device.Type.SWITCH;
+import static org.onosproject.net.Port.Type.COPPER;
 
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.junit.Test;
+import org.onlab.packet.ChassisId;
+import org.onlab.packet.IPv4;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Annotations;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.DefaultPort;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.HostLocation;
 import org.onosproject.net.NshServicePathId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.device.DeviceServiceAdapter;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.PortCriterion;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
+import org.onosproject.net.flowobjective.ForwardingObjective;
 import org.onosproject.net.host.HostService;
 import org.onosproject.net.host.HostServiceAdapter;
+import org.onosproject.net.provider.ProviderId;
 import org.onosproject.sfc.util.FlowClassifierAdapter;
 import org.onosproject.sfc.util.FlowObjectiveAdapter;
 import org.onosproject.sfc.util.MockDriverHandler;
 import org.onosproject.sfc.util.PortPairAdapter;
 import org.onosproject.sfc.util.PortPairGroupAdapter;
+import org.onosproject.sfc.util.TenantNetworkAdapter;
 import org.onosproject.sfc.util.VirtualPortAdapter;
 import org.onosproject.sfc.util.VtnRscAdapter;
 import org.onosproject.vtnrsc.AllowedAddressPair;
 import org.onosproject.vtnrsc.BindingHostId;
+import org.onosproject.vtnrsc.DefaultFiveTuple;
 import org.onosproject.vtnrsc.DefaultFlowClassifier;
 import org.onosproject.vtnrsc.DefaultPortChain;
 import org.onosproject.vtnrsc.DefaultPortPair;
 import org.onosproject.vtnrsc.DefaultPortPairGroup;
+import org.onosproject.vtnrsc.DefaultTenantNetwork;
 import org.onosproject.vtnrsc.DefaultVirtualPort;
+import org.onosproject.vtnrsc.FiveTuple;
 import org.onosproject.vtnrsc.FixedIp;
 import org.onosproject.vtnrsc.FlowClassifier;
 import org.onosproject.vtnrsc.FlowClassifierId;
+import org.onosproject.vtnrsc.LoadBalanceId;
+import org.onosproject.vtnrsc.PhysicalNetwork;
 import org.onosproject.vtnrsc.PortChain;
 import org.onosproject.vtnrsc.PortChainId;
 import org.onosproject.vtnrsc.PortPair;
@@ -67,8 +93,10 @@
 import org.onosproject.vtnrsc.PortPairGroupId;
 import org.onosproject.vtnrsc.PortPairId;
 import org.onosproject.vtnrsc.SecurityGroup;
+import org.onosproject.vtnrsc.SegmentationId;
 import org.onosproject.vtnrsc.SubnetId;
 import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.TenantNetwork;
 import org.onosproject.vtnrsc.TenantNetworkId;
 import org.onosproject.vtnrsc.VirtualPort;
 import org.onosproject.vtnrsc.VirtualPortId;
@@ -76,25 +104,30 @@
 import org.onosproject.vtnrsc.portpair.PortPairService;
 import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
 import org.onosproject.vtnrsc.service.VtnRscService;
+import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
 import org.onosproject.vtnrsc.virtualport.VirtualPortService;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
-public class FlowClassifierInstallerImplTest {
+public class SfcFlowRuleInstallerImplTest {
 
     FlowObjectiveService flowObjectiveService = new FlowObjectiveAdapter();
-    DeviceService deviceService = new DeviceServiceAdapter();
+    DeviceService deviceService = new DeviceServiceAdapter(createPortList());
+
     HostService hostService = new HostServiceAdapter();
     VirtualPortService virtualPortService = new VirtualPortAdapter();
     VtnRscService vtnRscService = new VtnRscAdapter();
     PortPairService portPairService = new PortPairAdapter();
     PortPairGroupService portPairGroupService = new PortPairGroupAdapter();
     FlowClassifierService flowClassifierService = new FlowClassifierAdapter();
+    TenantNetworkService tenantNetworkService = new TenantNetworkAdapter();
 
     final DriverService driverService = createMock(DriverService.class);
 
+    private final String networkIdStr = "123";
+
     final PortChainId portChainId = PortChainId.of("78888888-fc23-aeb6-f44b-56dc5e2fb3ae");
     final TenantId tenantId = TenantId.tenantId("1");
     final String name = "PortChain";
@@ -115,7 +148,6 @@
     final String ingress = "d3333333-24fc-4fae-af4b-321c5e2eb3d1";
     final String egress = "a4444444-4a56-2a6e-cd3a-9dee4e2ec345";
 
-
     final String ppgName = "PortPairGroup";
     final String ppgDescription = "PortPairGroup";
     final List<PortPairId> portPairList = new LinkedList<PortPairId>();
@@ -127,7 +159,21 @@
 
     final DriverHandler driverHandler = new MockDriverHandler();
 
-    private PortPair createPortPair(PortPairId  ppId) {
+    private List<Port> createPortList() {
+        List<Port> portList = Lists.newArrayList();
+        long sp1 = 1_000_000;
+        ProviderId pid = new ProviderId("of", "foo");
+        ChassisId cid = new ChassisId();
+        Device device = new DefaultDevice(pid, deviceId, SWITCH, "whitebox", "1.1.x", "3.9.1", "43311-12345", cid);
+        Annotations annotations = DefaultAnnotations
+                .builder()
+                .set(AnnotationKeys.PORT_NAME, "vxlan-0.0.0.0").build();
+        Port p1 = new DefaultPort(device, PortNumber.ANY, true, COPPER, sp1, annotations);
+        portList.add(p1);
+        return portList;
+    }
+
+    private PortPair createPortPair(PortPairId ppId) {
         DefaultPortPair.Builder portPairBuilder = new DefaultPortPair.Builder();
         PortPair portPair = portPairBuilder.setId(ppId).setName(ppName).setTenantId(tenantId)
                 .setDescription(ppDescription).setIngress(ingress).setEgress(egress).build();
@@ -193,7 +239,7 @@
         return flowClassifier;
     }
 
-    private VirtualPort createVirtualPort() {
+    private VirtualPort createVirtualPort(VirtualPortId id) {
         Set<FixedIp> fixedIps;
         Map<String, String> propertyMap;
         Set<AllowedAddressPair> allowedAddressPairs;
@@ -201,14 +247,13 @@
 
         String macAddressStr = "fa:12:3e:56:ee:a2";
         String ipAddress = "10.1.1.1";
-        String tenantNetworkId = "1234567";
         String subnet = "1212";
         String hostIdStr = "fa:e2:3e:56:ee:a2";
         String deviceOwner = "james";
         propertyMap = Maps.newHashMap();
         propertyMap.putIfAbsent("deviceOwner", deviceOwner);
 
-        TenantNetworkId networkId = TenantNetworkId.networkId(tenantNetworkId);
+        TenantNetworkId networkId = TenantNetworkId.networkId(networkIdStr);
         MacAddress macAddress = MacAddress.valueOf(macAddressStr);
         BindingHostId bindingHostId = BindingHostId.bindingHostId(hostIdStr);
         FixedIp fixedIp = FixedIp.fixedIp(SubnetId.subnetId(subnet),
@@ -222,7 +267,7 @@
                                     MacAddress.valueOf(macAddressStr));
         allowedAddressPairs.add(allowedAddressPair);
 
-        VirtualPort d1 = new DefaultVirtualPort(id1, networkId, true,
+        VirtualPort d1 = new DefaultVirtualPort(id, networkId, true,
                                                 propertyMap,
                                                 VirtualPort.State.ACTIVE,
                                                 macAddress, tenantId, deviceId,
@@ -236,7 +281,7 @@
     public void testInstallFlowClassifier() {
 
         ApplicationId appId = new DefaultApplicationId(1, "test");
-        FlowClassifierInstallerImpl flowClassifierInstaller = new FlowClassifierInstallerImpl();
+        SfcFlowRuleInstallerImpl flowClassifierInstaller = new SfcFlowRuleInstallerImpl();
         flowClassifierInstaller.virtualPortService = virtualPortService;
         flowClassifierInstaller.vtnRscService = vtnRscService;
         flowClassifierInstaller.portPairService = portPairService;
@@ -261,7 +306,7 @@
         flowClassifierService.createFlowClassifier(fc2);
 
         List<VirtualPort> virtualPortList = Lists.newArrayList();
-        virtualPortList.add(createVirtualPort());
+        virtualPortList.add(createVirtualPort(VirtualPortId.portId(ingress)));
         virtualPortService.createPorts(virtualPortList);
 
         expect(driverService.createHandler(deviceId)).andReturn(driverHandler).anyTimes();
@@ -271,4 +316,102 @@
 
         assertThat(connectPoint, is(HostLocation.NONE));
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testInstallLoadBalancedFlowRules() {
+        ApplicationId appId = new DefaultApplicationId(1, "test");
+        SfcFlowRuleInstallerImpl flowRuleInstaller = new SfcFlowRuleInstallerImpl();
+        flowRuleInstaller.virtualPortService = virtualPortService;
+        flowRuleInstaller.vtnRscService = vtnRscService;
+        flowRuleInstaller.portPairService = portPairService;
+        flowRuleInstaller.portPairGroupService = portPairGroupService;
+        flowRuleInstaller.flowClassifierService = flowClassifierService;
+        flowRuleInstaller.driverService = driverService;
+        flowRuleInstaller.deviceService = deviceService;
+        flowRuleInstaller.hostService = hostService;
+        flowRuleInstaller.flowObjectiveService = flowObjectiveService;
+        flowRuleInstaller.tenantNetworkService = tenantNetworkService;
+        flowRuleInstaller.appId = appId;
+
+        final PortChain portChain = createPortChain();
+
+        final String ppName1 = "PortPair1";
+        final String ppDescription1 = "PortPair1";
+        final String ingress1 = "d3333333-24fc-4fae-af4b-321c5e2eb3d1";
+        final String egress1 = "a4444444-4a56-2a6e-cd3a-9dee4e2ec345";
+        DefaultPortPair.Builder portPairBuilder = new DefaultPortPair.Builder();
+        PortPair portPair1 = portPairBuilder.setId(portPairId1).setName(ppName1).setTenantId(tenantId)
+                .setDescription(ppDescription1).setIngress(ingress1).setEgress(egress1).build();
+
+        final String ppName2 = "PortPair2";
+        final String ppDescription2 = "PortPair2";
+        final String ingress2 = "d5555555-24fc-4fae-af4b-321c5e2eb3d1";
+        final String egress2 = "a6666666-4a56-2a6e-cd3a-9dee4e2ec345";
+        PortPair portPair2 = portPairBuilder.setId(portPairId2).setName(ppName2).setTenantId(tenantId)
+                .setDescription(ppDescription2).setIngress(ingress2).setEgress(egress2).build();
+
+        portPairService.createPortPair(portPair1);
+        portPairService.createPortPair(portPair2);
+
+        FlowClassifier fc1 = createFlowClassifier(flowClassifierId1);
+        FlowClassifier fc2 = createFlowClassifier(flowClassifierId2);
+        flowClassifierService.createFlowClassifier(fc1);
+        flowClassifierService.createFlowClassifier(fc2);
+
+        NshServicePathId nshSpiId = NshServicePathId.of(10);
+        FiveTuple fiveTuple = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("3.3.3.3"))
+                .setIpDst(IpAddress.valueOf("4.4.4.4"))
+                .setPortSrc(PortNumber.portNumber(1500))
+                .setPortDst(PortNumber.portNumber(2000))
+                .setProtocol(IPv4.PROTOCOL_UDP)
+                .setTenantId(TenantId.tenantId("bbb"))
+                .build();
+        LoadBalanceId id = LoadBalanceId.of((byte) 1);
+
+        List<PortPairId> path = Lists.newArrayList();
+        path.add(portPairId1);
+        path.add(portPairId2);
+
+        List<VirtualPort> virtualPortList = Lists.newArrayList();
+        virtualPortList.add(createVirtualPort(VirtualPortId.portId(ingress1)));
+        virtualPortList.add(createVirtualPort(VirtualPortId.portId(egress1)));
+        virtualPortList.add(createVirtualPort(VirtualPortId.portId(ingress2)));
+        virtualPortList.add(createVirtualPort(VirtualPortId.portId(egress2)));
+        virtualPortService.createPorts(virtualPortList);
+
+        portChain.addLoadBalancePath(fiveTuple, id, path);
+
+        String physicalNetworkStr = "1234";
+        String segmentationIdStr = "1";
+        SegmentationId segmentationID = SegmentationId
+                .segmentationId(segmentationIdStr);
+        TenantNetworkId networkid1 = TenantNetworkId.networkId(networkIdStr);
+        PhysicalNetwork physicalNetwork = PhysicalNetwork
+                .physicalNetwork(physicalNetworkStr);
+        TenantNetwork p1 = new DefaultTenantNetwork(networkid1, name, false,
+                                                    TenantNetwork.State.ACTIVE,
+                                                    false, tenantId, false,
+                                                    TenantNetwork.Type.LOCAL,
+                                                    physicalNetwork,
+                                                    segmentationID);
+        tenantNetworkService.createNetworks(Collections.singletonList(p1));
+
+        expect(driverService.createHandler(deviceId)).andReturn(driverHandler).anyTimes();
+        replay(driverService);
+
+        flowRuleInstaller.installLoadBalancedFlowRules(portChain, fiveTuple, nshSpiId);
+
+        ForwardingObjective forObj = ((FlowObjectiveAdapter) flowObjectiveService).forwardingObjective();
+
+        // Check for Selector
+        assertThat(forObj.selector().getCriterion(Criterion.Type.IN_PORT), instanceOf(PortCriterion.class));
+
+        // Check for treatment
+        List<Instruction> instructions = forObj.treatment().allInstructions();
+        for (Instruction instruction : instructions) {
+            if (instruction.type() == Instruction.Type.OUTPUT) {
+                assertThat(((OutputInstruction) instruction).port(), is(PortNumber.P0));
+            }
+        }
+    }
+}