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