blob: be51b154537239b985975854b89976557e39263d [file] [log] [blame]
Jian Li4a3fffa2018-06-10 23:12:40 +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.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.TpPort;
boyoung2a8549d22018-11-23 20:42:37 +090024import org.onlab.packet.VlanId;
Jian Li4a3fffa2018-06-10 23:12:40 +090025import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.net.DeviceId;
28import org.onosproject.openstacktelemetry.api.FlowInfo;
29import org.onosproject.openstacktelemetry.api.StatsInfo;
30import org.onosproject.openstacktelemetry.impl.DefaultFlowInfo;
Jian Li4a3fffa2018-06-10 23:12:40 +090031
32import static com.google.common.base.Preconditions.checkNotNull;
boyoung2a8549d22018-11-23 20:42:37 +090033import static org.onlab.packet.VlanId.NO_VID;
Jian Li4a3fffa2018-06-10 23:12:40 +090034
35/**
36 * Openstack telemetry codec used for serializing and de-serializing JSON string.
37 */
38public final class FlowInfoJsonCodec extends JsonCodec<FlowInfo> {
39
Jian Li4a3fffa2018-06-10 23:12:40 +090040 private static final String FLOW_TYPE = "flowType";
41 private static final String DEVICE_ID = "deviceId";
42 private static final String INPUT_INTERFACE_ID = "inputInterfaceId";
43 private static final String OUTPUT_INTERFACE_ID = "outputInterfaceId";
44
45 private static final String VLAN_ID = "vlanId";
46 private static final String VXLAN_ID = "vxlanId";
47 private static final String SRC_IP = "srcIp";
48 private static final String SRC_IP_PREFIX_LEN = "srcIpPrefixLength";
49 private static final String DST_IP = "dstIp";
50 private static final String DST_IP_PREFIX_LEN = "dstIpPrefixLength";
51 private static final String SRC_PORT = "srcPort";
52 private static final String DST_PORT = "dstPort";
53 private static final String PROTOCOL = "protocol";
54 private static final String SRC_MAC = "srcMac";
55 private static final String DST_MAC = "dstMac";
56 private static final String STATS_INFO = "statsInfo";
57
58 @Override
59 public ObjectNode encode(FlowInfo info, CodecContext context) {
60 checkNotNull(info, "FlowInfo cannot be null");
61
62 ObjectNode result = context.mapper().createObjectNode()
63 .put(FLOW_TYPE, info.flowType())
64 .put(DEVICE_ID, info.deviceId().toString())
65 .put(INPUT_INTERFACE_ID, info.inputInterfaceId())
66 .put(OUTPUT_INTERFACE_ID, info.outputInterfaceId())
67 .put(SRC_IP, info.srcIp().address().toString())
68 .put(SRC_IP_PREFIX_LEN, info.srcIp().prefixLength())
69 .put(DST_IP, info.dstIp().address().toString())
70 .put(DST_IP_PREFIX_LEN, info.dstIp().prefixLength())
71 .put(SRC_PORT, info.srcPort().toString())
72 .put(DST_PORT, info.dstPort().toString())
73 .put(PROTOCOL, info.protocol())
74 .put(SRC_MAC, info.srcMac().toString())
75 .put(DST_MAC, info.dstMac().toString());
76
Jian Li4a3fffa2018-06-10 23:12:40 +090077 if (info.vlanId() != null) {
78 result.put(VLAN_ID, info.vlanId().toString());
79 } else {
80 result.put(VXLAN_ID, info.vxlanId());
81 }
82
83 ObjectNode statsInfoJson =
84 context.codec(StatsInfo.class).encode(info.statsInfo(), context);
85
86 result.put(STATS_INFO, statsInfoJson);
87
88 return result;
89 }
90
91 @Override
92 public FlowInfo decode(ObjectNode json, CodecContext context) {
93 if (json == null || !json.isObject()) {
94 return null;
95 }
96
97 String flowType = json.get(FLOW_TYPE).asText();
98 String deviceId = json.get(DEVICE_ID).asText();
99 int inputInterfaceId = json.get(INPUT_INTERFACE_ID).asInt();
100 int outputInterfaceId = json.get(OUTPUT_INTERFACE_ID).asInt();
101 String srcIp = json.get(SRC_IP).asText();
102 int srcIpPrefixLength = json.get(SRC_IP_PREFIX_LEN).asInt();
103 String dstIp = json.get(DST_IP).asText();
104 int dstIpPrefixLength = json.get(DST_IP_PREFIX_LEN).asInt();
105 int srcPort = json.get(SRC_PORT).asInt();
106 int dstPort = json.get(DST_PORT).asInt();
107 String protocol = json.get(PROTOCOL).asText();
108 String srcMac = json.get(SRC_MAC).asText();
109 String dstMac = json.get(DST_MAC).asText();
110
boyoung2a8549d22018-11-23 20:42:37 +0900111 VlanId vlanId;
112 short vxlanId = 0;
113 try {
114 if (json.get(VLAN_ID).isNull()) {
115 vlanId = VlanId.vlanId(NO_VID);
116 if (!(json.get(VXLAN_ID).isNull())) {
117 vxlanId = (short) json.get(VXLAN_ID).asInt();
118 }
119 } else {
120 vlanId = VlanId.vlanId((short) json.get(VLAN_ID).asInt());
121 }
122 } catch (NullPointerException ex) {
123 vlanId = VlanId.vlanId();
124 }
125
Jian Li4a3fffa2018-06-10 23:12:40 +0900126 JsonNode statsInfoJson = json.get(STATS_INFO);
127
128 JsonCodec<StatsInfo> statsInfoCodec = context.codec(StatsInfo.class);
129 StatsInfo statsInfo = statsInfoCodec.decode((ObjectNode) statsInfoJson.deepCopy(), context);
130
131 return new DefaultFlowInfo.DefaultBuilder()
132 .withFlowType(Byte.valueOf(flowType))
133 .withDeviceId(DeviceId.deviceId(deviceId))
134 .withInputInterfaceId(inputInterfaceId)
135 .withOutputInterfaceId(outputInterfaceId)
136 .withSrcIp(IpPrefix.valueOf(IpAddress.valueOf(srcIp), srcIpPrefixLength))
137 .withDstIp(IpPrefix.valueOf(IpAddress.valueOf(dstIp), dstIpPrefixLength))
138 .withSrcPort(TpPort.tpPort(srcPort))
139 .withDstPort(TpPort.tpPort(dstPort))
140 .withProtocol(Byte.valueOf(protocol))
141 .withSrcMac(MacAddress.valueOf(srcMac))
142 .withDstMac(MacAddress.valueOf(dstMac))
boyoung2a8549d22018-11-23 20:42:37 +0900143 .withVlanId(vlanId)
144 .withVxlanId(vxlanId)
Jian Li4a3fffa2018-06-10 23:12:40 +0900145 .withStatsInfo(statsInfo)
146 .build();
147 }
148}