blob: a26063c9950ecb9329b7ccb4237083c60d41e7f0 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li5c411232015-12-16 15:29:16 -08003 *
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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.net.meter.Band;
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053022import org.onosproject.net.meter.Band.Builder;
Jian Li5c411232015-12-16 15:29:16 -080023import org.onosproject.net.meter.DefaultBand;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onlab.util.Tools.nullIsIllegal;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Meter band JSON codec.
32 */
33public 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 Ghosh02a417f2016-08-30 01:24:45 +053069 Builder builder = DefaultBand.builder();
70
Jian Li5c411232015-12-16 15:29:16 -080071 // parse rate
72 long rate = nullIsIllegal(json.get(RATE), RATE + MISSING_MEMBER_MESSAGE).asLong();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053073 builder.withRate(rate);
Jian Li5c411232015-12-16 15:29:16 -080074
75 // parse burst size
76 long burstSize = nullIsIllegal(json.get(BURST_SIZE), BURST_SIZE + MISSING_MEMBER_MESSAGE).asLong();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053077 builder.burstSize(burstSize);
Jian Li5c411232015-12-16 15:29:16 -080078
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 Ghosh02a417f2016-08-30 01:24:45 +053084 Band.Type type = null;
Jian Li5c411232015-12-16 15:29:16 -080085 switch (typeStr) {
86 case "DROP":
87 type = Band.Type.DROP;
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053088 builder.ofType(type);
Jian Li5c411232015-12-16 15:29:16 -080089 break;
90 case "REMARK":
91 type = Band.Type.REMARK;
92 precedence = (short) nullIsIllegal(json.get(PREC), PREC + MISSING_MEMBER_MESSAGE).asInt();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053093 builder.ofType(type);
94 builder.dropPrecedence(precedence);
Jian Li5c411232015-12-16 15:29:16 -080095 break;
96 default:
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053097 nullIsIllegal(type, "The requested type " + typeStr + " is not defined for band.");
Jian Li5c411232015-12-16 15:29:16 -080098 }
99
Jayasree Ghosh02a417f2016-08-30 01:24:45 +0530100 Band band = builder.build();
Jian Li5c411232015-12-16 15:29:16 -0800101 return band;
102 }
103}