blob: d141915379a5c72573cbace0dd6b832dec008c57 [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.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.slf4j.Logger;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.slf4j.LoggerFactory.getLogger;
27
28/**
29 * Implementation of encoder for DpiStatistics codec.
30 */
31public final class DpiStatisticsCodec extends JsonCodec<DpiStatistics> {
32
33 private static final String RECEIVED_TIME = "receivedTime";
34 private static final String DPI_STATISTICS = "dpiStatistics";
35
36 private final Logger log = getLogger(getClass());
37
38 @Override
39 public ObjectNode encode(DpiStatistics ds, CodecContext context) {
40 checkNotNull(ds, "DpiStatistics cannot be null");
41
42 final ObjectNode result = context.mapper().createObjectNode();
43
44 result.put(RECEIVED_TIME, ds.receivedTime());
45
46 final JsonCodec<DpiStatInfo> dpiStatInfoCodec =
47 context.codec(DpiStatInfo.class);
48
49 final ObjectNode jsonDpiStatInfo = dpiStatInfoCodec.encode(ds.dpiStatInfo(), context);
50 result.set(DPI_STATISTICS, jsonDpiStatInfo);
51
52 return result;
53 }
54
55 @Override
56 public DpiStatistics decode(ObjectNode json, CodecContext context) {
57 if (json == null || !json.isObject()) {
58 return null;
59 }
60
61 log.debug("receivedTime={}, full json={} ", json.get("receivedTime"), json);
62 JsonNode receivedTimeJson = json.get(RECEIVED_TIME);
63 String receivedTime = receivedTimeJson == null ? "" : receivedTimeJson.asText();
64
65 final JsonCodec<DpiStatInfo> dpiStatInfoCodec =
66 context.codec(DpiStatInfo.class);
67
68 DpiStatInfo dpiStatInfo =
69 dpiStatInfoCodec.decode(get(json, DPI_STATISTICS), context);
70
71 return new DpiStatistics(receivedTime, dpiStatInfo);
72 }
73}