JSON Codec for FilteredConnectPoint

Change-Id: I5584361daa4b0753e583d97b201d5fa70e182778
diff --git a/core/api/src/main/java/org/onosproject/codec/CodecContext.java b/core/api/src/main/java/org/onosproject/codec/CodecContext.java
index c25abfe4..3189e10 100644
--- a/core/api/src/main/java/org/onosproject/codec/CodecContext.java
+++ b/core/api/src/main/java/org/onosproject/codec/CodecContext.java
@@ -15,7 +15,10 @@
  */
 package org.onosproject.codec;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 /**
  * Context for codecs to use while encoding/decoding.
@@ -47,4 +50,29 @@
      */
     <T> T getService(Class<T> serviceClass);
 
+    /**
+     * Decodes the specified entity from JSON using codec
+     * registered to this context.
+     *
+     * @param json    JSON to decode
+     * @param entityClass entity class
+     * @return decoded entity
+     */
+    default <T> T decode(JsonNode json, Class<T> entityClass) {
+        checkArgument(json.isObject());
+        return codec(entityClass).decode((ObjectNode) json, this);
+    }
+
+    /**
+     * Encodes the specified entity into JSON using codec
+     * registered to this context.
+     *
+     * @param entity  entity to encode
+     * @param entityClass entity class
+     * @return JSON node
+     */
+    default <T> ObjectNode encode(T entity, Class<T> entityClass) {
+        return codec(entityClass).encode(entity, this);
+    }
+
 }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 3f305b6..cd0e7ae 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -54,6 +54,7 @@
 import org.onosproject.net.MastershipRole;
 import org.onosproject.net.Path;
 import org.onosproject.net.DisjointPath;
+import org.onosproject.net.FilteredConnectPoint;
 import org.onosproject.net.Port;
 import org.onosproject.net.device.PortStatistics;
 import org.onosproject.net.driver.Driver;
@@ -165,6 +166,7 @@
         registerCodec(TrafficStatInfo.class, new TrafficStatInfoCodec());
         registerCodec(ProtocolStatInfo.class, new ProtocolStatInfoCodec());
         registerCodec(FlowStatInfo.class, new FlowStatInfoCodec());
+        registerCodec(FilteredConnectPoint.class, new FilteredConnectPointCodec());
         log.info("Started");
     }
 
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/FilteredConnectPointCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/FilteredConnectPointCodec.java
new file mode 100644
index 0000000..5ffc1c5
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/FilteredConnectPointCodec.java
@@ -0,0 +1,50 @@
+/*
+ * 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.codec.impl;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.FilteredConnectPoint;
+import org.onosproject.net.flow.TrafficSelector;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * JSON Codec for {@link FilteredConnectPoint}.
+ */
+public class FilteredConnectPointCodec extends JsonCodec<FilteredConnectPoint> {
+
+    private static final String CONNECT_POINT = "connectPoint";
+    private static final String TRAFFIC_SELECTOR = "trafficSelector";
+
+
+    @Override
+    public ObjectNode encode(FilteredConnectPoint entity,
+                             CodecContext context) {
+        ObjectNode node = context.mapper().createObjectNode();
+        node.set(CONNECT_POINT, context.encode(entity.connectPoint(), ConnectPoint.class));
+        node.set(TRAFFIC_SELECTOR, context.encode(entity.trafficSelector(), TrafficSelector.class));
+        return node;
+    }
+
+    @Override
+    public FilteredConnectPoint decode(ObjectNode json, CodecContext context) {
+        ConnectPoint cp = context.decode(json.get(CONNECT_POINT), ConnectPoint.class);
+        TrafficSelector ts = context.decode(json.get(TRAFFIC_SELECTOR), TrafficSelector.class);
+        return new FilteredConnectPoint(cp, ts);
+    }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/FilteredConnectPointCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/FilteredConnectPointCodecTest.java
new file mode 100644
index 0000000..2aaa796
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/FilteredConnectPointCodecTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.codec.impl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+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.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.FilteredConnectPoint;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.NumericNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Unit tests for {@link FilteredConnectPointCodec}.
+ */
+public class FilteredConnectPointCodecTest {
+
+    private static final VlanId VID_56 = VlanId.vlanId((short) 56);
+    private static final TrafficSelector VLAN_SELECTOR
+        = DefaultTrafficSelector.builder().matchVlanId(VID_56).build();
+    private static final PortNumber PN_42 = PortNumber.portNumber(42);
+    private static final DeviceId DID = DeviceId.deviceId("test:device");
+    private static final ConnectPoint CP = new ConnectPoint(DID, PN_42);
+
+    private static final String JSON
+        = "{"
+            + "\"connectPoint\":{\"port\":\"42\",\"device\":\"test:device\"},"
+            + "\"trafficSelector\":{\"criteria\":[{\"type\":\"VLAN_VID\",\"vlanId\":56}]}"
+        + "}";
+
+    private FilteredConnectPointCodec sut;
+    private CodecContext context;
+
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        JsonCodec<FilteredConnectPoint> codec = context.codec(FilteredConnectPoint.class);
+        assertThat(codec, instanceOf(FilteredConnectPointCodec.class));
+        sut = (FilteredConnectPointCodec) codec;
+    }
+
+
+    @Test
+    public void testNoInformationLoss() {
+        FilteredConnectPoint original = new FilteredConnectPoint(CP, VLAN_SELECTOR);
+        ObjectNode json = sut.encode(original, context);
+        assertNotNull(json);
+
+        FilteredConnectPoint decoded = sut.decode(json, context);
+        assertThat(decoded, is(equalTo(original)));
+    }
+
+    @Test
+    public void testJsonFormat() throws JsonProcessingException, IOException {
+        FilteredConnectPoint original = new FilteredConnectPoint(CP, VLAN_SELECTOR);
+
+        // Jackson configuration for ease of Numeric node comparison
+        // - treat integral number node as long node
+        context.mapper().enable(DeserializationFeature.USE_LONG_FOR_INTS);
+        context.mapper().setNodeFactory(new JsonNodeFactory(false) {
+            @Override
+            public NumericNode numberNode(int v) {
+                return super.numberNode((long) v);
+            }
+            @Override
+            public NumericNode numberNode(short v) {
+                return super.numberNode((long) v);
+            }
+        });
+
+        ObjectNode json = sut.encode(original, context);
+        JsonNode expected = context.mapper().readTree(JSON);
+
+        assertEquals(expected, json);
+    }
+
+    @Test
+    public void testEmptySelector() {
+        FilteredConnectPoint original = new FilteredConnectPoint(CP);
+        ObjectNode json = sut.encode(original, context);
+        assertNotNull(json);
+
+        FilteredConnectPoint decoded = sut.decode(json, context);
+        assertThat(decoded, is(equalTo(original)));
+    }
+
+}