blob: 2e84b799ab73ff72c54cd79470e97f5ac891601b [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
33public 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();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090062
63 DefaultStatsFlowRule.Builder flowRuleBuilder;
64
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090065 if (json.get(IP_PROTOCOL) == null) {
66 log.info("ipProtocol: null");
67 flowRuleBuilder = DefaultStatsFlowRule.builder()
68 .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix))
69 .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
70 } else {
boyoung2a8549d22018-11-23 20:42:37 +090071 byte ipProtocol = getProtocolTypeFromString(json.get(IP_PROTOCOL).asText());
72 int srcTpPort = json.get(SRC_TP_PORT).asInt();
73 int dstTpPort = json.get(DST_TP_PORT).asInt();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090074
75 flowRuleBuilder = DefaultStatsFlowRule.builder()
boyoung2a8549d22018-11-23 20:42:37 +090076 .srcIpPrefix(IpPrefix.valueOf(srcIpPrefix))
77 .dstIpPrefix(IpPrefix.valueOf(dstIpPrefix))
78 .ipProtocol(ipProtocol)
79 .srcTpPort(TpPort.tpPort(srcTpPort))
80 .dstTpPort(TpPort.tpPort(dstTpPort));
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090081 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090082 return flowRuleBuilder.build();
83 } catch (Exception ex) {
84 log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex));
85 }
86 return null;
87 }
88}