ONOS-5411: Introduce basic region config.

Change-Id: I3f58e5758d0b350a9e8a03093a1475dbbacfc446
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java
new file mode 100644
index 0000000..200b375
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.net.config.basics;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.region.Region;
+import org.onosproject.net.region.RegionId;
+
+import java.util.List;
+
+/**
+ * Basic configuration for network regions.
+ */
+public final class BasicRegionConfig extends Config<RegionId> {
+
+    private static final String TYPE = "type";
+    private static final String DEVICES = "devices";
+
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(TYPE, DEVICES);
+    }
+
+    /**
+     * Returns the region type.
+     *
+     * @return the region type
+     */
+    public Region.Type getType() {
+        String t = get(TYPE, null);
+        return t == null ? null : regionTypeFor(t);
+    }
+
+    private Region.Type regionTypeFor(String t) {
+        try {
+            return Region.Type.valueOf(t.toUpperCase());
+        } catch (IllegalArgumentException ignored) {
+        }
+        return null;
+    }
+
+    /**
+     * Returns the identities of the devices in this region.
+     *
+     * @return list of device identifiers
+     */
+    public List<DeviceId> getDevices() {
+        return getList(DEVICES, DeviceId::deviceId);
+    }
+
+    // TODO: implement setters
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicUiTopoLayoutConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicUiTopoLayoutConfig.java
new file mode 100644
index 0000000..5ba8093
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicUiTopoLayoutConfig.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.net.config.basics;
+
+import org.onosproject.net.config.Config;
+import org.onosproject.net.region.RegionId;
+import org.onosproject.ui.model.topo.UiTopoLayoutId;
+
+import static org.onosproject.net.region.RegionId.regionId;
+
+/**
+ * Basic configuration for UI topology layouts.
+ */
+public class BasicUiTopoLayoutConfig extends Config<UiTopoLayoutId> {
+
+    private static final String REGION = "region";
+    private static final String PARENT = "parent";
+
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(REGION, PARENT);
+    }
+
+    /**
+     * Returns the identifier of the backing region. This will be
+     * null if there is no backing region.
+     *
+     * @return backing region identity
+     */
+    public RegionId getRegion() {
+        String r = get(REGION, null);
+        return r == null ? null : regionId(r);
+    }
+
+    /**
+     * Returns the identifier of the parent layout.
+     *
+     * @return layout identifier of parent
+     */
+    public UiTopoLayoutId getParent() {
+        String p = get(PARENT, null);
+        return p == null ? UiTopoLayoutId.DEFAULT_ID : UiTopoLayoutId.layoutId(p);
+    }
+
+    // TODO: implement setters
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
index da075d4..104dfe5 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
@@ -22,6 +22,7 @@
 import org.onosproject.net.HostId;
 import org.onosproject.net.LinkKey;
 import org.onosproject.net.config.SubjectFactory;
+import org.onosproject.net.region.RegionId;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
@@ -37,18 +38,25 @@
     // Required for resolving application identifiers
     private static CoreService coreService;
 
+    /**
+     * Application ID subject factory.
+     */
     public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY =
             new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") {
                 @Override
                 public ApplicationId createSubject(String key) {
                     return coreService.registerApplication(key);
                 }
+
                 @Override
                 public String subjectKey(ApplicationId subject) {
                     return subject.name();
                 }
             };
 
+    /**
+     * Device ID subject factory.
+     */
     public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
             new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
                 @Override
@@ -57,18 +65,25 @@
                 }
             };
 
+    /**
+     * Connect point subject factory.
+     */
     public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
             new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
                 @Override
                 public ConnectPoint createSubject(String key) {
                     return ConnectPoint.deviceConnectPoint(key);
                 }
+
                 @Override
                 public String subjectKey(ConnectPoint subject) {
                     return key(subject);
                 }
             };
 
+    /**
+     * Host ID subject factory.
+     */
     public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
             new SubjectFactory<HostId>(HostId.class, "hosts") {
                 @Override
@@ -77,6 +92,9 @@
                 }
             };
 
