blob: 412cefe65c9cbd49593bab877082b49fe76a8e6d [file] [log] [blame]
Bharat saraswalfd923442015-10-26 18:21:10 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bharat saraswalfd923442015-10-26 18:21:10 +05303 *
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 */
samueljcce3d5b6a2015-10-27 15:52:52 -070016package org.onosproject.vtnweb.web;
Bharat saraswalfd923442015-10-26 18:21:10 +053017
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.util.Tools.nullIsIllegal;
20
Bharat saraswalfd923442015-10-26 18:21:10 +053021import org.onlab.packet.IpPrefix;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.vtnrsc.DefaultFlowClassifier;
25import org.onosproject.vtnrsc.FlowClassifier;
26import org.onosproject.vtnrsc.FlowClassifierId;
Bharat saraswalfd923442015-10-26 18:21:10 +053027import org.onosproject.vtnrsc.TenantId;
Bharat saraswalf215b0c2015-11-14 15:58:46 +053028import org.onosproject.vtnrsc.VirtualPortId;
Bharat saraswalfd923442015-10-26 18:21:10 +053029
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
32/**
33 * Flow Classifier JSON codec.
34 */
35public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
36
37 private static final String FLOW_CLASSIFIER_ID = "id";
38 private static final String TENANT_ID = "tenant_id";
39 private static final String NAME = "name";
40 private static final String DESCRIPTION = "description";
Bharat saraswalf215b0c2015-11-14 15:58:46 +053041 private static final String ETHER_TYPE = "ethertype";
Bharat saraswalfd923442015-10-26 18:21:10 +053042 private static final String PROTOCOL = "protocol";
43 private static final String MIN_SRC_PORT_RANGE = "source_port_range_min";
44 private static final String MAX_SRC_PORT_RANGE = "source_port_range_max";
45 private static final String MIN_DST_PORT_RANGE = "destination_port_range_min";
46 private static final String MAX_DST_PORT_RANGE = "destination_port_range_max";
47 private static final String SRC_IP_PREFIX = "source_ip_prefix";
48 private static final String DST_IP_PREFIX = "destination_ip_prefix";
49 private static final String SRC_PORT = "logical_source_port";
50 private static final String DST_PORT = "logical_destination_port";
51 private static final String MISSING_MEMBER_MESSAGE = " member is required in Flow Classifier.";
52
53 @Override
54 public FlowClassifier decode(ObjectNode json, CodecContext context) {
55 if (json == null || !json.isObject()) {
56 return null;
57 }
58
59 FlowClassifier.Builder resultBuilder = new DefaultFlowClassifier.Builder();
60
61 String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID),
62 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +053063 resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId));
Bharat saraswalfd923442015-10-26 18:21:10 +053064
65 String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
66 resultBuilder.setTenantId(TenantId.tenantId(tenantId));
67
68 String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText();
69 resultBuilder.setName(flowClassiferName);
70
Bharat saraswalf215b0c2015-11-14 15:58:46 +053071 String flowClassiferDescription = (json.get(DESCRIPTION)).asText();
Bharat saraswalfd923442015-10-26 18:21:10 +053072 resultBuilder.setDescription(flowClassiferDescription);
73
74 String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText();
75 resultBuilder.setEtherType(etherType);
76
Bharat saraswal3c494e82015-12-10 11:36:56 +053077 if (json.get(PROTOCOL) != null && !(json.get(PROTOCOL)).asText().equals("null")) {
78 String protocol = (json.get(PROTOCOL)).asText();
79 resultBuilder.setProtocol(protocol);
80 }
Bharat saraswalfd923442015-10-26 18:21:10 +053081
Bharat saraswalf215b0c2015-11-14 15:58:46 +053082 int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053083 resultBuilder.setMinSrcPortRange(minSrcPortRange);
84
Bharat saraswalf215b0c2015-11-14 15:58:46 +053085 int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053086 resultBuilder.setMaxSrcPortRange(maxSrcPortRange);
87
Bharat saraswalf215b0c2015-11-14 15:58:46 +053088 int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053089 resultBuilder.setMinDstPortRange(minDstPortRange);
90
Bharat saraswalf215b0c2015-11-14 15:58:46 +053091 int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053092 resultBuilder.setMaxDstPortRange(maxDstPortRange);
93
Bharat saraswal3c494e82015-12-10 11:36:56 +053094 if (json.get(SRC_IP_PREFIX) != null && !(json.get(SRC_IP_PREFIX)).asText().equals("null")) {
95 String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +053096 resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
97 }
Bharat saraswalfd923442015-10-26 18:21:10 +053098
Bharat saraswal3c494e82015-12-10 11:36:56 +053099 if (json.get(DST_IP_PREFIX) != null && !(json.get(DST_IP_PREFIX)).asText().equals("null")) {
100 String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530101 resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
102 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530103
Bharat saraswal3c494e82015-12-10 11:36:56 +0530104 if (json.get(SRC_PORT) != null && !(json.get(SRC_PORT)).asText().equals("null")) {
105 String srcPort = (json.get(SRC_PORT)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530106 resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
107 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530108
Bharat saraswal3c494e82015-12-10 11:36:56 +0530109 if (json.get(DST_PORT) != null && !(json.get(DST_PORT)).asText().equals("null")) {
110 String dstPort = (json.get(DST_PORT)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530111 resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
112 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530113 return resultBuilder.build();
114 }
115
116 @Override
117 public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) {
118 checkNotNull(flowClassifier, "flowClassifier cannot be null");
Bharat saraswal3c494e82015-12-10 11:36:56 +0530119 ObjectNode result = context.mapper().createObjectNode();
120 result.put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString())
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530121 .put(TENANT_ID, flowClassifier.tenantId().toString())
122 .put(NAME, flowClassifier.name())
123 .put(DESCRIPTION, flowClassifier.description())
124 .put(ETHER_TYPE, flowClassifier.etherType())
125 .put(PROTOCOL, flowClassifier.protocol())
126 .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange())
127 .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange())
128 .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange())
Bharat saraswal3c494e82015-12-10 11:36:56 +0530129 .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange());
130
131 if (flowClassifier.srcIpPrefix() != null) {
132 result.put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString());
133 } else {
134 result.put(SRC_IP_PREFIX, "null");
135 }
136 if (flowClassifier.dstIpPrefix() != null) {
137 result.put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString());
138 } else {
139 result.put(DST_IP_PREFIX, "null");
140 }
141
142 if (flowClassifier.srcPort() != null) {
143 result.put(SRC_PORT, flowClassifier.srcPort().toString());
144 } else {
145 result.put(SRC_PORT, "null");
146 }
147 if (flowClassifier.dstPort() != null) {
148 result.put(DST_PORT, flowClassifier.dstPort().toString());
149 } else {
150 result.put(DST_PORT, "null");
151 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530152 return result;
153 }
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +0530154}