blob: 26b66778179af1680f94a30638e26f70eb04f5d2 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jian Li5c411232015-12-16 15:29:16 -08003 *
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.codec.impl;
17
Jian Li5c411232015-12-16 15:29:16 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Jian Li5c411232015-12-16 15:29:16 -080022import org.onosproject.net.meter.Band;
Jian Li5c411232015-12-16 15:29:16 -080023import org.onosproject.net.meter.Meter;
Jian Li5c411232015-12-16 15:29:16 -080024
Jian Li5c411232015-12-16 15:29:16 -080025import static com.google.common.base.Preconditions.checkNotNull;
Jian Li5c411232015-12-16 15:29:16 -080026
27
28/**
29 * Meter JSON codec.
30 */
31public final class MeterCodec extends JsonCodec<Meter> {
Jian Li5c411232015-12-16 15:29:16 -080032
33 // JSON field names
34 private static final String ID = "id";
35 private static final String STATE = "state";
36 private static final String LIFE = "life";
37 private static final String PACKETS = "packets";
38 private static final String BYTES = "bytes";
39 private static final String REFERENCE_COUNT = "referenceCount";
40 private static final String APP_ID = "appId";
41 private static final String BURST = "burst";
42 private static final String DEVICE_ID = "deviceId";
43 private static final String UNIT = "unit";
44 private static final String BANDS = "bands";
45 public static final String REST_APP_ID = "org.onosproject.rest";
Jian Li5c411232015-12-16 15:29:16 -080046
47 @Override
48 public ObjectNode encode(Meter meter, CodecContext context) {
49 checkNotNull(meter, "Meter cannot be null");
50 ObjectNode result = context.mapper().createObjectNode()
pierventrec0914ec2021-08-27 15:25:02 +020051 .put(ID, meter.meterCellId().toString())
Jian Li5c411232015-12-16 15:29:16 -080052 .put(LIFE, meter.life())
53 .put(PACKETS, meter.packetsSeen())
54 .put(BYTES, meter.bytesSeen())
55 .put(REFERENCE_COUNT, meter.referenceCount())
56 .put(UNIT, meter.unit().toString())
57 .put(BURST, meter.isBurst())
58 .put(DEVICE_ID, meter.deviceId().toString());
59
60 if (meter.appId() != null) {
Jayasree Ghoshe7a240c2016-09-10 14:45:15 +053061 result.put(APP_ID, meter.appId().name());
Jian Li5c411232015-12-16 15:29:16 -080062 }
63
64 if (meter.state() != null) {
65 result.put(STATE, meter.state().toString());
66 }
67
68 ArrayNode bands = context.mapper().createArrayNode();
69 meter.bands().forEach(band -> {
70 ObjectNode bandJson = context.codec(Band.class).encode(band, context);
71 bands.add(bandJson);
72 });
73 result.set(BANDS, bands);
74 return result;
75 }
Jian Li5c411232015-12-16 15:29:16 -080076}