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; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 20 | import org.onlab.packet.TpPort; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 24 | import org.onosproject.openstacktelemetry.impl.DefaultStatsFlowRule; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 30 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getProtocolTypeFromString; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 33 | /** |
| 34 | * JSON codec for StatsFlowRule. |
| 35 | */ |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 36 | public class StatsFlowRuleJsonCodec extends JsonCodec<StatsFlowRule> { |
| 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | public static final String SRC_IP_PREFIX = "srcIpPrefix"; |
| 41 | public static final String DST_IP_PREFIX = "dstIpPrefix"; |
| 42 | public static final String IP_PROTOCOL = "ipProtocol"; |
| 43 | public static final String SRC_TP_PORT = "srcTpPort"; |
| 44 | public static final String DST_TP_PORT = "dstTpPort"; |
| 45 | |
| 46 | public ObjectNode encode(StatsFlowRule flowRule, CodecContext context) { |
| 47 | checkNotNull(flowRule, "FlowInfo cannot be null"); |
| 48 | ObjectNode result = context.mapper().createObjectNode() |
| 49 | .put(SRC_IP_PREFIX, flowRule.srcIpPrefix().toString()) |
| 50 | .put(DST_IP_PREFIX, flowRule.dstIpPrefix().toString()) |
| 51 | .put(IP_PROTOCOL, flowRule.ipProtocol()) |
| 52 | .put(SRC_TP_PORT, flowRule.srcTpPort().toString()) |
| 53 | .put(DST_TP_PORT, flowRule.dstTpPort().toString()); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public StatsFlowRule decode(ObjectNode json, CodecContext context) { |
| 59 | if (json == null || !json.isObject()) { |
| 60 | return null; |
| 61 | } |
| 62 | try { |
| 63 | String srcIpPrefix = json.get(SRC_IP_PREFIX).asText(); |
| 64 | String dstIpPrefix = json.get(DST_IP_PREFIX).asText(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 65 | |
| 66 | DefaultStatsFlowRule.Builder flowRuleBuilder; |
| 67 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 68 | if (json.get(IP_PROTOCOL) == null) { |
| 69 | log.info("ipProtocol: null"); |
| 70 | flowRuleBuilder = DefaultStatsFlowRule.builder() |
| 71 | .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix)) |
| 72 | .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); |
| 73 | } else { |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 74 | byte ipProtocol = getProtocolTypeFromString(json.get(IP_PROTOCOL).asText()); |
| 75 | int srcTpPort = json.get(SRC_TP_PORT).asInt(); |
| 76 | int dstTpPort = json.get(DST_TP_PORT).asInt(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 77 | |
| 78 | flowRuleBuilder = DefaultStatsFlowRule.builder() |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 79 | .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix)) |
| 80 | .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix)) |
| 81 | .ipProtocol(ipProtocol) |
| 82 | .srcTpPort(TpPort.tpPort(srcTpPort)) |
| 83 | .dstTpPort(TpPort.tpPort(dstTpPort)); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 84 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 85 | return flowRuleBuilder.build(); |
| 86 | } catch (Exception ex) { |
| 87 | log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex)); |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | } |