blob: 34e246a3a08277257a1f12babf66810999045b2e [file] [log] [blame]
Sangsik Yoonf0b3ad82016-08-19 18:47:59 +09001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-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.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.slf4j.Logger;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.stream.IntStream;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Implementation of encoder for DpiStatInfo codec.
35 */
36public final class DpiStatInfoCodec extends JsonCodec<DpiStatInfo> {
37
38 private static final String TRAFFIC_STATISTICS = "trafficStatistics";
39 private static final String DETECTED_PROTOS = "detectedProtos";
40 private static final String KNOWN_FLOWS = "knownFlows";
41 private static final String UNKNOWN_FLOWS = "unknownFlows";
42
43 private final Logger log = getLogger(getClass());
44
45 @Override
46 public ObjectNode encode(DpiStatInfo dsi, CodecContext context) {
47 checkNotNull(dsi, "DpiStatInfo cannot be null");
48
49 final ObjectNode result = context.mapper().createObjectNode();
50
51 final JsonCodec<TrafficStatInfo> trafficStatInfoCodec =
52 context.codec(TrafficStatInfo.class);
53
54
55 final TrafficStatInfo tsi = dsi.trafficStatistics();
56 if (tsi != null) {
57 final ObjectNode jsonTrafficStatistics = trafficStatInfoCodec.encode(tsi, context);
58 result.set(TRAFFIC_STATISTICS, jsonTrafficStatistics);
59 }
60
61
62 final List<ProtocolStatInfo> psi = dsi.detectedProtos();
63 if (psi != null) {
64 final ArrayNode jsonDetectedProtos = result.putArray(DETECTED_PROTOS);
65 final JsonCodec<ProtocolStatInfo> protocolStatInfoCodec =
66 context.codec(ProtocolStatInfo.class);
67
68 for (final ProtocolStatInfo protocolStatInfo : psi) {
69 jsonDetectedProtos.add(protocolStatInfoCodec.encode(protocolStatInfo, context));
70 }
71 }
72
73 List<FlowStatInfo> fsi = dsi.knownFlows();
74 if (fsi != null) {
75 final ArrayNode jsonKnownFlows = result.putArray(KNOWN_FLOWS);
76 final JsonCodec<FlowStatInfo> flowStatInfoCodec =
77 context.codec(FlowStatInfo.class);
78
79 for (final FlowStatInfo flowStatInfo : fsi) {
80 jsonKnownFlows.add(flowStatInfoCodec.encode(flowStatInfo, context));
81 }
82 }
83
84 fsi = dsi.unknownFlows();
85 if (fsi != null) {
86 final ArrayNode jsonUnknownFlows = result.putArray(UNKNOWN_FLOWS);
87 final JsonCodec<FlowStatInfo> flowStatInfoCodec =
88 context.codec(FlowStatInfo.class);
89
90 for (final FlowStatInfo flowStatInfo : fsi) {
91 jsonUnknownFlows.add(flowStatInfoCodec.encode(flowStatInfo, context));
92 }
93 }
94
95 return result;
96 }
97
98 @Override
99 public DpiStatInfo decode(ObjectNode json, CodecContext context) {
100 if (json == null || !json.isObject()) {
101 return null;
102 }
103
104 log.debug("trafficStatistics={}, full json={} ", json.get("trafficStatistics"), json);
105 TrafficStatInfo trafficStatInfo = null;
106 ObjectNode tsJson = get(json, TRAFFIC_STATISTICS);
107 if (tsJson != null) {
108 JsonCodec<TrafficStatInfo> trafficStatInfoJsonCodec =
109 context.codec(TrafficStatInfo.class);
110 trafficStatInfo = trafficStatInfoJsonCodec.decode(tsJson, context);
111 }
112
113 final JsonCodec<ProtocolStatInfo> protocolStatInfoCodec =
114 context.codec(ProtocolStatInfo.class);
115
116 List<ProtocolStatInfo> detectedProtos = new ArrayList<>();
117 JsonNode dpJson = json.get(DETECTED_PROTOS);
118 if (dpJson != null) {
119 IntStream.range(0, dpJson.size())
120 .forEach(i -> detectedProtos.add(
121 protocolStatInfoCodec.decode(get(dpJson, i),
122 context)));
123 }
124
125 final JsonCodec<FlowStatInfo> flowStatInfoCodec =
126 context.codec(FlowStatInfo.class);
127
128 List<FlowStatInfo> knownFlows = new ArrayList<>();
129 JsonNode kfJson = json.get(KNOWN_FLOWS);
130 if (kfJson != null) {
131 IntStream.range(0, kfJson.size())
132 .forEach(i -> knownFlows.add(
133 flowStatInfoCodec.decode(get(kfJson, i),
134 context)));
135 }
136
137 List<FlowStatInfo> unknownFlows = new ArrayList<>();
138 JsonNode ufJson = json.get(UNKNOWN_FLOWS);
139 if (ufJson != null) {
140 IntStream.range(0, ufJson.size())
141 .forEach(i -> unknownFlows.add(
142 flowStatInfoCodec.decode(get(ufJson, i),
143 context)));
144 }
145
146 return new DpiStatInfo(trafficStatInfo,
147 detectedProtos,
148 knownFlows,
149 unknownFlows);
150 }
151}