Bharat saraswal
Committed by Gerrit Code Review

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

Change-Id: I86a5745e0698a79b9d015a902c79606126d28400
...@@ -53,5 +53,25 @@ ...@@ -53,5 +53,25 @@
53 <artifactId>easymock</artifactId> 53 <artifactId>easymock</artifactId>
54 <scope>test</scope> 54 <scope>test</scope>
55 </dependency> 55 </dependency>
56 + <dependency>
57 + <groupId>org.onosproject</groupId>
58 + <artifactId>onlab-osgi</artifactId>
59 + <classifier>tests</classifier>
60 + <scope>test</scope>
61 + </dependency>
62 + <dependency>
63 + <groupId>com.sun.jersey</groupId>
64 + <artifactId>jersey-servlet</artifactId>
65 + </dependency>
66 + <dependency>
67 + <groupId>com.sun.jersey.jersey-test-framework</groupId>
68 + <artifactId>jersey-test-framework-core</artifactId>
69 + <scope>test</scope>
70 + </dependency>
71 + <dependency>
72 + <groupId>com.sun.jersey.jersey-test-framework</groupId>
73 + <artifactId>jersey-test-framework-grizzly2</artifactId>
74 + <scope>test</scope>
75 + </dependency>
56 </dependencies> 76 </dependencies>
57 </project> 77 </project>
......
...@@ -18,16 +18,14 @@ package org.onosproject.vtnweb.web; ...@@ -18,16 +18,14 @@ package org.onosproject.vtnweb.web;
18 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.onlab.util.Tools.nullIsIllegal; 19 import static org.onlab.util.Tools.nullIsIllegal;
20 20
21 -import java.util.UUID;
22 -
23 import org.onlab.packet.IpPrefix; 21 import org.onlab.packet.IpPrefix;
24 import org.onosproject.codec.CodecContext; 22 import org.onosproject.codec.CodecContext;
25 import org.onosproject.codec.JsonCodec; 23 import org.onosproject.codec.JsonCodec;
26 import org.onosproject.vtnrsc.DefaultFlowClassifier; 24 import org.onosproject.vtnrsc.DefaultFlowClassifier;
27 import org.onosproject.vtnrsc.FlowClassifier; 25 import org.onosproject.vtnrsc.FlowClassifier;
28 import org.onosproject.vtnrsc.FlowClassifierId; 26 import org.onosproject.vtnrsc.FlowClassifierId;
29 -import org.onosproject.vtnrsc.VirtualPortId;
30 import org.onosproject.vtnrsc.TenantId; 27 import org.onosproject.vtnrsc.TenantId;
28 +import org.onosproject.vtnrsc.VirtualPortId;
31 29
32 import com.fasterxml.jackson.databind.node.ObjectNode; 30 import com.fasterxml.jackson.databind.node.ObjectNode;
33 31
...@@ -40,7 +38,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { ...@@ -40,7 +38,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
40 private static final String TENANT_ID = "tenant_id"; 38 private static final String TENANT_ID = "tenant_id";
41 private static final String NAME = "name"; 39 private static final String NAME = "name";
42 private static final String DESCRIPTION = "description"; 40 private static final String DESCRIPTION = "description";
43 - private static final String ETHER_TYPE = "etherType"; 41 + private static final String ETHER_TYPE = "ethertype";
44 private static final String PROTOCOL = "protocol"; 42 private static final String PROTOCOL = "protocol";
45 private static final String MIN_SRC_PORT_RANGE = "source_port_range_min"; 43 private static final String MIN_SRC_PORT_RANGE = "source_port_range_min";
46 private static final String MAX_SRC_PORT_RANGE = "source_port_range_max"; 44 private static final String MAX_SRC_PORT_RANGE = "source_port_range_max";
...@@ -62,7 +60,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { ...@@ -62,7 +60,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
62 60
63 String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID), 61 String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID),
64 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText(); 62 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText();
65 - resultBuilder.setFlowClassifierId(FlowClassifierId.of(UUID.fromString(flowClassifierId))); 63 + resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId));
66 64
67 String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); 65 String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
68 resultBuilder.setTenantId(TenantId.tenantId(tenantId)); 66 resultBuilder.setTenantId(TenantId.tenantId(tenantId));
...@@ -70,44 +68,46 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { ...@@ -70,44 +68,46 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
70 String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText(); 68 String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText();
71 resultBuilder.setName(flowClassiferName); 69 resultBuilder.setName(flowClassiferName);
72 70
73 - String flowClassiferDescription = nullIsIllegal(json.get(DESCRIPTION), DESCRIPTION + MISSING_MEMBER_MESSAGE) 71 + String flowClassiferDescription = (json.get(DESCRIPTION)).asText();
74 - .asText();
75 resultBuilder.setDescription(flowClassiferDescription); 72 resultBuilder.setDescription(flowClassiferDescription);
76 73
77 String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText(); 74 String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText();
78 resultBuilder.setEtherType(etherType); 75 resultBuilder.setEtherType(etherType);
79 76
80 - String protocol = nullIsIllegal(json.get(PROTOCOL), PROTOCOL + MISSING_MEMBER_MESSAGE).asText(); 77 + String protocol = (json.get(PROTOCOL)).asText();
81 resultBuilder.setProtocol(protocol); 78 resultBuilder.setProtocol(protocol);
82 79
83 - int minSrcPortRange = nullIsIllegal(json.get(MIN_SRC_PORT_RANGE), MIN_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE) 80 + int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt();
84 - .asInt();
85 resultBuilder.setMinSrcPortRange(minSrcPortRange); 81 resultBuilder.setMinSrcPortRange(minSrcPortRange);
86 82
87 - int maxSrcPortRange = nullIsIllegal(json.get(MAX_SRC_PORT_RANGE), MAX_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE) 83 + int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt();
88 - .asInt();
89 resultBuilder.setMaxSrcPortRange(maxSrcPortRange); 84 resultBuilder.setMaxSrcPortRange(maxSrcPortRange);
90 85
91 - int minDstPortRange = nullIsIllegal(json.get(MIN_DST_PORT_RANGE), MIN_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE) 86 + int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt();
92 - .asInt();
93 resultBuilder.setMinDstPortRange(minDstPortRange); 87 resultBuilder.setMinDstPortRange(minDstPortRange);
94 88
95 - int maxDstPortRange = nullIsIllegal(json.get(MAX_DST_PORT_RANGE), MAX_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE) 89 + int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt();
96 - .asInt();
97 resultBuilder.setMaxDstPortRange(maxDstPortRange); 90 resultBuilder.setMaxDstPortRange(maxDstPortRange);
98 91
99 - String srcIpPrefix = nullIsIllegal(json.get(SRC_IP_PREFIX), SRC_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText(); 92 + String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText();
93 + if (!srcIpPrefix.isEmpty()) {
100 resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix)); 94 resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
95 + }
101 96
102 - String dstIpPrefix = nullIsIllegal(json.get(DST_IP_PREFIX), DST_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText(); 97 + String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText();
98 + if (!dstIpPrefix.isEmpty()) {
103 resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); 99 resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
100 + }
104 101
105 - String srcPort = nullIsIllegal(json.get(SRC_PORT), SRC_PORT + MISSING_MEMBER_MESSAGE).asText(); 102 + String srcPort = json.get(SRC_PORT) != null ? (json.get(SRC_PORT)).asText() : "";
103 + if (!srcPort.isEmpty()) {
106 resultBuilder.setSrcPort(VirtualPortId.portId(srcPort)); 104 resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
105 + }
107 106
108 - String dstPort = nullIsIllegal(json.get(DST_PORT), DST_PORT + MISSING_MEMBER_MESSAGE).asText(); 107 + String dstPort = json.get(DST_PORT) != null ? (json.get(DST_PORT)).asText() : "";
108 + if (!dstPort.isEmpty()) {
109 resultBuilder.setDstPort(VirtualPortId.portId(dstPort)); 109 resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
110 - 110 + }
111 return resultBuilder.build(); 111 return resultBuilder.build();
112 } 112 }
113 113
...@@ -115,20 +115,20 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { ...@@ -115,20 +115,20 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
115 public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) { 115 public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) {
116 checkNotNull(flowClassifier, "flowClassifier cannot be null"); 116 checkNotNull(flowClassifier, "flowClassifier cannot be null");
117 ObjectNode result = context.mapper().createObjectNode() 117 ObjectNode result = context.mapper().createObjectNode()
118 - .put("FLOW_CLASSIFIER_ID", flowClassifier.flowClassifierId().toString()) 118 + .put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString())
119 - .put("TENANT_ID", flowClassifier.tenantId().toString()) 119 + .put(TENANT_ID, flowClassifier.tenantId().toString())
120 - .put("NAME", flowClassifier.name()) 120 + .put(NAME, flowClassifier.name())
121 - .put("DESCRIPTION", flowClassifier.description()) 121 + .put(DESCRIPTION, flowClassifier.description())
122 - .put("ETHER_TYPE", flowClassifier.etherType()) 122 + .put(ETHER_TYPE, flowClassifier.etherType())
123 - .put("PROTOCOL", flowClassifier.protocol()) 123 + .put(PROTOCOL, flowClassifier.protocol())
124 - .put("MIN_SRC_PORT_RANGE", flowClassifier.minSrcPortRange()) 124 + .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange())
125 - .put("MAX_SRC_PORT_RANGE", flowClassifier.maxSrcPortRange()) 125 + .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange())
126 - .put("MIN_DST_PORT_RANGE", flowClassifier.minDstPortRange()) 126 + .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange())
127 - .put("MAX_DST_PORT_RANGE", flowClassifier.maxDstPortRange()) 127 + .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange())
128 - .put("SRC_IP_PREFIX", flowClassifier.srcIpPrefix().toString()) 128 + .put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString())
129 - .put("DST_IP_PREFIX", flowClassifier.dstIpPrefix().toString()) 129 + .put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString())
130 - .put("SRC_PORT", flowClassifier.srcPort().toString()) 130 + .put(SRC_PORT, flowClassifier.srcPort().toString())
131 - .put("DST_PORT", flowClassifier.dstPort().toString()); 131 + .put(DST_PORT, flowClassifier.dstPort().toString());
132 return result; 132 return result;
133 } 133 }
134 } 134 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.vtnweb.web;
17 +
18 +import static org.hamcrest.MatcherAssert.assertThat;
19 +import static org.hamcrest.Matchers.is;
20 +import static org.hamcrest.Matchers.notNullValue;
21 +
22 +import java.io.IOException;
23 +import java.io.InputStream;
24 +
25 +import org.junit.Before;
26 +import org.junit.Test;
27 +import org.onosproject.codec.JsonCodec;
28 +import org.onosproject.vtnrsc.FlowClassifier;
29 +import org.onosproject.vtnrsc.FlowClassifierId;
30 +import org.onosproject.vtnrsc.TenantId;
31 +
32 +import com.fasterxml.jackson.databind.JsonNode;
33 +import com.fasterxml.jackson.databind.ObjectMapper;
34 +import com.fasterxml.jackson.databind.node.ObjectNode;
35 +
36 +/**
37 + * Flow classifier codec unit tests.
38 + */
39 +public class FlowClassifierCodecTest {
40 +
41 + SfcCodecContext context;
42 + JsonCodec<FlowClassifier> flowClassifierCodec;
43 + /**
44 + * Sets up for each test. Creates a context and fetches the flow classifier
45 + * codec.
46 + */
47 + @Before
48 + public void setUp() {
49 + context = new SfcCodecContext();
50 + flowClassifierCodec = context.codec(FlowClassifier.class);
51 + assertThat(flowClassifierCodec, notNullValue());
52 + }
53 +
54 + /**
55 + * Reads in a flow classifier from the given resource and decodes it.
56 + *
57 + * @param resourceName resource to use to read the JSON for the flow classifier
58 + * @return decoded flow classifier
59 + * @throws IOException if processing the resource fails
60 + */
61 + private FlowClassifier getFlowClassifier(String resourceName) throws IOException {
62 + InputStream jsonStream = FlowClassifierCodecTest.class
63 + .getResourceAsStream(resourceName);
64 + ObjectMapper mapper = new ObjectMapper();
65 + JsonNode json = mapper.readTree(jsonStream);
66 + assertThat(json, notNullValue());
67 + FlowClassifier flowClassifier = flowClassifierCodec.decode((ObjectNode) json, context);
68 + assertThat(flowClassifier, notNullValue());
69 + return flowClassifier;
70 + }
71 +
72 + /**
73 + * Checks that a simple flow classifier decodes properly.
74 + *
75 + * @throws IOException if the resource cannot be processed
76 + */
77 + @Test
78 + public void codecFlowClassifierTest() throws IOException {
79 +
80 + FlowClassifier flowClassifier = getFlowClassifier("flowClassifier.json");
81 +
82 + assertThat(flowClassifier, notNullValue());
83 +
84 + FlowClassifierId flowClassifierId = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051");
85 + TenantId tenantId = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1");
86 +
87 + assertThat(flowClassifier.flowClassifierId().toString(), is(flowClassifierId.toString()));
88 + assertThat(flowClassifier.name(), is("flow1"));
89 + assertThat(flowClassifier.tenantId().toString(), is(tenantId.toString()));
90 + assertThat(flowClassifier.description(), is("flow classifier"));
91 + assertThat(flowClassifier.protocol(), is("tcp"));
92 + assertThat(flowClassifier.minSrcPortRange(), is(22));
93 + assertThat(flowClassifier.maxSrcPortRange(), is(4000));
94 + assertThat(flowClassifier.minDstPortRange(), is(80));
95 + assertThat(flowClassifier.maxDstPortRange(), is(80));
96 +
97 + }
98 +}
1 +{
2 + "id": "4a334cd4-fe9c-4fae-af4b-321c5e2eb051",
3 + "name": "flow1",
4 + "tenant_id": "1814726e2d22407b8ca76db5e567dcf1",
5 + "description": "flow classifier",
6 + "ethertype": "IPv4",
7 + "protocol": "tcp",
8 + "source_port_range_min": 22, "source_port_range_max": 4000,
9 + "destination_port_range_min": 80, "destination_port_range_max": 80,
10 + "source_ip_prefix": "1.1.1.1/16" , "destination_ip_prefix": "22.12.34.45/16"
11 + }