Fixing FlowRule priority in intent compilers

Change-Id: I13998e88d2a116017e87c019f4829101db6c6b6b
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
index cd7200a..76c5736 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
@@ -24,7 +24,6 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
-import org.onosproject.core.DefaultGroupId;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
@@ -123,9 +122,14 @@
                 treatment = defaultTreatment;
             }
 
-            DefaultFlowRule rule = new DefaultFlowRule(deviceId, selector, treatment, 123, appId,
-                    new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true, null);
-
+            FlowRule rule = DefaultFlowRule.builder()
+                                .forDevice(deviceId)
+                                .withSelector(selector)
+                                .withTreatment(treatment)
+                                .withPriority(intent.priority())
+                                .fromApp(appId)
+                                .makePermanent()
+                                .build();
             rules.add(rule);
         }
 
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
index 62e526f..926b8e1 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
@@ -188,13 +188,13 @@
 
         // Create optical circuit intent
         List<FlowRule> rules = new LinkedList<>();
-        rules.add(connectPorts(src, connIntent.getSrc()));
-        rules.add(connectPorts(connIntent.getDst(), dst));
+        rules.add(connectPorts(src, connIntent.getSrc(), intent.priority()));
+        rules.add(connectPorts(connIntent.getDst(), dst, intent.priority()));
 
         // Create flow rules for reverse path
         if (intent.isBidirectional()) {
-            rules.add(connectPorts(connIntent.getSrc(), src));
-            rules.add(connectPorts(dst, connIntent.getDst()));
+            rules.add(connectPorts(connIntent.getSrc(), src, intent.priority()));
+            rules.add(connectPorts(dst, connIntent.getDst(), intent.priority()));
         }
 
         circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
@@ -348,7 +348,7 @@
      * @param dst destination port
      * @return flow rules
      */
