blob: 905b2ff77f6574ac68577e5d52e34f1feb70c5be [file] [log] [blame]
Jian Li4a3fffa2018-06-10 23:12:40 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacktelemetry.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.openstacktelemetry.api.StatsInfo;
22import org.onosproject.openstacktelemetry.impl.DefaultStatsInfo;
Jian Li4a3fffa2018-06-10 23:12:40 +090023
24import static com.google.common.base.Preconditions.checkNotNull;
Jian Li4a3fffa2018-06-10 23:12:40 +090025
26/**
27 * Openstack telemetry codec used for serializing and de-serializing JSON string.
28 */
29public final class StatsInfoJsonCodec extends JsonCodec<StatsInfo> {
30
Jian Li4a3fffa2018-06-10 23:12:40 +090031 private static final String STARTUP_TIME = "startupTime";
32 private static final String FST_PKT_ARR_TIME = "fstPktArrTime";
33 private static final String LST_PKT_OFFSET = "lstPktOffset";
34 private static final String PREV_ACC_BYTES = "prevAccBytes";
35 private static final String PREV_ACC_PKTS = "prevAccPkts";
36 private static final String CURR_ACC_BYTES = "currAccBytes";
37 private static final String CURR_ACC_PKTS = "currAccPkts";
38 private static final String ERROR_PKTS = "errorPkts";
39 private static final String DROP_PKTS = "dropPkts";
40
41 @Override
42 public ObjectNode encode(StatsInfo info, CodecContext context) {
43 checkNotNull(info, "StatsInfo cannot be null");
44
45 return context.mapper().createObjectNode()
46 .put(STARTUP_TIME, info.startupTime())
47 .put(FST_PKT_ARR_TIME, info.fstPktArrTime())
48 .put(LST_PKT_OFFSET, info.lstPktOffset())
49 .put(PREV_ACC_BYTES, info.prevAccBytes())
50 .put(PREV_ACC_PKTS, info.prevAccPkts())
51 .put(CURR_ACC_BYTES, info.prevAccBytes())
52 .put(CURR_ACC_PKTS, info.prevAccPkts())
53 .put(ERROR_PKTS, info.errorPkts())
54 .put(DROP_PKTS, info.dropPkts());
55 }
56
57 @Override
58 public StatsInfo decode(ObjectNode json, CodecContext context) {
59 if (json == null || !json.isObject()) {
60 return null;
61 }
62
63 return new DefaultStatsInfo.DefaultBuilder()
64 .withStartupTime(json.get(STARTUP_TIME).asLong())
65 .withFstPktArrTime(json.get(FST_PKT_ARR_TIME).asLong())
66 .withLstPktOffset(json.get(LST_PKT_OFFSET).asInt())
67 .withPrevAccBytes(json.get(PREV_ACC_BYTES).asLong())
68 .withPrevAccPkts(json.get(PREV_ACC_PKTS).asInt())
69 .withCurrAccBytes(json.get(CURR_ACC_BYTES).asLong())
70 .withCurrAccPkts(json.get(CURR_ACC_PKTS).asInt())
71 .withErrorPkts((short) json.get(ERROR_PKTS).asInt())
72 .withDropPkts((short) json.get(DROP_PKTS).asInt())
73 .build();
74 }
75}