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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.net.meter.Band; |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 22 | import org.onosproject.net.meter.Band.Builder; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 23 | import org.onosproject.net.meter.DefaultBand; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * Meter band JSON codec. |
| 32 | */ |
| 33 | public final class MeterBandCodec extends JsonCodec<Band> { |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
| 36 | // JSON field names |
| 37 | private static final String TYPE = "type"; |
| 38 | private static final String RATE = "rate"; |
| 39 | private static final String BURST_SIZE = "burstSize"; |
| 40 | private static final String PREC = "prec"; |
| 41 | private static final String PACKETS = "packets"; |
| 42 | private static final String BYTES = "bytes"; |
| 43 | private static final String MISSING_MEMBER_MESSAGE = " member is required in Band"; |
| 44 | |
| 45 | @Override |
| 46 | public ObjectNode encode(Band band, CodecContext context) { |
| 47 | checkNotNull(band, "Band cannot be null"); |
| 48 | |
| 49 | ObjectNode result = context.mapper().createObjectNode() |
| 50 | .put(TYPE, band.type().toString()) |
| 51 | .put(RATE, band.rate()) |
| 52 | .put(PACKETS, band.packets()) |
| 53 | .put(BYTES, band.bytes()) |
| 54 | .put(BURST_SIZE, band.burst()); |
| 55 | |
| 56 | if (band.dropPrecedence() != null) { |
| 57 | result.put(PREC, band.dropPrecedence()); |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Band decode(ObjectNode json, CodecContext context) { |
| 65 | if (json == null || !json.isObject()) { |
| 66 | return null; |
| 67 | } |
| 68 | |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 69 | Builder builder = DefaultBand.builder(); |
| 70 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 71 | // parse rate |
| 72 | long rate = nullIsIllegal(json.get(RATE), RATE + MISSING_MEMBER_MESSAGE).asLong(); |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 73 | builder.withRate(rate); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 74 | |
| 75 | // parse burst size |
| 76 | long burstSize = nullIsIllegal(json.get(BURST_SIZE), BURST_SIZE + MISSING_MEMBER_MESSAGE).asLong(); |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 77 | builder.burstSize(burstSize); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 78 | |
| 79 | // parse precedence |
| 80 | Short precedence = null; |
| 81 | |
| 82 | // parse band type |
| 83 | String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 84 | Band.Type type = null; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 85 | switch (typeStr) { |
| 86 | case "DROP": |
| 87 | type = Band.Type.DROP; |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 88 | builder.ofType(type); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 89 | break; |
| 90 | case "REMARK": |
| 91 | type = Band.Type.REMARK; |
| 92 | precedence = (short) nullIsIllegal(json.get(PREC), PREC + MISSING_MEMBER_MESSAGE).asInt(); |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 93 | builder.ofType(type); |
| 94 | builder.dropPrecedence(precedence); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 95 | break; |
| 96 | default: |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 97 | nullIsIllegal(type, "The requested type " + typeStr + " is not defined for band."); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 100 | Band band = builder.build(); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 101 | return band; |
| 102 | } |
| 103 | } |