Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | import org.slf4j.Logger; |
| 25 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Meter JSON codec. |
| 32 | */ |
| 33 | public 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 Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 49 | |
| 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 Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 79 | } |