blob: 94e02dbe9347d75f29dda4dce42f811a8fccdb05 [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";
Phaneendra Manda299877f2016-04-13 23:28:03 +053043 private static final String PRIORITY = "priority";
Bharat saraswalfd923442015-10-26 18:21:10 +053044 private static final String MIN_SRC_PORT_RANGE = "source_port_range_min";
45 private static final String MAX_SRC_PORT_RANGE = "source_port_range_max";
46 private static final String MIN_DST_PORT_RANGE = "destination_port_range_min";
47 private static final String MAX_DST_PORT_RANGE = "destination_port_range_max";
48 private static final String SRC_IP_PREFIX = "source_ip_prefix";
49 private static final String DST_IP_PREFIX = "destination_ip_prefix";
50 private static final String SRC_PORT = "logical_source_port";
51 private static final String DST_PORT = "logical_destination_port";
52 private static final String MISSING_MEMBER_MESSAGE = " member is required in Flow Classifier.";
53
54 @Override
55 public FlowClassifier decode(ObjectNode json, CodecContext context) {
56 if (json == null || !json.isObject()) {
57 return null;
58 }
59
60 FlowClassifier.Builder resultBuilder = new DefaultFlowClassifier.Builder();
61
62 String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID),
63 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +053064 resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId));
Bharat saraswalfd923442015-10-26 18:21:10 +053065
66 String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
67 resultBuilder.setTenantId(TenantId.tenantId(tenantId));
68
69 String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText();
70 resultBuilder.setName(flowClassiferName);
71
Bharat saraswalf215b0c2015-11-14 15:58:46 +053072 String flowClassiferDescription = (json.get(DESCRIPTION)).asText();
Bharat saraswalfd923442015-10-26 18:21:10 +053073 resultBuilder.setDescription(flowClassiferDescription);
74
75 String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText();
76 resultBuilder.setEtherType(etherType);
77
Bharat saraswal3c494e82015-12-10 11:36:56 +053078 if (json.get(PROTOCOL) != null && !(json.get(PROTOCOL)).asText().equals("null")) {
79 String protocol = (json.get(PROTOCOL)).asText();
80 resultBuilder.setProtocol(protocol);
81 }
Bharat saraswalfd923442015-10-26 18:21:10 +053082
Phaneendra Manda299877f2016-04-13 23:28:03 +053083 int priority = (json.get(PRIORITY)).asInt();
84 resultBuilder.setPriority(priority);
85
Bharat saraswalf215b0c2015-11-14 15:58:46 +053086 int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053087 resultBuilder.setMinSrcPortRange(minSrcPortRange);
88
Bharat saraswalf215b0c2015-11-14 15:58:46 +053089 int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053090 resultBuilder.setMaxSrcPortRange(maxSrcPortRange);
91
Bharat saraswalf215b0c2015-11-14 15:58:46 +053092 int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053093 resultBuilder.setMinDstPortRange(minDstPortRange);
94
Bharat saraswalf215b0c2015-11-14 15:58:46 +053095 int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt();
Bharat saraswalfd923442015-10-26 18:21:10 +053096 resultBuilder.setMaxDstPortRange(maxDstPortRange);
97
Bharat saraswal3c494e82015-12-10 11:36:56 +053098 if (json.get(SRC_IP_PREFIX) != null && !(json.get(SRC_IP_PREFIX)).asText().equals("null")) {
99 String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530100 resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
101 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530102
Bharat saraswal3c494e82015-12-10 11:36:56 +0530103 if (json.get(DST_IP_PREFIX) != null && !(json.get(DST_IP_PREFIX)).asText().equals("null")) {
104 String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530105 resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
106 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530107
Bharat saraswal3c494e82015-12-10 11:36:56 +0530108 if (json.get(SRC_PORT) != null && !(json.get(SRC_PORT)).asText().equals("null")) {
109 String srcPort = (json.get(SRC_PORT)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530110 resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
111 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530112
Bharat saraswal3c494e82015-12-10 11:36:56 +0530113 if (json.get(DST_PORT) != null && !(json.get(DST_PORT)).asText().equals("null")) {
114 String dstPort = (json.get(DST_PORT)).asText();
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530115 resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
116 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530117 return resultBuilder.build();
118 }
119
120 @Override
121 public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) {
122 checkNotNull(flowClassifier, "flowClassifier cannot be null");
Bharat saraswal3c494e82015-12-10 11:36:56 +0530123 ObjectNode result = context.mapper().createObjectNode();
124 result.put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString())
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530125 .put(TENANT_ID, flowClassifier.tenantId().toString())
126 .put(NAME, flowClassifier.name())
127 .put(DESCRIPTION, flowClassifier.description())
128 .put(ETHER_TYPE, flowClassifier.etherType())
129 .put(PROTOCOL, flowClassifier.protocol())
Phaneendra Manda299877f2016-04-13 23:28:03 +0530130 .put(PRIORITY, flowClassifier.priority())
Bharat saraswalf215b0c2015-11-14 15:58:46 +0530131 .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange())
132 .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange())
133 .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange())
Bharat saraswal3c494e82015-12-10 11:36:56 +0530134 .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange());
135
136 if (flowClassifier.srcIpPrefix() != null) {
137 result.put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString());
138 } else {
139 result.put(SRC_IP_PREFIX, "null");
140 }
141 if (flowClassifier.dstIpPrefix() != null) {
142 result.put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString());
143 } else {
144 result.put(DST_IP_PREFIX, "null");
145 }
146
147 if (flowClassifier.srcPort() != null) {
148 result.put(SRC_PORT, flowClassifier.srcPort().toString());
149 } else {
150 result.put(SRC_PORT, "null");
151 }
152 if (flowClassifier.dstPort() != null) {
153 result.put(DST_PORT, flowClassifier.dstPort().toString());
154 } else {
155 result.put(DST_PORT, "null");
156 }
Bharat saraswalfd923442015-10-26 18:21:10 +0530157 return result;
158 }
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +0530159}