blob: 5c695ad60c9da4ef0d60896deca7ef7360846b59 [file] [log] [blame]
Sangsik Yoonf0b3ad82016-08-19 18:47:59 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sangsik Yoonf0b3ad82016-08-19 18:47:59 +09003 *
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
17package org.onosproject.incubator.net.dpi;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.slf4j.Logger;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.slf4j.LoggerFactory.getLogger;
26
27/**
28 * Implementation of encoder for TrafficStatInfo codec.
29 */
30public final class TrafficStatInfoCodec extends JsonCodec<TrafficStatInfo> {
31
32 private final Logger log = getLogger(getClass());
33
34 @Override
35 public ObjectNode encode(TrafficStatInfo tsi, CodecContext context) {
36 checkNotNull(tsi, "TrafficStatInfo cannot be null");
37
38 return context.mapper().createObjectNode()
39 .put("ethernetBytes", tsi.ethernetBytes())
40 .put("discardedBytes", tsi.discardedBytes())
41 .put("ipPackets", tsi.ipPackets())
42 .put("totalPackets", tsi.totalPackets())
43 .put("ipBytes", tsi.ipBytes())
44 .put("avgPktSize", tsi.avgPktSize())
45 .put("uniqueFlows", tsi.uniqueFlows())
46 .put("tcpPackets", tsi.tcpPackets())
47 .put("udpPackets", tsi.udpPackets())
48 .put("dpiThroughputPps", tsi.dpiThroughputPps())
49 .put("dpiThroughputBps", tsi.dpiThroughputBps())
50 .put("trafficThroughputPps", tsi.trafficThroughputPps())
51 .put("trafficThroughputBps", tsi.trafficThroughputBps())
52 .put("trafficDurationSec", tsi.trafficDurationSec())
53 .put("guessedFlowProtos", tsi.guessedFlowProtos());
54 }
55
56 @Override
57 public TrafficStatInfo decode(ObjectNode json, CodecContext context) {
58 if (json == null || !json.isObject()) {
59 return null;
60 }
61
62 log.debug("ethernetBytes={}, full json={} ", json.get("ethernetBytes"), json);
63 final Long ethernetBytes = json.get("ethernetBytes").asLong();
64 final Long discardedBytes = json.get("discardedBytes").asLong();
65 final Long ipPackets = json.get("ipPackets").asLong();
66 final Long totalPackets = json.get("totalPackets").asLong();
67 final Long ipBytes = json.get("ipBytes").asLong();
68 final int avgPktSize = json.get("avgPktSize").asInt();
69 final int uniqueFlows = json.get("uniqueFlows").asInt();
70 final Long tcpPackets = json.get("tcpPackets").asLong();
71 final Long udpPackets = json.get("udpPackets").asLong();
72 final double dpiThroughputPps = json.get("dpiThroughputPps").asDouble();
73 final double dpiThroughputBps = json.get("dpiThroughputBps").asDouble();
74 final double trafficThroughputPps = json.get("trafficThroughputPps").asDouble();
75 final double trafficThroughputBps = json.get("trafficThroughputBps").asDouble();
76 final double trafficDurationSec = json.get("trafficDurationSec").asDouble();
77 final int guessedFlowProtos = json.get("guessedFlowProtos").asInt();
78
79 return new TrafficStatInfo(ethernetBytes,
80 discardedBytes,
81 ipPackets, totalPackets,
82 ipBytes, avgPktSize,
83 uniqueFlows,
84 tcpPackets, udpPackets,
85 dpiThroughputPps, dpiThroughputBps,
86 trafficThroughputPps, trafficThroughputBps,
87 trafficDurationSec,
88 guessedFlowProtos);
89 }
90}