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