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