blob: 5b7df817c0cb446232e0b2dfc32887504eea2cd5 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 -080024import org.slf4j.Logger;
25
Jian Li5c411232015-12-16 15:29:16 -080026import static com.google.common.base.Preconditions.checkNotNull;
Jian Li5c411232015-12-16 15:29:16 -080027import static org.slf4j.LoggerFactory.getLogger;
28
29
30/**
31 * Meter JSON codec.
32 */
33public final class MeterCodec extends JsonCodec<Meter> {
34 private final Logger log = getLogger(getClass());
35
36 // JSON field names
37 private static final String ID = "id";
38 private static final String STATE = "state";
39 private static final String LIFE = "life";
40 private static final String PACKETS = "packets";
41 private static final String BYTES = "bytes";
42 private static final String REFERENCE_COUNT = "referenceCount";
43 private static final String APP_ID = "appId";
44 private static final String BURST = "burst";
45 private static final String DEVICE_ID = "deviceId";
46 private static final String UNIT = "unit";
47 private static final String BANDS = "bands";
48 public static final String REST_APP_ID = "org.onosproject.rest";
Jian Li5c411232015-12-16 15:29:16 -080049
50 @Override
51 public ObjectNode encode(Meter meter, CodecContext context) {
52 checkNotNull(meter, "Meter cannot be null");
53 ObjectNode result = context.mapper().createObjectNode()
54 .put(ID, meter.id().toString())
55 .put(LIFE, meter.life())
56 .put(PACKETS, meter.packetsSeen())
57 .put(BYTES, meter.bytesSeen())
58 .put(REFERENCE_COUNT, meter.referenceCount())
59 .put(UNIT, meter.unit().toString())
60 .put(BURST, meter.isBurst())
61 .put(DEVICE_ID, meter.deviceId().toString());
62
63 if (meter.appId() != null) {
64 result.put(APP_ID, meter.appId().toString());
65 }
66
67 if (meter.state() != null) {
68 result.put(STATE, meter.state().toString());
69 }
70
71 ArrayNode bands = context.mapper().createArrayNode();
72 meter.bands().forEach(band -> {
73 ObjectNode bandJson = context.codec(Band.class).encode(band, context);
74 bands.add(bandJson);
75 });
76 result.set(BANDS, bands);
77 return result;
78 }
Jian Li5c411232015-12-16 15:29:16 -080079}