Fixed REST API bug in Xconnect

In addition,
- Deprecated old XConnectHandler
- Fixed equals() of XconnectDesc and XconnectKey
- Implemented unit tests for XconnectCodec

Change-Id: I1b5f2c1f389523e3b65a3d9acaf75bc06e39fdda
diff --git a/app/src/test/java/org/onosproject/segmentrouting/config/XConnectConfigTest.java b/app/src/test/java/org/onosproject/segmentrouting/config/XConnectConfigTest.java
deleted file mode 100644
index 62d2d86..0000000
--- a/app/src/test/java/org/onosproject/segmentrouting/config/XConnectConfigTest.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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.VlanId;
-import org.onosproject.TestApplicationId;
-import org.onosproject.core.ApplicationId;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.PortNumber;
-import org.onosproject.net.config.Config;
-import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.segmentrouting.SegmentRoutingManager;
-import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
-import java.io.InputStream;
-import java.util.Set;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertThat;
-import static org.hamcrest.Matchers.is;
-
-/**
- * Tests for class {@link XConnectConfig}.
- */
-public class XConnectConfigTest {
-    private static final DeviceId DEV1 = DeviceId.deviceId("of:0000000000000001");
-    private static final DeviceId DEV2 = DeviceId.deviceId("of:0000000000000002");
-    private static final VlanId VLAN10 = VlanId.vlanId((short) 10);
-    private static final VlanId VLAN20 = VlanId.vlanId((short) 20);
-    private static final PortNumber PORT3 = PortNumber.portNumber(3);
-    private static final PortNumber PORT4 = PortNumber.portNumber(4);
-    private static final PortNumber PORT5 = PortNumber.portNumber(5);
-    private static final XConnectStoreKey KEY1 = new XConnectStoreKey(DEV1, VLAN10);
-    private static final XConnectStoreKey KEY2 = new XConnectStoreKey(DEV2, VLAN10);
-    private static final XConnectStoreKey KEY3 = new XConnectStoreKey(DEV2, VLAN20);
-    private static final XConnectStoreKey KEY4 = new XConnectStoreKey(DEV2, VlanId.NONE);
-
-    private XConnectConfig config;
-    private XConnectConfig invalidConfig;
-
-    @Before
-    public void setUp() throws Exception {
-        InputStream jsonStream = SegmentRoutingAppConfigTest.class
-                .getResourceAsStream("/xconnect.json");
-        InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
-                .getResourceAsStream("/xconnect-invalid.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);
-        ConfigApplyDelegate delegate = new XConnectConfigTest.MockDelegate();
-
-        config = new XConnectConfig();
-        config.init(subject, key, jsonNode, mapper, delegate);
-        invalidConfig = new XConnectConfig();
-        invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
-    }
-
-    /**
-     * Tests config validity.
-     */
-    @Test
-    public void testIsValid() {
-        assertTrue(config.isValid());
-        assertFalse(invalidConfig.isValid());
-    }
-
-    /**
-     * Tests getXconnects.
-     */
-    @Test
-    public void testGetXconnects() {
-        Set<XConnectStoreKey> xconnects = config.getXconnects();
-        assertThat(xconnects.size(), is(3));
-        assertTrue(xconnects.contains(KEY1));
-        assertTrue(xconnects.contains(KEY2));
-        assertTrue(xconnects.contains(KEY3));
-        assertFalse(xconnects.contains(KEY4));
-    }
-
-    /**
-     * Tests getPorts.
-     */
-    @Test
-    public void testGetPorts() {
-        Set<PortNumber> ports;
-
-        ports = config.getPorts(KEY1);
-        assertThat(ports.size(), is(2));
-        assertTrue(ports.contains(PORT3));
-        assertTrue(ports.contains(PORT4));
-
-        ports = config.getPorts(KEY2);
-        assertThat(ports.size(), is(2));
-        assertTrue(ports.contains(PORT3));
-        assertTrue(ports.contains(PORT4));
-
-        ports = config.getPorts(KEY3);
-        assertThat(ports.size(), is(2));
-        assertTrue(ports.contains(PORT4));
-        assertTrue(ports.contains(PORT5));
-    }
-
-    private class MockDelegate implements ConfigApplyDelegate {
-        @Override
-        public void onApply(Config config) {
-        }
-    }
-}
\ No newline at end of file
diff --git a/app/src/test/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodecTest.java b/app/src/test/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodecTest.java
new file mode 100644
index 0000000..bac9ff4
--- /dev/null
+++ b/app/src/test/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodecTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2019-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.xconnect.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.VlanId;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.MockCodecContext;
+import org.onosproject.net.DeviceId;
+
+import java.io.InputStream;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+
+public class XconnectCodecTest {
+    private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:1");
+    private static final VlanId VLAN_VID = VlanId.vlanId((short) 10);
+    private static final XconnectKey KEY = new XconnectKey(DEVICE_ID, VLAN_VID);
+    private static final XconnectEndpoint EP1 = XconnectEndpoint.fromString("1");
+    private static final XconnectEndpoint EP2 = XconnectEndpoint.fromString("2");
+    private static final XconnectEndpoint EP3 = XconnectEndpoint.fromString("LB:5");
+
+    private CodecContext context;
+    private JsonCodec<XconnectDesc> codec;
+
+    @Before
+    public void setUp() throws Exception {
+        context = new MockCodecContext();
+        codec = new XconnectCodec();
+    }
+
+    @Test
+    public void testEncodePort() throws Exception {
+        Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP2);
+        XconnectDesc desc1 = new XconnectDesc(KEY, endpoints1);
+
+        ObjectMapper mapper = new ObjectMapper();
+        InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect1.json");
+        JsonNode expected = mapper.readTree(jsonStream1);
+
+        JsonNode actual = codec.encode(desc1, context);
+
+        assertEquals(expected.get(XconnectCodec.DEVICE_ID), actual.get(XconnectCodec.DEVICE_ID));
+        assertEquals(expected.get(XconnectCodec.VLAN_ID).asInt(), actual.get(XconnectCodec.VLAN_ID).asInt());
+        assertEquals(expected.get(XconnectCodec.ENDPOINTS), actual.get(XconnectCodec.ENDPOINTS));
+    }
+
+    @Test
+    public void testDecodePort() throws Exception {
+        Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP2);
+        XconnectDesc expected = new XconnectDesc(KEY, endpoints1);
+
+        ObjectMapper mapper = new ObjectMapper();
+        InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect1.json");
+        ObjectNode objectNode = mapper.readTree(jsonStream1).deepCopy();
+
+        XconnectDesc actual = codec.decode(objectNode, context);
+
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testEncodeLb() throws Exception {
+        Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP3);
+        XconnectDesc desc1 = new XconnectDesc(KEY, endpoints1);
+
+        ObjectMapper mapper = new ObjectMapper();
+        InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect2.json");
+        JsonNode expected = mapper.readTree(jsonStream1);
+
+        JsonNode actual = codec.encode(desc1, context);
+
+        assertEquals(expected.get(XconnectCodec.DEVICE_ID), actual.get(XconnectCodec.DEVICE_ID));
+        assertEquals(expected.get(XconnectCodec.VLAN_ID).asInt(), actual.get(XconnectCodec.VLAN_ID).asInt());
+        assertEquals(expected.get(XconnectCodec.ENDPOINTS), actual.get(XconnectCodec.ENDPOINTS));
+    }
+
+    @Test
+    public void testDecodeLb() throws Exception {
+        Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP3);
+        XconnectDesc expected = new XconnectDesc(KEY, endpoints1);
+
+        ObjectMapper mapper = new ObjectMapper();
+        InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect2.json");
+        ObjectNode objectNode = mapper.readTree(jsonStream1).deepCopy();
+
+        XconnectDesc actual = codec.decode(objectNode, context);
+
+        assertEquals(expected, actual);
+    }
+}
\ No newline at end of file
diff --git a/app/src/test/resources/xconnect-invalid.json b/app/src/test/resources/xconnect-invalid.json
deleted file mode 100644
index e468271..0000000
--- a/app/src/test/resources/xconnect-invalid.json
+++ /dev/null
@@ -1,18 +0,0 @@
-{
-  "of:0000000000000001": [
-    {
-      "vlan": 10,
-      "ports": [3, 4]
-    }
-  ],
-  "of:0000000000000002": [
-    {
-      "vlan": 10,
-      "ports": [3, 4]
-    },
-    {
-      "vlan": 20,
-      "ports": [4, 5, 6]
-    }
-  ]
-}
diff --git a/app/src/test/resources/xconnect.json b/app/src/test/resources/xconnect.json
deleted file mode 100644
index ebd61b3..0000000
--- a/app/src/test/resources/xconnect.json
+++ /dev/null
@@ -1,19 +0,0 @@
-{
-  "of:0000000000000001": [
-    {
-      "vlan": 10,
-      "ports": [3, 4],
-      "name": "OLT1"
-    }
-  ],
-  "of:0000000000000002": [
-    {
-      "vlan": 10,
-      "ports": [3, 4]
-    },
-    {
-      "vlan": 20,
-      "ports": [4, 5]
-    }
-  ]
-}
diff --git a/app/src/test/resources/xconnect1.json b/app/src/test/resources/xconnect1.json
new file mode 100644
index 0000000..d8990d4
--- /dev/null
+++ b/app/src/test/resources/xconnect1.json
@@ -0,0 +1,5 @@
+{
+  "deviceId": "of:1",
+  "vlanId": 10,
+  "endpoints": ["1", "2"]
+}
diff --git a/app/src/test/resources/xconnect2.json b/app/src/test/resources/xconnect2.json
new file mode 100644
index 0000000..61919da
--- /dev/null
+++ b/app/src/test/resources/xconnect2.json
@@ -0,0 +1,5 @@
+{
+  "deviceId": "of:1",
+  "vlanId": 10,
+  "endpoints": ["1", "LB:5"]
+}