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