Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 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 | */ |
samueljcc | e3d5b6a | 2015-10-27 15:52:52 -0700 | [diff] [blame] | 16 | package org.onosproject.vtnweb.web; |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.util.Tools.nullIsIllegal; |
| 20 | |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.vtnrsc.DefaultFlowClassifier; |
| 25 | import org.onosproject.vtnrsc.FlowClassifier; |
| 26 | import org.onosproject.vtnrsc.FlowClassifierId; |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 27 | import org.onosproject.vtnrsc.TenantId; |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 28 | import org.onosproject.vtnrsc.VirtualPortId; |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 29 | |
| 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 31 | |
| 32 | /** |
| 33 | * Flow Classifier JSON codec. |
| 34 | */ |
| 35 | public 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 saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 41 | private static final String ETHER_TYPE = "ethertype"; |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 42 | private static final String PROTOCOL = "protocol"; |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 43 | private static final String PRIORITY = "priority"; |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 44 | 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 saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 64 | resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId)); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 65 | |
| 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 saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 72 | String flowClassiferDescription = (json.get(DESCRIPTION)).asText(); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 73 | resultBuilder.setDescription(flowClassiferDescription); |
| 74 | |
| 75 | String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText(); |
| 76 | resultBuilder.setEtherType(etherType); |
| 77 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 78 | if (json.get(PROTOCOL) != null && !"null".equals((json.get(PROTOCOL)).asText())) { |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 79 | String protocol = (json.get(PROTOCOL)).asText(); |
| 80 | resultBuilder.setProtocol(protocol); |
| 81 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 82 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 83 | if (json.get(PRIORITY) != null && !"null".equals((json.get(PRIORITY)).asText())) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 84 | int priority = (json.get(PRIORITY)).asInt(); |
| 85 | resultBuilder.setPriority(priority); |
| 86 | } |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 87 | |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 88 | int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt(); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 89 | resultBuilder.setMinSrcPortRange(minSrcPortRange); |
| 90 | |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 91 | int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt(); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 92 | resultBuilder.setMaxSrcPortRange(maxSrcPortRange); |
| 93 | |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 94 | int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt(); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 95 | resultBuilder.setMinDstPortRange(minDstPortRange); |
| 96 | |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 97 | int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt(); |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 98 | resultBuilder.setMaxDstPortRange(maxDstPortRange); |
| 99 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 100 | if (json.get(SRC_IP_PREFIX) != null && !"null".equals((json.get(SRC_IP_PREFIX)).asText())) { |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 101 | String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText(); |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 102 | resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix)); |
| 103 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 104 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 105 | if (json.get(DST_IP_PREFIX) != null && !"null".equals((json.get(DST_IP_PREFIX)).asText())) { |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 106 | String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText(); |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 107 | resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); |
| 108 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 109 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 110 | if (json.get(SRC_PORT) != null && !"null".equals((json.get(SRC_PORT)).asText())) { |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 111 | String srcPort = (json.get(SRC_PORT)).asText(); |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 112 | resultBuilder.setSrcPort(VirtualPortId.portId(srcPort)); |
| 113 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 114 | |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 115 | if (json.get(DST_PORT) != null && !"null".equals((json.get(DST_PORT)).asText())) { |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 116 | String dstPort = (json.get(DST_PORT)).asText(); |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 117 | resultBuilder.setDstPort(VirtualPortId.portId(dstPort)); |
| 118 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 119 | return resultBuilder.build(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) { |
| 124 | checkNotNull(flowClassifier, "flowClassifier cannot be null"); |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 125 | ObjectNode result = context.mapper().createObjectNode(); |
| 126 | result.put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString()) |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 127 | .put(TENANT_ID, flowClassifier.tenantId().toString()) |
| 128 | .put(NAME, flowClassifier.name()) |
| 129 | .put(DESCRIPTION, flowClassifier.description()) |
| 130 | .put(ETHER_TYPE, flowClassifier.etherType()) |
| 131 | .put(PROTOCOL, flowClassifier.protocol()) |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 132 | .put(PRIORITY, flowClassifier.priority()) |
Bharat saraswal | f215b0c | 2015-11-14 15:58:46 +0530 | [diff] [blame] | 133 | .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange()) |
| 134 | .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange()) |
| 135 | .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange()) |
Bharat saraswal | 3c494e8 | 2015-12-10 11:36:56 +0530 | [diff] [blame] | 136 | .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange()); |
| 137 | |
| 138 | if (flowClassifier.srcIpPrefix() != null) { |
| 139 | result.put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString()); |
| 140 | } else { |
| 141 | result.put(SRC_IP_PREFIX, "null"); |
| 142 | } |
| 143 | if (flowClassifier.dstIpPrefix() != null) { |
| 144 | result.put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString()); |
| 145 | } else { |
| 146 | result.put(DST_IP_PREFIX, "null"); |
| 147 | } |
| 148 | |
| 149 | if (flowClassifier.srcPort() != null) { |
| 150 | result.put(SRC_PORT, flowClassifier.srcPort().toString()); |
| 151 | } else { |
| 152 | result.put(SRC_PORT, "null"); |
| 153 | } |
| 154 | if (flowClassifier.dstPort() != null) { |
| 155 | result.put(DST_PORT, flowClassifier.dstPort().toString()); |
| 156 | } else { |
| 157 | result.put(DST_PORT, "null"); |
| 158 | } |
Bharat saraswal | fd92344 | 2015-10-26 18:21:10 +0530 | [diff] [blame] | 159 | return result; |
| 160 | } |
Mahesh Poojary Huawei | 321cc50 | 2015-11-03 13:23:32 +0530 | [diff] [blame] | 161 | } |