Partially extract segmentrouting-api
Change-Id: Iaf2a0bb387a6e7024fa3d75b42cb4874c93a09bb
diff --git a/api/src/test/java/org/onosproject/segmentrouting/config/BlockedPortsConfigTest.java b/api/src/test/java/org/onosproject/segmentrouting/config/BlockedPortsConfigTest.java
new file mode 100644
index 0000000..e457896
--- /dev/null
+++ b/api/src/test/java/org/onosproject/segmentrouting/config/BlockedPortsConfigTest.java
@@ -0,0 +1,239 @@
+/*
+ * 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.segmentrouting.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.DefaultApplicationId;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * Unit tests for {@link BlockedPortsConfig}.
+ */
+public class BlockedPortsConfigTest {
+
+ private static final ApplicationId APP_ID = new DefaultApplicationId(1, "foo");
+ private static final String KEY = "blocked";
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private static final String DEV1 = "of:0000000000000001";
+ private static final String DEV2 = "of:0000000000000002";
+ private static final String DEV3 = "of:0000000000000003";
+ private static final String DEV4 = "of:0000000000000004";
+ private static final String RANGE_14 = "1-4";
+ private static final String RANGE_79 = "7-9";
+ private static final String P1 = "1";
+ private static final String P5 = "5";
+ private static final String P9 = "9";
+
+ private BlockedPortsConfig cfg;
+ private BlockedPortsConfig.Range range;
+
+ private void print(String s) {
+ System.out.println(s);
+ }
+
+ private void print(Object o) {
+ print(o.toString());
+ }
+
+ @Before
+ public void setUp() throws IOException {
+ InputStream blockedPortsJson = BlockedPortsConfigTest.class
+ .getResourceAsStream("/blocked-ports.json");
+ JsonNode node = MAPPER.readTree(blockedPortsJson);
+ cfg = new BlockedPortsConfig();
+ cfg.init(APP_ID, KEY, node, MAPPER, null);
+ }
+
+ @Test
+ public void basic() {
+ cfg = new BlockedPortsConfig();
+ print(cfg);
+
+ assertEquals("non-empty devices list", 0, cfg.deviceIds().size());
+ assertEquals("non-empty port-ranges list", 0, cfg.portRanges("non-exist").size());
+ }
+
+
+ @Test
+ public void overIteratePort() {
+ Iterator<Long> iterator = cfg.portIterator(DEV3);
+ while (iterator.hasNext()) {
+ print(iterator.next());
+ }
+
+ try {
+ print(iterator.next());
+ fail("NoSuchElement exception NOT thrown");
+ } catch (NoSuchElementException e) {
+ print("<good> " + e);
+ }
+ }
+
+ @Test
+ public void overIterateRange() {
+ range = new BlockedPortsConfig.Range("4-6");
+
+ Iterator<Long> iterator = range.iterator();
+ while (iterator.hasNext()) {
+ print(iterator.next());
+ }
+
+ try {
+ print(iterator.next());
+ fail("NoSuchElement exception NOT thrown");
+ } catch (NoSuchElementException e) {
+ print("<good> " + e);
+ }
+ }
+
+
+ @Test
+ public void simple() {
+ List<String> devIds = cfg.deviceIds();
+ print(devIds);
+ assertEquals("wrong dev id count", 3, devIds.size());
+ assertEquals("missing dev 1", true, devIds.contains(DEV1));
+ assertEquals("dev 2??", false, devIds.contains(DEV2));
+ assertEquals("missing dev 3", true, devIds.contains(DEV3));
+
+ List<String> d1ranges = cfg.portRanges(DEV1);
+ print(d1ranges);
+ assertEquals("wrong d1 range count", 2, d1ranges.size());
+ assertEquals("missing 1-4", true, d1ranges.contains(RANGE_14));
+ assertEquals("missing 7-9", true, d1ranges.contains(RANGE_79));
+
+ List<String> d2ranges = cfg.portRanges(DEV2);
+ print(d2ranges);
+ assertEquals("wrong d2 range count", 0, d2ranges.size());
+
+ List<String> d3ranges = cfg.portRanges(DEV3);
+ print(d3ranges);
+ assertEquals("wrong d3 range count", 1, d3ranges.size());
+ assertEquals("range 1-4?", false, d3ranges.contains(RANGE_14));
+ assertEquals("missing 7-9", true, d3ranges.contains(RANGE_79));
+ }
+
+
+ private void verifyPorts(List<Long> ports, long... exp) {
+ assertEquals("Wrong port count", exp.length, ports.size());
+ for (long e : exp) {
+ assertEquals("missing port", true, ports.contains(e));
+ }
+ }
+
+ private void verifyPortIterator(String devid, long... exp) {
+ List<Long> ports = new ArrayList<>();
+ Iterator<Long> iter = cfg.portIterator(devid);
+ iter.forEachRemaining(ports::add);
+ print(ports);
+ verifyPorts(ports, exp);
+ }
+
+ @Test
+ public void rangeIterators() {
+ verifyPortIterator(DEV1, 1, 2, 3, 4, 7, 8, 9);
+ verifyPortIterator(DEV2);
+ verifyPortIterator(DEV3, 7, 8, 9);
+ }
+
+ @Test
+ public void singlePorts() {
+ List<String> devIds = cfg.deviceIds();
+ print(devIds);
+ assertEquals("wrong dev id count", 3, devIds.size());
+ assertEquals("missing dev 4", true, devIds.contains(DEV4));
+
+ List<String> d1ranges = cfg.portRanges(DEV4);
+ print(d1ranges);
+ assertEquals("wrong d4 range count", 3, d1ranges.size());
+ assertEquals("missing 1", true, d1ranges.contains(P1));
+ assertEquals("missing 5", true, d1ranges.contains(P5));
+ assertEquals("missing 9", true, d1ranges.contains(P9));
+
+ verifyPortIterator(DEV4, 1, 5, 9);
+ }
+
+
+ // test Range inner class
+
+ @Test
+ public void rangeBadFormat() {
+ try {
+ range = new BlockedPortsConfig.Range("not-a-range-format");
+ fail("no exception thrown");
+ } catch (IllegalArgumentException iar) {
+ print(iar);
+ assertEquals("wrong msg", "Bad Range Format not-a-range-format", iar.getMessage());
+ }
+ }
+
+ @Test
+ public void rangeBadHi() {
+ try {
+ range = new BlockedPortsConfig.Range("2-nine");
+ fail("no exception thrown");
+ } catch (IllegalArgumentException iar) {
+ print(iar);
+ assertEquals("wrong msg", "Bad Range Format 2-nine", iar.getMessage());
+ }
+ }
+
+ @Test
+ public void rangeHiLessThanLo() {
+ try {
+ range = new BlockedPortsConfig.Range("9-5");
+ fail("no exception thrown");
+ } catch (IllegalArgumentException iar) {
+ print(iar);
+ assertEquals("wrong msg", "Bad Range Format 9-5", iar.getMessage());
+ }
+ }
+
+ @Test
+ public void rangeNegative() {
+ try {
+ range = new BlockedPortsConfig.Range("-2-4");
+ fail("no exception thrown");
+ } catch (IllegalArgumentException iar) {
+ print(iar);
+ assertEquals("wrong msg", "Bad Range Format -2-4", iar.getMessage());
+ }
+ }
+
+ @Test
+ public void rangeGood() {
+ range = new BlockedPortsConfig.Range("100-104");
+ List<Long> values = new ArrayList<>();
+ range.iterator().forEachRemaining(values::add);
+ print(values);
+ verifyPorts(values, 100, 101, 102, 103, 104);
+ }
+}
diff --git a/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java b/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java
new file mode 100644
index 0000000..fa667c0
--- /dev/null
+++ b/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfigTest.java
@@ -0,0 +1,302 @@
+/*
+ * Copyright 2016-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.segmentrouting.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+
+import java.io.InputStream;
+import java.util.Set;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for class {@link SegmentRoutingAppConfig}.
+ */
+public class SegmentRoutingAppConfigTest {
+ private SegmentRoutingAppConfig config;
+ private SegmentRoutingAppConfig invalidConfig;
+ private SegmentRoutingAppConfig mplsEcmpConfig;
+
+ private static final String APP_NAME = "org.onosproject.segmentrouting";
+ 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");
+ private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03");
+ private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1");
+ private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2");
+ private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3");
+ private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1");
+ private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2");
+ private static final String PROVIDER_1 = "org.onosproject.provider.host";
+ private static final String PROVIDER_2 = "org.onosproject.netcfghost";
+ private static final String PROVIDER_3 = "org.onosproject.anotherprovider";
+ private static final IpPrefix BLACKHOLE_IP = IpPrefix.valueOf("10.0.0.0/8");
+ private static final IpPrefix BLACKHOLE_IP_2 = IpPrefix.valueOf("20.0.0.0/8");
+
+ /**
+ * Initialize test related variables.
+ *
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ InputStream jsonStream = SegmentRoutingAppConfigTest.class
+ .getResourceAsStream("/app.json");
+ InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
+ .getResourceAsStream("/app-invalid.json");
+ InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class
+ .getResourceAsStream("/app-ecmp.json");
+
+ String key = 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);
+ }
+
+ /**
+ * Tests config validity.
+ *
+ * @throws Exception
+ */
+ @Test
+ 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));
+ }
+
+ /**
+ * Tests vRouterMacs getter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testVRouterMacs() throws Exception {
+ Set<MacAddress> vRouterMacs = config.vRouterMacs();
+ assertNotNull("vRouterMacs should not be null", vRouterMacs);
+ assertThat(vRouterMacs.size(), is(2));
+ assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
+ assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
+ }
+
+ /**
+ * Tests vRouterMacs setter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSetVRouterMacs() throws Exception {
+ ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
+ builder.add(ROUTER_MAC_3);
+ config.setVRouterMacs(builder.build());
+
+ Set<MacAddress> vRouterMacs = config.vRouterMacs();
+ assertThat(vRouterMacs.size(), is(1));
+ assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
+ }
+
+ /**
+ * Tests suppressSubnet getter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSuppressSubnet() throws Exception {
+ Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
+ assertNotNull("suppressSubnet should not be null", suppressSubnet);
+ assertThat(suppressSubnet.size(), is(2));
+ assertTrue(suppressSubnet.contains(PORT_1));
+ assertTrue(suppressSubnet.contains(PORT_2));
+ }
+
+ /**
+ * Tests suppressSubnet setter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSetSuppressSubnet() throws Exception {
+ ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
+ builder.add(PORT_3);
+ config.setSuppressSubnet(builder.build());
+
+ Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
+ assertNotNull("suppressSubnet should not be null", suppressSubnet);
+ assertThat(suppressSubnet.size(), is(1));
+ assertTrue(suppressSubnet.contains(PORT_3));
+ }
+
+ /**
+ * Tests suppressHostByPort getter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSuppressHostByPort() throws Exception {
+ Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
+ assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
+ assertThat(suppressHostByPort.size(), is(2));
+ assertTrue(suppressHostByPort.contains(PORT_1));
+ assertTrue(suppressHostByPort.contains(PORT_2));
+ }
+
+ /**
+ * Tests suppressHostByPort setter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSetSuppressHostByPort() throws Exception {
+ ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
+ builder.add(PORT_3);
+ config.setSuppressHostByPort(builder.build());
+
+ Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
+ assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
+ assertThat(suppressHostByPort.size(), is(1));
+ assertTrue(suppressHostByPort.contains(PORT_3));
+ }
+
+ /**
+ * Tests suppressHostByProvider getter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSuppressHostByProvider() throws Exception {
+ Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
+ assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
+ assertThat(supprsuppressHostByProvider.size(), is(2));
+ assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1));
+ assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2));
+ }
+
+ /**
+ * Tests suppressHostByProvider setter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSetHostLearning() throws Exception {
+ ImmutableSet.Builder<String> builder = ImmutableSet.builder();
+ builder.add(PROVIDER_3);
+ config.setSuppressHostByProvider(builder.build());
+
+ Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
+ assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
+ assertThat(supprsuppressHostByProvider.size(), is(1));
+ assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3));
+ }
+
+ /**
+ * Tests BlackHoleIps getter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testBlackHoleIps() throws Exception {
+ Set<IpPrefix> blackHoleIps = config.blackholeIPs();
+ assertNotNull("BlackHoleIps should not be null", blackHoleIps);
+ assertThat(blackHoleIps.size(), is(1));
+ assertTrue(blackHoleIps.contains(BLACKHOLE_IP));
+ }
+
+ /**
+ * Tests BlackHoleIps setter.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSetBlackHoleIps() throws Exception {
+
+ config.setBalckholeIps(ImmutableSet.of(BLACKHOLE_IP_2));
+
+ Set<IpPrefix> blackHoleIps = config.blackholeIPs();
+ assertThat(blackHoleIps.size(), is(1));
+ assertTrue(blackHoleIps.contains(BLACKHOLE_IP_2));
+ }
+
+ private class MockDelegate implements ConfigApplyDelegate {
+ @Override
+ public void onApply(Config config) {
+ }
+ }
+}
diff --git a/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingDeviceConfigTest.java b/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingDeviceConfigTest.java
new file mode 100644
index 0000000..ddf90bb
--- /dev/null
+++ b/api/src/test/java/org/onosproject/segmentrouting/config/SegmentRoutingDeviceConfigTest.java
@@ -0,0 +1,222 @@
+/*
+ * Copyright 2016-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.segmentrouting.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link SegmentRoutingDeviceConfig}.
+ */
+public class SegmentRoutingDeviceConfigTest {
+ private SegmentRoutingDeviceConfig config;
+ private SegmentRoutingDeviceConfig ipv6Config;
+ private SegmentRoutingDeviceConfig pairConfig;
+ private SegmentRoutingDeviceConfig invalidConfig;
+ private Map<Integer, Set<Integer>> adjacencySids1;
+ private Map<Integer, Set<Integer>> adjacencySids2;
+ private static final DeviceId PAIR_DEVICE_ID = DeviceId.deviceId("of:123456789ABCDEF0");
+ private static final PortNumber PAIR_LOCAL_PORT = PortNumber.portNumber(10);
+
+ @Before
+ public void setUp() throws Exception {
+ InputStream jsonStream = SegmentRoutingDeviceConfigTest.class
+ .getResourceAsStream("/device.json");
+ InputStream ipv6JsonStream = SegmentRoutingDeviceConfigTest.class
+ .getResourceAsStream("/device-ipv6.json");
+ InputStream pairJsonStream = SegmentRoutingDeviceConfigTest.class
+ .getResourceAsStream("/device-pair.json");
+ InputStream invalidJsonStream = SegmentRoutingDeviceConfigTest.class
+ .getResourceAsStream("/device-invalid.json");
+
+ adjacencySids1 = new HashMap<>();
+ Set<Integer> ports1 = new HashSet<>();
+ ports1.add(2);
+ ports1.add(3);
+ adjacencySids1.put(100, ports1);
+ Set<Integer> ports2 = new HashSet<>();
+ ports2.add(4);
+ ports2.add(5);
+ adjacencySids1.put(200, ports2);
+
+ adjacencySids2 = new HashMap<>();
+ Set<Integer> ports3 = new HashSet<>();
+ ports3.add(6);
+ adjacencySids2.put(300, ports3);
+
+ DeviceId subject = DeviceId.deviceId("of:0000000000000001");
+ String key = "segmentrouting";
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonNode = mapper.readTree(jsonStream);
+ JsonNode ipv6JsonNode = mapper.readTree(ipv6JsonStream);
+ JsonNode pairJsonNode = mapper.readTree(pairJsonStream);
+ JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
+ ConfigApplyDelegate delegate = new MockDelegate();
+
+ config = new SegmentRoutingDeviceConfig();
+ config.init(subject, key, jsonNode, mapper, delegate);
+
+ ipv6Config = new SegmentRoutingDeviceConfig();
+ ipv6Config.init(subject, key, ipv6JsonNode, mapper, delegate);
+
+ pairConfig = new SegmentRoutingDeviceConfig();
+ pairConfig.init(subject, key, pairJsonNode, mapper, delegate);
+
+ invalidConfig = new SegmentRoutingDeviceConfig();
+ invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue(config.isValid());
+ assertTrue(ipv6Config.isValid());
+ assertTrue(pairConfig.isValid());
+ assertFalse(invalidConfig.isValid());
+ }
+
+ @Test
+ public void testName() throws Exception {
+ assertTrue(config.name().isPresent());
+ assertThat(config.name().get(), is("Leaf-R1"));
+ }
+
+ @Test
+ public void testSetName() throws Exception {
+ config.setName("Spine-R1");
+ assertTrue(config.name().isPresent());
+ assertThat(config.name().get(), is("Spine-R1"));
+ }
+
+ @Test
+ public void testRouterIp() throws Exception {
+ assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
+ assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
+ assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a8:0101")));
+ }
+
+ @Test
+ public void testSetRouterIp() throws Exception {
+ config.setRouterIpv4("10.0.2.254");
+ assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
+ ipv6Config.setRouterIpv4("10.0.2.254");
+ assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
+ ipv6Config.setRouterIpv6("2000::c0a9:0101");
+ assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a9:0101")));
+ }
+
+ @Test
+ public void testRouterMac() throws Exception {
+ assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:01:80")));
+ }
+
+ @Test
+ public void testSetRouterMac() throws Exception {
+ config.setRouterMac("00:00:00:00:02:80");
+ assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:02:80")));
+ }
+
+ @Test
+ public void testNodeSid() throws Exception {
+ assertThat(config.nodeSidIPv4(), is(101));
+ assertThat(ipv6Config.nodeSidIPv4(), is(101));
+ assertThat(ipv6Config.nodeSidIPv6(), is(111));
+ }
+
+ @Test
+ public void testSetNodeSid() throws Exception {
+ config.setNodeSidIPv4(200);
+ assertThat(config.nodeSidIPv4(), is(200));
+ ipv6Config.setNodeSidIPv4(200);
+ assertThat(ipv6Config.nodeSidIPv4(), is(200));
+ ipv6Config.setNodeSidIPv6(201);
+ assertThat(ipv6Config.nodeSidIPv6(), is(201));
+ }
+
+ @Test
+ public void testIsEdgeRouter() throws Exception {
+ assertThat(config.isEdgeRouter(), is(true));
+ }
+
+ @Test
+ public void testSetIsEdgeRouter() throws Exception {
+ config.setIsEdgeRouter(false);
+ assertThat(config.isEdgeRouter(), is(false));
+ }
+
+ @Test
+ public void testAdjacencySids() throws Exception {
+ assertThat(config.adjacencySids(), is(adjacencySids1));
+ }
+
+ @Test
+ public void testSetAdjacencySids() throws Exception {
+ config.setAdjacencySids(adjacencySids2);
+ assertThat(config.adjacencySids(), is(adjacencySids2));
+ }
+
+ @Test
+ public void testPairDeviceId() throws Exception {
+ assertNull(config.pairDeviceId());
+ assertNull(ipv6Config.pairDeviceId());
+ assertThat(pairConfig.pairDeviceId(), is(PAIR_DEVICE_ID));
+ }
+
+ @Test
+ public void testSetPairDeviceId() throws Exception {
+ config.setPairDeviceId(PAIR_DEVICE_ID);
+ assertThat(config.pairDeviceId(), is(PAIR_DEVICE_ID));
+ }
+
+ @Test
+ public void testPairLocalPort() throws Exception {
+ assertNull(config.pairLocalPort());
+ assertNull(ipv6Config.pairLocalPort());
+ assertThat(pairConfig.pairLocalPort(), is(PAIR_LOCAL_PORT));
+ }
+
+ @Test
+ public void testSetPairLocalPort() throws Exception {
+ config.setPairLocalPort(PAIR_LOCAL_PORT);
+ assertThat(config.pairLocalPort(), is(PAIR_LOCAL_PORT));
+ }
+
+ private class MockDelegate implements ConfigApplyDelegate {
+ @Override
+ public void onApply(Config configFile) {
+ }
+ }
+}