blob: 9469f6cd561091c6445c31201b3a82b21db0d2bd [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.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 ProtocolStatInfo codec.
29 */
30public final class ProtocolStatInfoCodec extends JsonCodec<ProtocolStatInfo> {
31
32 private final Logger log = getLogger(getClass());
33
34 @Override
35 public ObjectNode encode(ProtocolStatInfo psi, CodecContext context) {
36 checkNotNull(psi, "ProtocolStatInfo cannot be null");
37
38 return context.mapper().createObjectNode()
39 .put("name", psi.name())
40 .put("breed", psi.breed())
41 .put("packets", psi.packets())
42 .put("bytes", psi.bytes())
43 .put("flows", psi.flows());
44 }
45
46 @Override
47 public ProtocolStatInfo decode(ObjectNode json, CodecContext context) {
48 if (json == null || !json.isObject()) {
49 return null;
50 }
51
52 log.debug("name={}, full json={} ", json.get("name"), json);
53 final String name = json.get("name").asText();
54 final String breed = json.get("breed").asText();
55 final long packets = json.get("packets").asLong();
56 final long bytes = json.get("bytes").asLong();
57 final int flows = json.get("flows").asInt();
58
59 return new ProtocolStatInfo(name, breed, packets, bytes, flows);
60 }
61}