+    /**
+     * Link key subject factory.
+     */
     public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
             new SubjectFactory<LinkKey>(LinkKey.class, "links") {
                 @Override
@@ -84,8 +102,9 @@
                     String[] cps = key.split("-");
                     checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
                     return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
-                                           ConnectPoint.deviceConnectPoint(cps[1]));
+                            ConnectPoint.deviceConnectPoint(cps[1]));
                 }
+
                 @Override
                 public String subjectKey(LinkKey subject) {
                     return key(subject.src()) + "-" + key(subject.dst());
@@ -93,6 +112,17 @@
             };
 
     /**
+     * Region ID subject factory.
+     */
+    public static final SubjectFactory<RegionId> REGION_SUBJECT_FACTORY =
+            new SubjectFactory<RegionId>(RegionId.class, "regions") {
+                @Override
+                public RegionId createSubject(String key) {
+                    return RegionId.regionId(key);
+                }
+            };
+
+    /**
      * Provides reference to the core service, which is required for
      * application subject factory.
      *
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
index 3297608..f4ffb02 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
@@ -490,8 +490,12 @@
             log.debug("Synthetic link: {}", synthetic);
         });
 
+
         synthLinks.clear();
         synthLinks.addAll(slinks);
+
+        // TODO : compute and add host-device links to synthLinks...
+
     }
 
     private UiSynthLink inferSyntheticLink(UiDeviceLink link) {
diff --git a/core/api/src/test/java/org/onosproject/net/config/basics/AbstractConfigTest.java b/core/api/src/test/java/org/onosproject/net/config/basics/AbstractConfigTest.java
new file mode 100644
index 0000000..4eb00c2
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/AbstractConfigTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.net.config.basics;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.ConfigApplyDelegate;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.fail;
+
+/**
+ * Abstract superclass for config tests.
+ */
+abstract class AbstractConfigTest {
+
+    private static final String D_PREFIX = "of:00000000000000";
+
+    /**
+     * Shared object mapper.
+     */
+    final ObjectMapper mapper = new ObjectMapper();
+
+    /**
+     * Shared null-delegate.
+     */
+    final ConfigApplyDelegate delegate = config -> {
+    };
+
+    /**
+     * Prints the given format string with parameter replacement to stdout.
+     *
+     * @param fmt    format string
+     * @param params parameters
+     * @see String#format(String, Object...)
+     */
+    static void print(String fmt, Object... params) {
+        System.out.println(String.format(fmt, params));
+    }
+
+    /**
+     * Prints the given object's string representation to stdout.
+     *
+     * @param o the object to print
+     */
+    static void print(Object o) {
+        print("%s", o);
+    }
+
+    /**
+     * Reads in and parses the specified JSON file, returning a JSON node
+     * representation of the data. Note that if an error occurs while
+     * attempting to read the file, {@link org.junit.Assert#fail()} will be
+     * called.
+     *
+     * @param path JSON file path
+     * @return data represented as a JSON node
+     */
+    JsonNode getTestJson(String path) {
+        try {
+            InputStream is = AbstractConfigTest.class.getResourceAsStream(path);
+            return mapper.readTree(is);
+
+        } catch (IOException e) {
+            fail("Could not read json from: " + path + ": " + e.getMessage());
+        }
+        return null;
+    }
+
+    /**
+     * Returns a device identifier from a given string suffix.
+     *
+     * @param suffix two character suffix
+     * @return device identifier
+     */
+    static DeviceId dstr(String suffix) {
+        return DeviceId.deviceId(D_PREFIX + suffix);
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/config/basics/BasicRegionConfigTest.java b/core/api/src/test/java/org/onosproject/net/config/basics/BasicRegionConfigTest.java
new file mode 100644
index 0000000..9a01b3d
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/BasicRegionConfigTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.net.config.basics;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.region.Region;
+
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.region.RegionId.regionId;
+
+/**
+ * Test class for {@link BasicRegionConfig}.
+ */
+public class BasicRegionConfigTest extends AbstractConfigTest {
+
+    private static final String REGION_JSON = "configs.regions.1.json";
+    private static final String R1 = "r1";
+    private static final String R2 = "r2";
+    private static final String R3 = "r3";
+
+    private static final Set<DeviceId> R1_DEVS =
+            ImmutableSet.of(dstr("01"), dstr("02"), dstr("03"));
+    private static final Set<DeviceId> R2_DEVS =
+            ImmutableSet.of(dstr("04"), dstr("05"), dstr("06"));
+    private static final Set<DeviceId> R3_DEVS =
+            ImmutableSet.of(dstr("07"), dstr("08"), dstr("09"));
+
+
+    private JsonNode data;
+
+    @Before
+    public void setUp() {
+        data = getTestJson(REGION_JSON);
+    }
+
+    private JsonNode getR(String key) {
+        return data.get("regions").get(key).get("basic");
+    }
+
+    private void checkRegion(String rid, Region.Type expT, Set<DeviceId> expD) {
+        JsonNode r1json = getR(rid);
+        print(r1json);
+
+        BasicRegionConfig brc = new BasicRegionConfig();
+        brc.init(regionId(rid), rid, r1json, mapper, delegate);
+
+        Region.Type type = brc.getType();
+        assertEquals("wrong type", expT, type);
+
+        List<DeviceId> devs = brc.getDevices();
+        assertEquals("wr.size", expD.size(), devs.size());
+        for (DeviceId d : expD) {
+            assertTrue("missing dev: " + d, devs.contains(d));
+        }
+    }
+
+    @Test
+    public void region1Config() {
+        checkRegion(R1, Region.Type.CONTINENT, R1_DEVS);
+    }
+
+    @Test
+    public void region2Config() {
+        checkRegion(R2, Region.Type.METRO, R2_DEVS);
+    }
+
+    @Test
+    public void region3Config() {
+        checkRegion(R3, null, R3_DEVS);
+    }
+
+}
diff --git a/core/api/src/test/resources/org/onosproject/net/config/basics/configs.layouts.1.json b/core/api/src/test/resources/org/onosproject/net/config/basics/configs.layouts.1.json
new file mode 100644
index 0000000..b92c999
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/net/config/basics/configs.layouts.1.json
@@ -0,0 +1,35 @@
+{
+  "layouts": {
+    "_default_": {
+      "basic": {
+        "region": "",
+        "geomap": "uk",
+        "scale": 1.2,
+        "offsetX": -50,
+        "offsetY": 0
+      }
+    },
+    "l1": {
+      "basic": {
+        "region": "r1",
+        "geomap": "uk-brighton",
+        "scale": 0.9,
+        "offsetX": 200,
+        "offsetY": -45
+      }
+    },
+    "l2": {
+      "basic": {
+        "region": "r2",
+        "geomap": "uk-london"
+      }
+    },
+    "l3": {
+      "basic": {
+        "parent": "l2",
+        "region": "r3",
+        "sprites": "uk-london-westminster"
+      }
+    }
+  }
+}
diff --git a/core/api/src/test/resources/org/onosproject/net/config/basics/configs.regions.1.json b/core/api/src/test/resources/org/onosproject/net/config/basics/configs.regions.1.json
new file mode 100644
index 0000000..9ce4073
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/net/config/basics/configs.regions.1.json
@@ -0,0 +1,33 @@
+{
+  "regions": {
+    "r1": {
+      "basic": {
+        "type": "continent",
+        "devices": [
+          "of:0000000000000001",
+          "of:0000000000000002",
+          "of:0000000000000003"
+        ]
+      }
+    },
+    "r2": {
+      "basic": {
+        "type": "metro",
+        "devices": [
+          "of:0000000000000004",
+          "of:0000000000000005",
+          "of:0000000000000006"
+        ]
+      }
+    },
+    "r3": {
+      "basic": {
+        "devices": [
+          "of:0000000000000007",
+          "of:0000000000000008",
+          "of:0000000000000009"
+        ]
+      }
+    }
+  }
+}