[ONOS-3163] Flow Classifier Codec UT. Codec fixes.

Change-Id: I86a5745e0698a79b9d015a902c79606126d28400
diff --git a/apps/vtn/pom.xml b/apps/vtn/pom.xml
index e8387cb..5096a5e 100644
--- a/apps/vtn/pom.xml
+++ b/apps/vtn/pom.xml
@@ -36,9 +36,9 @@
         <module>vtnmgr</module>
         <module>vtnweb</module>
         <module>app</module>
-    </modules>
-    <dependencies>
-        <dependency>
+  	</modules>
+  	<dependencies>
+  	    <dependency>
             <groupId>org.onosproject</groupId>
             <artifactId>onlab-junit</artifactId>
             <scope>test</scope>
@@ -53,5 +53,25 @@
             <artifactId>easymock</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-osgi</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-servlet</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey.jersey-test-framework</groupId>
+            <artifactId>jersey-test-framework-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey.jersey-test-framework</groupId>
+            <artifactId>jersey-test-framework-grizzly2</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java
index 4c17633..a18ca36 100644
--- a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java
+++ b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java
@@ -18,16 +18,14 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onlab.util.Tools.nullIsIllegal;
 
-import java.util.UUID;
-
 import org.onlab.packet.IpPrefix;
 import org.onosproject.codec.CodecContext;
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.vtnrsc.DefaultFlowClassifier;
 import org.onosproject.vtnrsc.FlowClassifier;
 import org.onosproject.vtnrsc.FlowClassifierId;
-import org.onosproject.vtnrsc.VirtualPortId;
 import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.VirtualPortId;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
@@ -40,7 +38,7 @@
     private static final String TENANT_ID = "tenant_id";
     private static final String NAME = "name";
     private static final String DESCRIPTION = "description";
-    private static final String ETHER_TYPE = "etherType";
+    private static final String ETHER_TYPE = "ethertype";
     private static final String PROTOCOL = "protocol";
     private static final String MIN_SRC_PORT_RANGE = "source_port_range_min";
     private static final String MAX_SRC_PORT_RANGE = "source_port_range_max";
@@ -62,7 +60,7 @@
 
         String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID),
                 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText();
-        resultBuilder.setFlowClassifierId(FlowClassifierId.of(UUID.fromString(flowClassifierId)));
+        resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId));
 
         String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
         resultBuilder.setTenantId(TenantId.tenantId(tenantId));
@@ -70,44 +68,46 @@
         String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText();
         resultBuilder.setName(flowClassiferName);
 
-        String flowClassiferDescription = nullIsIllegal(json.get(DESCRIPTION), DESCRIPTION + MISSING_MEMBER_MESSAGE)
-                .asText();
+        String flowClassiferDescription = (json.get(DESCRIPTION)).asText();
         resultBuilder.setDescription(flowClassiferDescription);
 
         String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText();
         resultBuilder.setEtherType(etherType);
 
-        String protocol = nullIsIllegal(json.get(PROTOCOL), PROTOCOL + MISSING_MEMBER_MESSAGE).asText();
+        String protocol = (json.get(PROTOCOL)).asText();
         resultBuilder.setProtocol(protocol);
 
-        int minSrcPortRange = nullIsIllegal(json.get(MIN_SRC_PORT_RANGE), MIN_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE)
-                .asInt();
+        int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt();
         resultBuilder.setMinSrcPortRange(minSrcPortRange);
 
-        int maxSrcPortRange = nullIsIllegal(json.get(MAX_SRC_PORT_RANGE), MAX_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE)
-                .asInt();
+        int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt();
         resultBuilder.setMaxSrcPortRange(maxSrcPortRange);
 
-        int minDstPortRange = nullIsIllegal(json.get(MIN_DST_PORT_RANGE), MIN_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE)
-                .asInt();
+        int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt();
         resultBuilder.setMinDstPortRange(minDstPortRange);
 
-        int maxDstPortRange = nullIsIllegal(json.get(MAX_DST_PORT_RANGE), MAX_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE)
-                .asInt();
+        int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt();
         resultBuilder.setMaxDstPortRange(maxDstPortRange);
 
