blob: d32ff6c7d96637696b1af7705e6b11c5e9bfc17a [file] [log] [blame]
Sangsik Yoonf0b3ad82016-08-19 18:47:59 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
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
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 FlowStatInfo codec.
29 */
30public final class FlowStatInfoCodec extends JsonCodec<FlowStatInfo> {
31
32 private final Logger log = getLogger(getClass());
33
34 @Override
35 public ObjectNode encode(FlowStatInfo fsi, CodecContext context) {
36 checkNotNull(fsi, "FlowStatInfo cannot be null");
37
38 return context.mapper().createObjectNode()
39 .put("protocol", fsi.protocol())
40 .put("hostAName", fsi.hostAName())
41 .put("hostAPort", fsi.hostAPort())
42 .put("hostBName", fsi.hostBName())
43 .put("hostBPort", fsi.hostBPort())
44 .put("detectedProtocol", fsi.detectedProtocol())
45 .put("detectedProtocolName", fsi.detectedProtocolName())
46 .put("packets", fsi.packets())
47 .put("bytes", fsi.bytes())
48 .put("hostServerName", fsi.hostServerName());
49 }
50
51 @Override
52 public FlowStatInfo decode(ObjectNode json, CodecContext context) {
53 if (json == null || !json.isObject()) {
54 return null;
55 }
56
57 log.debug("protocol={}, full json={} ", json.get("protocol"), json);
58 final String protocol = json.get("protocol").asText();
59 final String hostAName = json.get("hostAName").asText();
60 final int hostAPort = json.get("hostAPort").asInt();
61 final String hostBName = json.get("hostBName").asText();
62 final int hostBPort = json.get("hostBPort").asInt();
63 final int detectedProtocol = json.get("detectedProtocol").asInt();
64 final String detectedProtocolName = json.get("detectedProtocolName").asText();
65 final long packets = json.get("packets").asLong();
66 final long bytes = json.get("bytes").asLong();
67 final String hostServerName = json.get("hostServerName").asText();
68
69 return new FlowStatInfo(protocol,
70 hostAName, hostAPort, hostBName, hostBPort,
71 detectedProtocol, detectedProtocolName,
72 packets, bytes, hostServerName);
73 }
74}