blob: 384987b6a96bd686a3fd33abd7e2a247edc12e5b [file] [log] [blame]
Boyoung Jeong9e8faec2018-06-17 21:19:23 +09001/*
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 */
16package org.onosproject.openstacktelemetry.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.apache.commons.lang3.exception.ExceptionUtils;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090020import org.onlab.packet.TpPort;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.openstacktelemetry.api.StatsFlowRule;
24import org.onosproject.openstacktelemetry.impl.DefaultStatsFlowRule;
25import org.slf4j.Logger;
26
27import org.onlab.packet.IpPrefix;
28
29import static com.google.common.base.Preconditions.checkNotNull;
boyoung2a8549d22018-11-23 20:42:37 +090030import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getProtocolTypeFromString;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090031import static org.slf4j.LoggerFactory.getLogger;
32
Jian Li69600e02018-12-24 13:21:18 +090033/**
34 * JSON codec for StatsFlowRule.
35 */
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090036public 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 Jeong9e8faec2018-06-17 21:19:23 +090065
66 DefaultStatsFlowRule.Builder flowRuleBuilder;
67
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090068 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 {
boyoung2a8549d22018-11-23 20:42:37 +090074 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 Jeong9e8faec2018-06-17 21:19:23 +090077
78 flowRuleBuilder = DefaultStatsFlowRule.builder()
boyoung2a8549d22018-11-23 20:42:37 +090079 .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix))
80 .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix))
81 .ipProtocol(ipProtocol)
82 .srcTpPort(TpPort.tpPort(srcTpPort))
83 .dstTpPort(TpPort.tpPort(dstTpPort));
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090084 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090085 return flowRuleBuilder.build();
86 } catch (Exception ex) {
87 log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex));
88 }
89 return null;
90 }
91}