[CORD-1628] DHCP relay app configuration change for multiple servers

Change-Id: I13747d8e6451658dfd9344c428b6aad11bf5af62
(cherry picked from commit 483ac6f0689220425061056e748230efe6d8a1b2)
diff --git a/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/DhcpRelayManagerTest.java b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/DhcpRelayManagerTest.java
index 8c57c9f..8a47cf8 100644
--- a/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/DhcpRelayManagerTest.java
+++ b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/DhcpRelayManagerTest.java
@@ -40,6 +40,10 @@
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
+import org.onosproject.dhcprelay.api.DhcpHandler;
+import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig;
+import org.onosproject.dhcprelay.config.DhcpServerConfig;
+import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig;
 import org.onosproject.dhcprelay.store.DhcpRecord;
 import org.onosproject.dhcprelay.store.DhcpRelayStore;
 import org.onosproject.dhcprelay.store.DhcpRelayStoreEvent;
@@ -149,7 +153,7 @@
 
     // Components
     private static final ApplicationId APP_ID = TestApplicationId.create(DhcpRelayManager.DHCP_RELAY_APP);
-    private static final DhcpRelayConfig CONFIG = new MockDhcpRelayConfig();
+    private static final DefaultDhcpRelayConfig CONFIG = new MockDefaultDhcpRelayConfig();
     private static final Set<Interface> INTERFACES = ImmutableSet.of(
             CLIENT_INTERFACE,
             CLIENT2_INTERFACE,
@@ -167,10 +171,15 @@
         manager = new DhcpRelayManager();
         manager.cfgService = createNiceMock(NetworkConfigRegistry.class);
 
-        expect(manager.cfgService.getConfig(anyObject(), anyObject()))
+        expect(manager.cfgService.getConfig(APP_ID, DefaultDhcpRelayConfig.class))
                 .andReturn(CONFIG)
                 .anyTimes();
 
+        // TODO: add indirect test
+        expect(manager.cfgService.getConfig(APP_ID, IndirectDhcpRelayConfig.class))
+                .andReturn(null)
+                .anyTimes();
+
         manager.coreService = createNiceMock(CoreService.class);
         expect(manager.coreService.registerApplication(anyString()))
                 .andReturn(APP_ID).anyTimes();
@@ -200,6 +209,9 @@
         manager.v4Handler = v4Handler;
 
         // TODO: initialize v6 handler.
+        DhcpHandler v6Handler = createNiceMock(DhcpHandler.class);
+        manager.v6Handler = v6Handler;
+
         // properties
         Dictionary<String, Object> dictionary = createNiceMock(Dictionary.class);
         expect(dictionary.get("arpEnabled")).andReturn(true).anyTimes();
@@ -297,23 +309,27 @@
         assertArrayEquals(arp.getSenderHardwareAddress(), CLIENT_INTERFACE.mac().toBytes());
     }
 
-    private static class MockDhcpRelayConfig extends DhcpRelayConfig {
+    private static class MockDefaultDhcpRelayConfig extends DefaultDhcpRelayConfig {
         @Override
         public boolean isValid() {
             return true;
         }
 
         @Override
-        public ConnectPoint getDhcpServerConnectPoint() {
-            return SERVER_CONNECT_POINT;
+        public List<DhcpServerConfig> dhcpServerConfigs() {
+            return ImmutableList.of(new MockDhcpServerConfig());
+        }
+    }
+
+    private static class MockDhcpServerConfig extends DhcpServerConfig {
+        @Override
+        public Optional<ConnectPoint> getDhcpServerConnectPoint() {
+            return Optional.of(SERVER_CONNECT_POINT);
         }
 
-        public Ip4Address getDhcpServerIp() {
-            return SERVER_IP;
-        }
-
-        public Ip4Address getDhcpGatewayIp() {
-            return null;
+        @Override
+        public Optional<Ip4Address> getDhcpServerIp4() {
+            return Optional.of(SERVER_IP);
         }
     }
 
diff --git a/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/config/DhcpRelayConfigTest.java b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/config/DhcpRelayConfigTest.java
new file mode 100644
index 0000000..4160e06
--- /dev/null
+++ b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/config/DhcpRelayConfigTest.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.dhcprelay.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.io.Resources;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip6Address;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+
+
+import java.io.IOException;
+
+import static org.junit.Assert.*;
+import static org.onosproject.dhcprelay.DhcpRelayManager.DHCP_RELAY_APP;
+
+/**
+ * Tests for DHCP relay app configuration.
+ */
+public class DhcpRelayConfigTest {
+    private static final String CONFIG_FILE_PATH = "dhcp-relay.json";
+    private static final String INVALID_CONFIG_FILE_PATH = "invalid-dhcp-relay.json";
+    private static final ApplicationId APP_ID = new TestApplicationId("DhcpRelayTest");
+    private static final ConnectPoint DEFAULT_CONNECT_POINT = ConnectPoint.deviceConnectPoint("of:0000000000000002/2");
+    private static final Ip4Address DEFAULT_SERVER_IP = Ip4Address.valueOf("172.168.10.2");
+    private static final Ip4Address DEFAULT_GATEWAY_IP = Ip4Address.valueOf("192.168.10.254");
+    private static final Ip6Address DEFAULT_SERVER_IP_V6 = Ip6Address.valueOf("2000::200:1");
+    private static final Ip6Address DEFAULT_GATEWAY_IP_V6 = Ip6Address.valueOf("1000::100:1");
+
+    private static final ConnectPoint INDIRECT_CONNECT_POINT = ConnectPoint.deviceConnectPoint("of:0000000000000002/3");
+    private static final Ip4Address INDIRECT_SERVER_IP = Ip4Address.valueOf("172.168.10.3");
+
+    @Test
+    public void testDefaultConfig() throws IOException {
+        ObjectMapper om = new ObjectMapper();
+        JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH));
+        DefaultDhcpRelayConfig config = new DefaultDhcpRelayConfig();
+        json = json.path("apps").path(DHCP_RELAY_APP).path(DefaultDhcpRelayConfig.KEY);
+        config.init(APP_ID, DefaultDhcpRelayConfig.KEY, json, om, null);
+
+        assertEquals(1, config.dhcpServerConfigs().size());
+        DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0);
+        assertEquals(DEFAULT_CONNECT_POINT, serverConfig.getDhcpServerConnectPoint().orElse(null));
+        assertEquals(DEFAULT_SERVER_IP, serverConfig.getDhcpServerIp4().orElse(null));
+        assertEquals(DEFAULT_GATEWAY_IP, serverConfig.getDhcpGatewayIp4().orElse(null));
+        assertEquals(DEFAULT_SERVER_IP_V6, serverConfig.getDhcpServerIp6().orElse(null));
+        assertEquals(DEFAULT_GATEWAY_IP_V6, serverConfig.getDhcpGatewayIp6().orElse(null));
+    }
+
+    @Test
+    public void testIndirectConfig() throws IOException {
+        ObjectMapper om = new ObjectMapper();
+        JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH));
+        IndirectDhcpRelayConfig config = new IndirectDhcpRelayConfig();
+        json = json.path("apps").path(DHCP_RELAY_APP).path(IndirectDhcpRelayConfig.KEY);
+        config.init(APP_ID, IndirectDhcpRelayConfig.KEY, json, om, null);
+
+        assertEquals(1, config.dhcpServerConfigs().size());
+        DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0);
+        assertEquals(INDIRECT_CONNECT_POINT, serverConfig.getDhcpServerConnectPoint().orElse(null));
+        assertEquals(INDIRECT_SERVER_IP, serverConfig.getDhcpServerIp4().orElse(null));
+        assertNull(serverConfig.getDhcpGatewayIp4().orElse(null));
+        assertNull(serverConfig.getDhcpServerIp6().orElse(null));
+        assertNull(serverConfig.getDhcpGatewayIp6().orElse(null));
+    }
+
+    @Test
+    public void testInvalidConfig() throws IOException {
+        ObjectMapper om = new ObjectMapper();
+        JsonNode json = om.readTree(Resources.getResource(INVALID_CONFIG_FILE_PATH));
+        DefaultDhcpRelayConfig config = new DefaultDhcpRelayConfig();
+        json = json.path("apps").path(DHCP_RELAY_APP).path(DefaultDhcpRelayConfig.KEY);
+        config.init(APP_ID, DefaultDhcpRelayConfig.KEY, json, om, null);
+        assertFalse(config.isValid());
+    }
+}
diff --git a/apps/dhcprelay/src/test/resources/dhcp-relay.json b/apps/dhcprelay/src/test/resources/dhcp-relay.json
new file mode 100644
index 0000000..bd90b7a
--- /dev/null
+++ b/apps/dhcprelay/src/test/resources/dhcp-relay.json
@@ -0,0 +1,19 @@
+{
+  "apps": {
+    "org.onosproject.dhcprelay" : {
+      "default": [
+        {
+          "dhcpServerConnectPoint": "of:0000000000000002/2",
+          "serverIps": ["172.168.10.2", "2000::200:1"],
+          "gatewayIps": ["192.168.10.254", "1000::100:1"]
+        }
+      ],
+      "indirect": [
+        {
+          "dhcpServerConnectPoint": "of:0000000000000002/3",
+          "serverIps": ["172.168.10.3"]
+        }
+      ]
+    }
+  }
+}
diff --git a/apps/dhcprelay/src/test/resources/invalid-dhcp-relay.json b/apps/dhcprelay/src/test/resources/invalid-dhcp-relay.json
new file mode 100644
index 0000000..2c67eaa
--- /dev/null
+++ b/apps/dhcprelay/src/test/resources/invalid-dhcp-relay.json
@@ -0,0 +1,12 @@
+{
+  "apps": {
+    "org.onosproject.dhcprelay" : {
+      "default": [
+        {
+          "dhcpServerConnectPoint": "of:0000000000000002/2",
+          "gatewayIps": ["192.168.10.254", "1000::100:1"]
+        }
+      ]
+    }
+  }
+}