blob: d46e2579c0e1d1a6cda98de260f93ae0a4fd8ab8 [file] [log] [blame]
Jian Li95678962016-01-26 17:17:52 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li95678962016-01-26 17:17:52 -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.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.meter.Band;
26import org.onosproject.net.meter.DefaultMeterRequest;
27import org.onosproject.net.meter.Meter;
28import org.onosproject.net.meter.MeterRequest;
pierventrec0914ec2021-08-27 15:25:02 +020029import org.onosproject.net.meter.MeterScope;
Jian Li95678962016-01-26 17:17:52 -080030
31import java.util.ArrayList;
32import java.util.List;
33import java.util.stream.IntStream;
34
35import static com.google.common.base.Preconditions.checkNotNull;
pierventrec0914ec2021-08-27 15:25:02 +020036import static com.google.common.base.Strings.isNullOrEmpty;
Jian Li95678962016-01-26 17:17:52 -080037import static org.onlab.util.Tools.nullIsIllegal;
Jian Li95678962016-01-26 17:17:52 -080038
39/**
40 * MeterRequest JSON codec.
41 */
42public final class MeterRequestCodec extends JsonCodec<MeterRequest> {
Jian Li95678962016-01-26 17:17:52 -080043
44 // JSON field names
45 private static final String DEVICE_ID = "deviceId";
46 private static final String UNIT = "unit";
47 private static final String BANDS = "bands";
pierventrec0914ec2021-08-27 15:25:02 +020048 private static final String SCOPE = "scope";
49 private static final String INDEX = "index";
50 private static final String REST_APP_ID = "org.onosproject.rest";
Jian Li95678962016-01-26 17:17:52 -080051 private static final String MISSING_MEMBER_MESSAGE = " member is required in MeterRequest";
52
pierventrec0914ec2021-08-27 15:25:02 +020053 private ApplicationId applicationId;
54
Jian Li95678962016-01-26 17:17:52 -080055 @Override
56 public MeterRequest decode(ObjectNode json, CodecContext context) {
57 if (json == null || !json.isObject()) {
58 return null;
59 }
60
61 final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
pierventrec0914ec2021-08-27 15:25:02 +020062
Jian Li95678962016-01-26 17:17:52 -080063
64 // parse device id
65 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
66 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
67
68 // application id
pierventrec0914ec2021-08-27 15:25:02 +020069 if (applicationId == null) {
70 CoreService coreService = context.getService(CoreService.class);
71 applicationId = coreService.registerApplication(REST_APP_ID);
72 }
Jian Li95678962016-01-26 17:17:52 -080073
74 // parse burst
75 boolean burst = false;
76 JsonNode burstJson = json.get("burst");
77 if (burstJson != null) {
78 burst = burstJson.asBoolean();
79 }
80
81 // parse unit type
82 String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053083 Meter.Unit meterUnit = null;
Jian Li95678962016-01-26 17:17:52 -080084
85 switch (unit) {
86 case "KB_PER_SEC":
87 meterUnit = Meter.Unit.KB_PER_SEC;
88 break;
89 case "PKTS_PER_SEC":
90 meterUnit = Meter.Unit.PKTS_PER_SEC;
91 break;
pierventrec0914ec2021-08-27 15:25:02 +020092 case "BYTES_PER_SEC":
93 meterUnit = Meter.Unit.BYTES_PER_SEC;
94 break;
Jian Li95678962016-01-26 17:17:52 -080095 default:
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053096 nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
Jian Li95678962016-01-26 17:17:52 -080097 }
98
99 // parse meter bands
100 List<Band> bandList = new ArrayList<>();
101 JsonNode bandsJson = json.get(BANDS);
102 checkNotNull(bandsJson);
103 if (bandsJson != null) {
104 IntStream.range(0, bandsJson.size()).forEach(i -> {
105 ObjectNode bandJson = get(bandsJson, i);
106 bandList.add(meterBandCodec.decode(bandJson, context));
107 });
108 }
109
pierventrec0914ec2021-08-27 15:25:02 +0200110 // parse scope and index
111 JsonNode scopeJson = json.get(SCOPE);
112 MeterScope scope = null;
113 if (scopeJson != null && !isNullOrEmpty(scopeJson.asText())) {
114 scope = MeterScope.of(scopeJson.asText());
Jian Li95678962016-01-26 17:17:52 -0800115 }
116
pierventrec0914ec2021-08-27 15:25:02 +0200117 JsonNode indexJson = json.get(INDEX);
118 Long index = null;
119 if (indexJson != null && !isNullOrEmpty(indexJson.asText()) && scope != null) {
120 index = indexJson.asLong();
121 }
122
123 // build the final request
124 MeterRequest.Builder meterRequest = DefaultMeterRequest.builder();
125 if (scope != null) {
126 meterRequest.withScope(scope);
127 }
128
129 if (index != null) {
130 meterRequest.withIndex(index);
131 }
132
133 meterRequest.fromApp(applicationId)
134 .forDevice(deviceId)
135 .withUnit(meterUnit)
136 .withBands(bandList);
137
138 if (burst) {
139 meterRequest.burst();
140 }
141
142 return meterRequest.add();
Jian Li95678962016-01-26 17:17:52 -0800143 }
pierventrec0914ec2021-08-27 15:25:02 +0200144
Jian Li95678962016-01-26 17:17:52 -0800145}