[CORD-578] MPLS ECMP configurable

Changes:
- Adds MPLS-ECMP option to SR configuration;
- Updates the json examples;
- Implements unit tests to verify the expected behaviors;

Change-Id: I6a7f5d34161be7c85ecb76c9a09288d960aad3cb
diff --git a/src/main/java/org/onosproject/segmentrouting/AppConfigHandler.java b/src/main/java/org/onosproject/segmentrouting/AppConfigHandler.java
index 841ad2f..c24c3ca 100644
--- a/src/main/java/org/onosproject/segmentrouting/AppConfigHandler.java
+++ b/src/main/java/org/onosproject/segmentrouting/AppConfigHandler.java
@@ -58,7 +58,7 @@
      * @param event network config added event
      */
     protected void processAppConfigAdded(NetworkConfigEvent event) {
-        log.info("Processing vRouter CONFIG_ADDED");
+        log.info("Processing AppConfig CONFIG_ADDED");
         SegmentRoutingAppConfig config = (SegmentRoutingAppConfig) event.config().get();
         deviceService.getAvailableDevices().forEach(device -> {
             populateVRouter(device.id(), getMacAddresses(config));
@@ -71,7 +71,7 @@
      * @param event network config updated event
      */
     protected void processAppConfigUpdated(NetworkConfigEvent event) {
-        log.info("Processing vRouter CONFIG_UPDATED");
+        log.info("Processing AppConfig CONFIG_UPDATED");
         SegmentRoutingAppConfig config = (SegmentRoutingAppConfig) event.config().get();
         SegmentRoutingAppConfig prevConfig = (SegmentRoutingAppConfig) event.prevConfig().get();
         deviceService.getAvailableDevices().forEach(device -> {
@@ -96,7 +96,7 @@
      * @param event network config removed event
      */
     protected void processAppConfigRemoved(NetworkConfigEvent event) {
-        log.info("Processing vRouter CONFIG_REMOVED");
+        log.info("Processing AppConfig CONFIG_REMOVED");
         SegmentRoutingAppConfig prevConfig = (SegmentRoutingAppConfig) event.prevConfig().get();
         deviceService.getAvailableDevices().forEach(device -> {
             revokeVRouter(device.id(), getMacAddresses(prevConfig));
diff --git a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index bd89102..81fb220 100644
--- a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -468,6 +468,17 @@
     }
 
     /**
+     * Returns the MPLS-ECMP configuration.
+     *
+     * @return MPLS-ECMP value
+     */
+    public boolean getMplsEcmp() {
+        SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
+                .getConfig(this.appId, SegmentRoutingAppConfig.class);
+        return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
+    }
+
+    /**
      * Returns the tunnel object with the tunnel ID.
      *
      * @param tunnelId Tunnel ID
diff --git a/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java b/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
index 0eb2c8e..3fe7e4b 100644
--- a/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
+++ b/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
@@ -40,17 +40,39 @@
     private static final String SUPPRESS_HOST_BY_PORT = "suppressHostByPort";
     // TODO We might want to move SUPPRESS_HOST_BY_PROVIDER to Component Config
     private static final String SUPPRESS_HOST_BY_PROVIDER = "suppressHostByProvider";
+    private static final String MPLS_ECMP = "MPLS-ECMP";
 
     @Override
     public boolean isValid() {
         return hasOnlyFields(VROUTER_MACS, VROUTER_ID, SUPPRESS_SUBNET,
-                SUPPRESS_HOST_BY_PORT, SUPPRESS_HOST_BY_PROVIDER) &&
+                SUPPRESS_HOST_BY_PORT, SUPPRESS_HOST_BY_PROVIDER, MPLS_ECMP) &&
                 vRouterMacs() != null && vRouterId() != null &&
                 suppressSubnet() != null && suppressHostByPort() != null &&
                 suppressHostByProvider() != null;
     }
 
     /**
+     * Gets MPLS-ECMP configuration from the config.
+     *
+     * @return the configuration of MPLS-ECMP. If it is not
+     *         specified, the default behavior is false.
+     */
+    public boolean mplsEcmp() {
+        return get(MPLS_ECMP, false);
+    }
+
+    /**
+     * Sets MPLS-ECMP to the config.
+     *
+     * @param mplsEcmp the MPLS-ECMP configuration
+     * @return this {@link SegmentRoutingAppConfig}
+     */
+    public SegmentRoutingAppConfig setMplsEcmp(boolean mplsEcmp) {
+        object.put(MPLS_ECMP, mplsEcmp);
+        return this;
+    }
+
+    /**
      * Gets vRouters from the config.
      *
      * @return Set of vRouter MAC addresses, empty is not specified,
@@ -276,6 +298,7 @@
                 .add("suppressSubnet", suppressSubnet())
                 .add("suppressHostByPort", suppressHostByPort())
                 .add("suppressHostByProvider", suppressHostByProvider())
+                .add("mplsEcmp", mplsEcmp())
                 .toString();
     }
 }
diff --git a/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java b/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java
index 0f3a431..aa8896c 100644
--- a/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java
+++ b/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java
@@ -43,6 +43,7 @@
 public class SegmentRoutingAppConfigTest {
     private SegmentRoutingAppConfig config;
     private SegmentRoutingAppConfig invalidConfig;
+    private SegmentRoutingAppConfig mplsEcmpConfig;
 
     private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01");
     private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02");
@@ -67,18 +68,23 @@
                 .getResourceAsStream("/app.json");
         InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
                 .getResourceAsStream("/app-invalid.json");
+        InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class
+                .getResourceAsStream("/app-ecmp.json");
 
         String key = SegmentRoutingManager.APP_NAME;
         ApplicationId subject = new TestApplicationId(key);
         ObjectMapper mapper = new ObjectMapper();
         JsonNode jsonNode = mapper.readTree(jsonStream);
         JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
+        JsonNode mplsEcmpJsonNode = mapper.readTree(mplsEcmpJsonStream);
         ConfigApplyDelegate delegate = new MockDelegate();
 
         config = new SegmentRoutingAppConfig();
         config.init(subject, key, jsonNode, mapper, delegate);
         invalidConfig = new SegmentRoutingAppConfig();
         invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
+        mplsEcmpConfig = new SegmentRoutingAppConfig();
+        mplsEcmpConfig.init(subject, key, mplsEcmpJsonNode, mapper, delegate);
     }
 
     /**
@@ -90,6 +96,49 @@
     public void testIsValid() throws Exception {
         assertTrue(config.isValid());
         assertFalse(invalidConfig.isValid());
+        assertTrue(mplsEcmpConfig.isValid());
+    }
+
+    /**
+     * Test MPLS-ECMP default getter. By-default
+     * MPLS-ECMPS is false.
+     */
+    @Test
+    public void testDefaultMplsEcmp() {
+        boolean mplsEcmp = config.mplsEcmp();
+        assertThat(mplsEcmp, is(false));
+    }
+
+    /**
+     * Test MPLS-ECMP getter.
+     */
+    @Test
+    public void testMplsEcmp() {
+        boolean mplsEcmp = mplsEcmpConfig.mplsEcmp();
+        assertThat(mplsEcmp, is(true));
+    }
+
+    /**
+     * Test MPLS-ECMP setter.
+     */
+    @Test
+    public void testSetMplsEcmp() {
+        /*
+         * In the config the value is not set.
+         */
+        boolean mplsEcmp = config.mplsEcmp();
+        assertThat(mplsEcmp, is(false));
+        config.setMplsEcmp(true);
+        mplsEcmp = config.mplsEcmp();
+        assertThat(mplsEcmp, is(true));
+        /*
+         * In the mplsEcmpConfig the value is true,
+         */
+        mplsEcmp = mplsEcmpConfig.mplsEcmp();
+        assertThat(mplsEcmp, is(true));
+        config.setMplsEcmp(false);
+        mplsEcmp = config.mplsEcmp();
+        assertThat(mplsEcmp, is(false));
     }
 
     /**
@@ -246,4 +295,4 @@
         public void onApply(Config config) {
         }
     }
-}
\ No newline at end of file
+}
diff --git a/src/test/resources/app-ecmp.json b/src/test/resources/app-ecmp.json
new file mode 100644
index 0000000..5c09001
--- /dev/null
+++ b/src/test/resources/app-ecmp.json
@@ -0,0 +1,20 @@
+{
+  "vRouterMacs" : [
+      "00:00:00:00:00:01",
+      "00:00:00:00:00:02"
+  ],
+  "vRouterId" : "of:1",
+  "suppressSubnet" : [
+      "of:1/1",
+      "of:1/2"
+  ],
+  "suppressHostByPort" : [
+      "of:1/1",
+      "of:1/2"
+  ],
+  "suppressHostByProvider" : [
+      "org.onosproject.provider.host",
+      "org.onosproject.netcfghost"
+  ],
+  "MPLS-ECMP" : true
+}
diff --git a/src/test/resources/device.json b/src/test/resources/device.json
index 133f6d4..247d7f5 100644
--- a/src/test/resources/device.json
+++ b/src/test/resources/device.json
@@ -8,4 +8,4 @@
     { "adjSid" : 100, "ports" : [2, 3] },
     { "adjSid" : 200, "ports" : [4, 5] }
   ]
-}
\ No newline at end of file
+}
diff --git a/src/test/resources/xconnect-invalid.json b/src/test/resources/xconnect-invalid.json
index aa35f31..e468271 100644
--- a/src/test/resources/xconnect-invalid.json
+++ b/src/test/resources/xconnect-invalid.json
@@ -15,4 +15,4 @@
       "ports": [4, 5, 6]
     }
   ]
-}
\ No newline at end of file
+}
diff --git a/src/test/resources/xconnect.json b/src/test/resources/xconnect.json
index ab9cfe6..ebd61b3 100644
--- a/src/test/resources/xconnect.json
+++ b/src/test/resources/xconnect.json
@@ -16,4 +16,4 @@
       "ports": [4, 5]
     }
   ]
-}
\ No newline at end of file
+}