-    private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst) {
+    private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority) {
         checkArgument(src.deviceId().equals(dst.deviceId()));
 
         TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
@@ -363,7 +363,7 @@
                 .forDevice(src.deviceId())
                 .withSelector(selectorBuilder.build())
                 .withTreatment(treatmentBuilder.build())
-                .withPriority(100)
+                .withPriority(priority)
                 .fromApp(appId)
                 .makePermanent()
                 .build();
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
index 4cc93bb..61a2ac7 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
@@ -111,7 +111,7 @@
                     .forDevice(current.deviceId())
                     .withSelector(selectorBuilder.build())
                     .withTreatment(treatmentBuilder.build())
-                    .withPriority(100)
+                    .withPriority(intent.priority())
                     .fromApp(appId)
                     .makePermanent()
                     .build();
@@ -132,7 +132,7 @@
                 .forDevice(intent.dst().deviceId())
                 .withSelector(selectorBuilder.build())
                 .withTreatment(treatmentLast.build())
-                .withPriority(100)
+                .withPriority(intent.priority())
                 .fromApp(appId)
                 .makePermanent()
                 .build();
@@ -163,7 +163,7 @@
                     .forDevice(current.deviceId())
                     .withSelector(selectorBuilder.build())
                     .withTreatment(treatmentBuilder.build())
-                    .withPriority(100)
+                    .withPriority(intent.priority())
                     .fromApp(appId)
                     .makePermanent()
                     .build();
@@ -184,7 +184,7 @@
                 .forDevice(intent.src().deviceId())
                 .withSelector(selectorBuilder.build())
                 .withTreatment(treatmentLast.build())
-                .withPriority(100)
+                .withPriority(intent.priority())
                 .fromApp(appId)
                 .makePermanent()
                 .build();
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
index 90e3981..7add217 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
@@ -76,7 +76,9 @@
         for (int i = 0; i < links.size() - 1; i++) {
             ConnectPoint ingress = links.get(i).dst();
             ConnectPoint egress = links.get(i + 1).src();
-            FlowRule rule = createFlowRule(intent.selector(), intent.treatment(), ingress, egress, isLast(links, i));
+            FlowRule rule = createFlowRule(intent.selector(), intent.treatment(),
+                                           ingress, egress, intent.priority(),
+                                           isLast(links, i));
             rules.add(rule);
         }
 
@@ -84,7 +86,8 @@
     }
 
     private FlowRule createFlowRule(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
-                                    ConnectPoint ingress, ConnectPoint egress, boolean last) {
+                                    ConnectPoint ingress, ConnectPoint egress,
+                                    int priority, boolean last) {
         TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
                 .matchInPort(ingress.port())
                 .build();
@@ -101,7 +104,7 @@
                 .forDevice(ingress.deviceId())
                 .withSelector(selector)
                 .withTreatment(treatment)
-                .withPriority(123)
+                .withPriority(priority)
                 .fromApp(appId)
                 .makePermanent()
                 .build();
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
index f314fe9..c5fa371 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
@@ -81,7 +81,7 @@
     private LinkCollectionIntentCompiler sut;
 
     @Before
-    public void setUP() {
+    public void setUp() {
         sut = new LinkCollectionIntentCompiler();
         coreService = createMock(CoreService.class);
         expect(coreService.registerApplication("org.onosproject.net.intent"))
@@ -132,6 +132,7 @@
         assertThat(rule1.treatment(), is(
                 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
         ));
+        assertThat(rule1.priority(), is(intent.priority()));
 
         FlowRule rule2 = rules.stream()
                 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
@@ -143,6 +144,7 @@
         assertThat(rule2.treatment(), is(
                 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
         ));
+        assertThat(rule2.priority(), is(intent.priority()));
 
         FlowRule rule3 = rules.stream()
                 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
@@ -154,6 +156,7 @@
         assertThat(rule3.treatment(), is(
                 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
         ));
+        assertThat(rule3.priority(), is(intent.priority()));
 
         sut.deactivate();
     }
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java
index b773f0f..2f40b37 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java
@@ -50,6 +50,7 @@
 import static org.easymock.EasyMock.replay;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertEquals;
 import static org.onosproject.net.Link.Type.DIRECT;
 import static org.onosproject.net.NetTestTools.PID;
 import static org.onosproject.net.NetTestTools.connectPoint;
@@ -135,7 +136,12 @@
                 .findFirst()
                 .get();
 
+        rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
+                                           intent.priority(), rule.priority()));
+
         sut.deactivate();
     }
 
+    //TODO test bidirectional optical paths and verify rules
+
 }
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java
index 058b607..f07bf42 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java
@@ -75,6 +75,7 @@
     private final ConnectPoint d3p1 = connectPoint("s3", 1);
     private final ConnectPoint d3p0 = connectPoint("s3", 10);
     private final ConnectPoint d1p0 = connectPoint("s1", 10);
+    private static final int PRIORITY = 555;
 
     private final List<Link> links = Arrays.asList(
             createEdgeLink(d1p0, true),
@@ -102,6 +103,7 @@
                 .appId(APP_ID)
                 .selector(selector)
                 .treatment(treatment)
+                .priority(PRIORITY)
                 .path(new DefaultPath(pid, links, hops))
                 .build();
         intentExtensionService = createMock(IntentExtensionService.class);
@@ -141,6 +143,7 @@
                 is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build()));
         assertThat(rule1.treatment(),
                 is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build()));
+        assertThat(rule1.priority(), is(intent.priority()));
 
         FlowRule rule2 = rules.stream()
                 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
@@ -151,6 +154,7 @@
                 is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build()));
         assertThat(rule2.treatment(),
                 is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()));
+        assertThat(rule2.priority(), is(intent.priority()));
 
         FlowRule rule3 = rules.stream()
                 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
@@ -161,6 +165,7 @@
                 is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build()));
         assertThat(rule3.treatment(),
                 is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build()));
+        assertThat(rule3.priority(), is(intent.priority()));
 
         sut.deactivate();
     }