Sangsik Yoon | f0b3ad8 | 2016-08-19 18:47:59 +0900 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sangsik Yoon | f0b3ad8 | 2016-08-19 18:47:59 +0900 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.incubator.net.dpi; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | /** |
| 29 | * Implementation of encoder for DpiStatistics codec. |
| 30 | */ |
| 31 | public 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 | } |