-        String srcIpPrefix = nullIsIllegal(json.get(SRC_IP_PREFIX), SRC_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText();
-        resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
+        String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText();
+        if (!srcIpPrefix.isEmpty()) {
+            resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
+        }
 
-        String dstIpPrefix = nullIsIllegal(json.get(DST_IP_PREFIX), DST_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText();
-        resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
+        String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText();
+        if (!dstIpPrefix.isEmpty()) {
+            resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
+        }
 
-        String srcPort = nullIsIllegal(json.get(SRC_PORT), SRC_PORT + MISSING_MEMBER_MESSAGE).asText();
-        resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
+        String srcPort = json.get(SRC_PORT) != null ? (json.get(SRC_PORT)).asText() : "";
+        if (!srcPort.isEmpty()) {
+            resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
+        }
 
-        String dstPort = nullIsIllegal(json.get(DST_PORT), DST_PORT + MISSING_MEMBER_MESSAGE).asText();
-        resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
-
+        String dstPort = json.get(DST_PORT) != null ? (json.get(DST_PORT)).asText() : "";
+        if (!dstPort.isEmpty()) {
+            resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
+        }
         return resultBuilder.build();
     }
 
@@ -115,20 +115,20 @@
     public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) {
         checkNotNull(flowClassifier, "flowClassifier cannot be null");
         ObjectNode result = context.mapper().createObjectNode()
-                .put("FLOW_CLASSIFIER_ID", flowClassifier.flowClassifierId().toString())
-                .put("TENANT_ID", flowClassifier.tenantId().toString())
-                .put("NAME", flowClassifier.name())
-                .put("DESCRIPTION", flowClassifier.description())
-                .put("ETHER_TYPE", flowClassifier.etherType())
-                .put("PROTOCOL", flowClassifier.protocol())
-                .put("MIN_SRC_PORT_RANGE", flowClassifier.minSrcPortRange())
-                .put("MAX_SRC_PORT_RANGE", flowClassifier.maxSrcPortRange())
-                .put("MIN_DST_PORT_RANGE", flowClassifier.minDstPortRange())
-                .put("MAX_DST_PORT_RANGE", flowClassifier.maxDstPortRange())
-                .put("SRC_IP_PREFIX", flowClassifier.srcIpPrefix().toString())
-                .put("DST_IP_PREFIX", flowClassifier.dstIpPrefix().toString())
-                .put("SRC_PORT", flowClassifier.srcPort().toString())
-                .put("DST_PORT", flowClassifier.dstPort().toString());
+                .put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString())
+                .put(TENANT_ID, flowClassifier.tenantId().toString())
+                .put(NAME, flowClassifier.name())
+                .put(DESCRIPTION, flowClassifier.description())
+                .put(ETHER_TYPE, flowClassifier.etherType())
+                .put(PROTOCOL, flowClassifier.protocol())
+                .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange())
+                .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange())
+                .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange())
+                .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange())
+                .put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString())
+                .put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString())
+                .put(SRC_PORT, flowClassifier.srcPort().toString())
+                .put(DST_PORT, flowClassifier.dstPort().toString());
         return result;
     }
 }
diff --git a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java
new file mode 100644
index 0000000..be36aa8
--- /dev/null
+++ b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2015 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.vtnweb.web;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.vtnrsc.FlowClassifier;
+import org.onosproject.vtnrsc.FlowClassifierId;
+import org.onosproject.vtnrsc.TenantId;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Flow classifier codec unit tests.
+ */
+public class FlowClassifierCodecTest {
+
+    SfcCodecContext context;
+    JsonCodec<FlowClassifier> flowClassifierCodec;
+    /**
+     * Sets up for each test.  Creates a context and fetches the flow classifier
+     * codec.
+     */
+    @Before
+    public void setUp() {
+        context = new SfcCodecContext();
+        flowClassifierCodec = context.codec(FlowClassifier.class);
+        assertThat(flowClassifierCodec, notNullValue());
+    }
+
+    /**
+     * Reads in a flow classifier from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the flow classifier
+     * @return decoded flow classifier
+     * @throws IOException if processing the resource fails
+     */
+    private FlowClassifier getFlowClassifier(String resourceName) throws IOException {
+        InputStream jsonStream = FlowClassifierCodecTest.class
+                .getResourceAsStream(resourceName);
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode json = mapper.readTree(jsonStream);
+        assertThat(json, notNullValue());
+        FlowClassifier flowClassifier = flowClassifierCodec.decode((ObjectNode) json, context);
+        assertThat(flowClassifier, notNullValue());
+        return flowClassifier;
+    }
+
+    /**
+     * Checks that a simple flow classifier decodes properly.
+     *
+     * @throws IOException if the resource cannot be processed
+     */
+    @Test
+    public void codecFlowClassifierTest() throws IOException {
+
+        FlowClassifier flowClassifier = getFlowClassifier("flowClassifier.json");
+
+        assertThat(flowClassifier, notNullValue());
+
+        FlowClassifierId flowClassifierId = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051");
+        TenantId tenantId = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1");
+
+        assertThat(flowClassifier.flowClassifierId().toString(), is(flowClassifierId.toString()));
+        assertThat(flowClassifier.name(), is("flow1"));
+        assertThat(flowClassifier.tenantId().toString(), is(tenantId.toString()));
+        assertThat(flowClassifier.description(), is("flow classifier"));
+        assertThat(flowClassifier.protocol(), is("tcp"));
+        assertThat(flowClassifier.minSrcPortRange(), is(22));
+        assertThat(flowClassifier.maxSrcPortRange(), is(4000));
+        assertThat(flowClassifier.minDstPortRange(), is(80));
+        assertThat(flowClassifier.maxDstPortRange(), is(80));
+
+    }
+}
diff --git a/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json b/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json
new file mode 100644
index 0000000..0fc0b74
--- /dev/null
+++ b/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json
@@ -0,0 +1,11 @@
+{
+    "id": "4a334cd4-fe9c-4fae-af4b-321c5e2eb051",
+    "name": "flow1",
+    "tenant_id": "1814726e2d22407b8ca76db5e567dcf1",
+    "description": "flow classifier",
+    "ethertype": "IPv4",
+    "protocol": "tcp",
+    "source_port_range_min": 22, "source_port_range_max": 4000,
+    "destination_port_range_min": 80, "destination_port_range_max": 80,
+    "source_ip_prefix": "1.1.1.1/16" , "destination_ip_prefix": "22.12.34.45/16"
+   }