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.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of encoder for ProtocolStatInfo codec. |
| 29 | */ |
| 30 | public 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 | } |