ONOS-5411: Introduce basic region config.
Change-Id: I3f58e5758d0b350a9e8a03093a1475dbbacfc446
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"
+ ]
+ }
+ }
+ }
+}