blob: 3d2b13fc50af2a172c8c7a6c67a4da572d201a9d [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 */
Jian Li7fe7eaf2018-12-31 17:00:33 +090016package org.onosproject.openstacktelemetry.codec.rest;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.apache.commons.lang3.exception.ExceptionUtils;
Jian Li7fe7eaf2018-12-31 17:00:33 +090020import org.onlab.packet.IPv4;
21import org.onlab.packet.IpPrefix;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090022import org.onlab.packet.TpPort;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
Jian Li7fe7eaf2018-12-31 17:00:33 +090025import org.onosproject.openstacktelemetry.api.DefaultStatsFlowRule;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090026import org.onosproject.openstacktelemetry.api.StatsFlowRule;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090027import org.slf4j.Logger;
28
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090029import static com.google.common.base.Preconditions.checkNotNull;
30import static org.slf4j.LoggerFactory.getLogger;
31
Jian Li69600e02018-12-24 13:21:18 +090032/**
33 * JSON codec for StatsFlowRule.
34 */
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090035public class StatsFlowRuleJsonCodec extends JsonCodec<StatsFlowRule> {
36
37 private final Logger log = getLogger(getClass());
38
Jian Li7fe7eaf2018-12-31 17:00:33 +090039 private static final String PROTOCOL_NAME_TCP = "tcp";
40 private static final String PROTOCOL_NAME_UDP = "udp";
41 private static final String PROTOCOL_NAME_ANY = "any";
42 private static final int ARBITRARY_PROTOCOL = 0x0;
43
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090044 public static final String SRC_IP_PREFIX = "srcIpPrefix";
45 public static final String DST_IP_PREFIX = "dstIpPrefix";
46 public static final String IP_PROTOCOL = "ipProtocol";
47 public static final String SRC_TP_PORT = "srcTpPort";
48 public static final String DST_TP_PORT = "dstTpPort";
49
50 public ObjectNode encode(StatsFlowRule flowRule, CodecContext context) {
51 checkNotNull(flowRule, "FlowInfo cannot be null");
52 ObjectNode result = context.mapper().createObjectNode()
53 .put(SRC_IP_PREFIX, flowRule.srcIpPrefix().toString())
54 .put(DST_IP_PREFIX, flowRule.dstIpPrefix().toString())
55 .put(IP_PROTOCOL, flowRule.ipProtocol())
56 .put(SRC_TP_PORT, flowRule.srcTpPort().toString())
57 .put(DST_TP_PORT, flowRule.dstTpPort().toString());
58 return result;
59 }
60
61 @Override
62 public StatsFlowRule decode(ObjectNode json, CodecContext context) {
63 if (json == null || !json.isObject()) {
64 return null;
65 }
66 try {
67 String srcIpPrefix = json.get(SRC_IP_PREFIX).asText();
68 String dstIpPrefix = json.get(DST_IP_PREFIX).asText();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090069
70 DefaultStatsFlowRule.Builder flowRuleBuilder;
71
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090072 if (json.get(IP_PROTOCOL) == null) {
73 log.info("ipProtocol: null");
74 flowRuleBuilder = DefaultStatsFlowRule.builder()
75 .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix))
76 .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
77 } else {
boyoung2a8549d22018-11-23 20:42:37 +090078 byte ipProtocol = getProtocolTypeFromString(json.get(IP_PROTOCOL).asText());
79 int srcTpPort = json.get(SRC_TP_PORT).asInt();
80 int dstTpPort = json.get(DST_TP_PORT).asInt();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090081
82 flowRuleBuilder = DefaultStatsFlowRule.builder()
boyoung2a8549d22018-11-23 20:42:37 +090083 .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix))
84 .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix))
85 .ipProtocol(ipProtocol)
86 .srcTpPort(TpPort.tpPort(srcTpPort))
87 .dstTpPort(TpPort.tpPort(dstTpPort));
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090088 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090089 return flowRuleBuilder.build();
90 } catch (Exception ex) {
91 log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex));
92 }
93 return null;
94 }
Jian Li7fe7eaf2018-12-31 17:00:33 +090095
96 /**
97 * Obtains transport protocol type from the given string.
98 *
99 * @param str transport protocol name
100 * @return transport protocol type
101 */
102 private byte getProtocolTypeFromString(String str) {
103 switch (str.toLowerCase()) {
104 case PROTOCOL_NAME_TCP:
105 return IPv4.PROTOCOL_TCP;
106 case PROTOCOL_NAME_UDP:
107 return IPv4.PROTOCOL_UDP;
108 default:
109 return ARBITRARY_PROTOCOL;
110 }
111 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900112}