Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.openstackvtap.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
| 25 | import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion; |
| 26 | |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolStringFromType; |
| 29 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString; |
| 30 | |
| 31 | /** |
| 32 | * Openstack vTap codec used for serializing and de-serializing JSON string. |
| 33 | */ |
| 34 | public final class OpenstackVtapCriterionCodec extends JsonCodec<OpenstackVtapCriterion> { |
| 35 | |
| 36 | private static final String SRC_IP = "srcIp"; |
| 37 | private static final String DST_IP = "dstIp"; |
| 38 | private static final String IP_PROTOCOL = "ipProto"; |
| 39 | private static final String SRC_PORT = "srcPort"; |
| 40 | private static final String DST_PORT = "dstPort"; |
| 41 | |
| 42 | private static final String MISSING_MESSAGE = " is required in OpenstackVtapCriterion"; |
| 43 | |
| 44 | @Override |
| 45 | public ObjectNode encode(OpenstackVtapCriterion entity, CodecContext context) { |
| 46 | String protoStr = getProtocolStringFromType(entity.ipProtocol()); |
| 47 | |
| 48 | return context.mapper().createObjectNode() |
| 49 | .put(SRC_IP, entity.srcIpPrefix().address().toString()) |
| 50 | .put(DST_IP, entity.dstIpPrefix().address().toString()) |
| 51 | .put(IP_PROTOCOL, protoStr) |
| 52 | .put(SRC_PORT, entity.srcTpPort().toString()) |
| 53 | .put(DST_PORT, entity.dstTpPort().toString()); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public OpenstackVtapCriterion decode(ObjectNode json, CodecContext context) { |
| 58 | if (json == null || !json.isObject()) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | OpenstackVtapCriterion.Builder cBuilder = DefaultOpenstackVtapCriterion.builder(); |
| 63 | |
| 64 | // parse source IP address |
| 65 | IpPrefix srcIp = IpPrefix.valueOf(IpAddress.valueOf(nullIsIllegal( |
| 66 | json.get(SRC_IP).asText(), SRC_IP + MISSING_MESSAGE)), 32); |
| 67 | // parse destination IP address |
| 68 | IpPrefix dstIp = IpPrefix.valueOf(IpAddress.valueOf(nullIsIllegal( |
| 69 | json.get(DST_IP).asText(), DST_IP + MISSING_MESSAGE)), 32); |
| 70 | |
| 71 | cBuilder.srcIpPrefix(srcIp); |
| 72 | cBuilder.dstIpPrefix(dstIp); |
| 73 | |
| 74 | // parse IP protocol |
| 75 | String ipProtoStr = json.get(IP_PROTOCOL).asText(); |
| 76 | if (ipProtoStr != null) { |
| 77 | cBuilder.ipProtocol(getProtocolTypeFromString(ipProtoStr)); |
| 78 | } |
| 79 | |
| 80 | // parse source port number |
| 81 | int srcPort = json.get(SRC_PORT).asInt(0); |
| 82 | |
| 83 | // parse destination port number |
| 84 | int dstPort = json.get(DST_PORT).asInt(0); |
| 85 | |
| 86 | cBuilder.srcTpPort(TpPort.tpPort(srcPort)); |
| 87 | cBuilder.dstTpPort(TpPort.tpPort(dstPort)); |
| 88 | |
| 89 | return cBuilder.build(); |
| 90 | } |
| 91 | } |