blob: 3cb68a08b1b60af4b7e605322cc56e0db9c82f22 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Jian Li5c411232015-12-16 15:29:16 -080024
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.onlab.util.Tools.nullIsIllegal;
Jian Li5c411232015-12-16 15:29:16 -080027
28/**
29 * Meter band JSON codec.
30 */
31public final class MeterBandCodec extends JsonCodec<Band> {
Jian Li5c411232015-12-16 15:29:16 -080032
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 Ghosh02a417f2016-08-30 01:24:45 +053066 Builder builder = DefaultBand.builder();
67
Jian Li5c411232015-12-16 15:29:16 -080068 // parse rate
69 long rate = nullIsIllegal(json.get(RATE), RATE + MISSING_MEMBER_MESSAGE).asLong();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053070 builder.withRate(rate);
Jian Li5c411232015-12-16 15:29:16 -080071
72 // parse burst size
73 long burstSize = nullIsIllegal(json.get(BURST_SIZE), BURST_SIZE + MISSING_MEMBER_MESSAGE).asLong();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053074 builder.burstSize(burstSize);
Jian Li5c411232015-12-16 15:29:16 -080075
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 Ghosh02a417f2016-08-30 01:24:45 +053081 Band.Type type = null;
Jian Li5c411232015-12-16 15:29:16 -080082 switch (typeStr) {
83 case "DROP":
84 type = Band.Type.DROP;
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053085 builder.ofType(type);
Jian Li5c411232015-12-16 15:29:16 -080086 break;
87 case "REMARK":
88 type = Band.Type.REMARK;
89 precedence = (short) nullIsIllegal(json.get(PREC), PREC + MISSING_MEMBER_MESSAGE).asInt();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053090 builder.ofType(type);
91 builder.dropPrecedence(precedence);
Jian Li5c411232015-12-16 15:29:16 -080092 break;
pierventrec0914ec2021-08-27 15:25:02 +020093 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 Li5c411232015-12-16 15:29:16 -0800105 default:
Jayasree Ghosh02a417f2016-08-30 01:24:45 +0530106 nullIsIllegal(type, "The requested type " + typeStr + " is not defined for band.");
Jian Li5c411232015-12-16 15:29:16 -0800107 }
108
Jayasree Ghosh02a417f2016-08-30 01:24:45 +0530109 Band band = builder.build();
Jian Li5c411232015-12-16 15:29:16 -0800110 return band;
111 }
112}