Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacktelemetry.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.apache.commons.lang3.exception.ExceptionUtils; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 25 | import org.onosproject.openstacktelemetry.impl.DefaultStatsFlowRule; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | public class StatsFlowRuleJsonCodec extends JsonCodec<StatsFlowRule> { |
| 34 | |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
| 37 | public static final String SRC_IP_PREFIX = "srcIpPrefix"; |
| 38 | public static final String DST_IP_PREFIX = "dstIpPrefix"; |
| 39 | public static final String IP_PROTOCOL = "ipProtocol"; |
| 40 | public static final String SRC_TP_PORT = "srcTpPort"; |
| 41 | public static final String DST_TP_PORT = "dstTpPort"; |
| 42 | |
| 43 | public ObjectNode encode(StatsFlowRule flowRule, CodecContext context) { |
| 44 | checkNotNull(flowRule, "FlowInfo cannot be null"); |
| 45 | ObjectNode result = context.mapper().createObjectNode() |
| 46 | .put(SRC_IP_PREFIX, flowRule.srcIpPrefix().toString()) |
| 47 | .put(DST_IP_PREFIX, flowRule.dstIpPrefix().toString()) |
| 48 | .put(IP_PROTOCOL, flowRule.ipProtocol()) |
| 49 | .put(SRC_TP_PORT, flowRule.srcTpPort().toString()) |
| 50 | .put(DST_TP_PORT, flowRule.dstTpPort().toString()); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public StatsFlowRule decode(ObjectNode json, CodecContext context) { |
| 56 | if (json == null || !json.isObject()) { |
| 57 | return null; |
| 58 | } |
| 59 | try { |
| 60 | String srcIpPrefix = json.get(SRC_IP_PREFIX).asText(); |
| 61 | String dstIpPrefix = json.get(DST_IP_PREFIX).asText(); |
| 62 | String tmpIpProtocol = new String(""); |
| 63 | int srcTpPort = 0; |
| 64 | int dstTpPort = 0; |
| 65 | |
| 66 | DefaultStatsFlowRule.Builder flowRuleBuilder; |
| 67 | |
| 68 | byte ipProtocol = 0; |
| 69 | if (json.get(IP_PROTOCOL) == null) { |
| 70 | log.info("ipProtocol: null"); |
| 71 | flowRuleBuilder = DefaultStatsFlowRule.builder() |
| 72 | .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix)) |
| 73 | .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); |
| 74 | } else { |
| 75 | tmpIpProtocol = json.get(IP_PROTOCOL).asText().toUpperCase(); |
| 76 | srcTpPort = json.get(SRC_TP_PORT).asInt(); |
| 77 | dstTpPort = json.get(DST_TP_PORT).asInt(); |
| 78 | if (tmpIpProtocol.equals("TCP")) { |
| 79 | ipProtocol = IPv4.PROTOCOL_TCP; |
| 80 | } else if (tmpIpProtocol.equals("UDP")) { |
| 81 | ipProtocol = IPv4.PROTOCOL_UDP; |
| 82 | } else { |
| 83 | ipProtocol = 0; |
| 84 | } |
| 85 | |
| 86 | flowRuleBuilder = DefaultStatsFlowRule.builder() |
| 87 | .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix)) |
| 88 | .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix)) |
| 89 | .ipProtocol(ipProtocol) |
| 90 | .srcTpPort(TpPort.tpPort(srcTpPort)) |
| 91 | .dstTpPort(TpPort.tpPort(dstTpPort)); |
| 92 | } |
| 93 | |
| 94 | log.debug("StatsFlowRule after building from JSON:\n{}", |
| 95 | flowRuleBuilder.build().toString()); |
| 96 | |
| 97 | return flowRuleBuilder.build(); |
| 98 | } catch (Exception ex) { |
| 99 | log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex)); |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | } |