Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 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.codec.impl; |
| 17 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 22 | import org.onosproject.net.meter.Band; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 23 | import org.onosproject.net.meter.Meter; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 24 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | /** |
| 29 | * Meter JSON codec. |
| 30 | */ |
| 31 | public final class MeterCodec extends JsonCodec<Meter> { |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 32 | |
| 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 Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | public ObjectNode encode(Meter meter, CodecContext context) { |
| 49 | checkNotNull(meter, "Meter cannot be null"); |
| 50 | ObjectNode result = context.mapper().createObjectNode() |
pierventre | 45dbcc7 | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 51 | .put(ID, meter.meterCellId().toString()) |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 52 | .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 Ghosh | e7a240c | 2016-09-10 14:45:15 +0530 | [diff] [blame] | 61 | result.put(APP_ID, meter.appId().name()); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 62 | } |
| 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 Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 76 | } |