blob: b64fd23b910282e4a084601660b4a2b49d09857f [file] [log] [blame]
Jian Li95678962016-01-26 17:17:52 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
29import org.slf4j.Logger;
30
31import java.util.ArrayList;
32import java.util.List;
33import java.util.stream.IntStream;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import static org.onlab.util.Tools.nullIsIllegal;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * MeterRequest JSON codec.
41 */
42public final class MeterRequestCodec extends JsonCodec<MeterRequest> {
43 private final Logger log = getLogger(getClass());
44
45 // JSON field names
46 private static final String DEVICE_ID = "deviceId";
47 private static final String UNIT = "unit";
48 private static final String BANDS = "bands";
49 public static final String REST_APP_ID = "org.onosproject.rest";
50 private static final String MISSING_MEMBER_MESSAGE = " member is required in MeterRequest";
51
52 @Override
53 public MeterRequest decode(ObjectNode json, CodecContext context) {
54 if (json == null || !json.isObject()) {
55 return null;
56 }
57
58 final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
59 CoreService coreService = context.getService(CoreService.class);
60
61 // parse device id
62 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
63 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
64
65 // application id
66 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
67
68 // parse burst
69 boolean burst = false;
70 JsonNode burstJson = json.get("burst");
71 if (burstJson != null) {
72 burst = burstJson.asBoolean();
73 }
74
75 // parse unit type
76 String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053077 Meter.Unit meterUnit = null;
Jian Li95678962016-01-26 17:17:52 -080078
79 switch (unit) {
80 case "KB_PER_SEC":
81 meterUnit = Meter.Unit.KB_PER_SEC;
82 break;
83 case "PKTS_PER_SEC":
84 meterUnit = Meter.Unit.PKTS_PER_SEC;
85 break;
86 default:
Jayasree Ghosh02a417f2016-08-30 01:24:45 +053087 nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
Jian Li95678962016-01-26 17:17:52 -080088 }
89
90 // parse meter bands
91 List<Band> bandList = new ArrayList<>();
92 JsonNode bandsJson = json.get(BANDS);
93 checkNotNull(bandsJson);
94 if (bandsJson != null) {
95 IntStream.range(0, bandsJson.size()).forEach(i -> {
96 ObjectNode bandJson = get(bandsJson, i);
97 bandList.add(meterBandCodec.decode(bandJson, context));
98 });
99 }
100
101 MeterRequest meterRequest;
102 if (burst) {
103 meterRequest = DefaultMeterRequest.builder()
104 .fromApp(appId)
105 .forDevice(deviceId)
106 .withUnit(meterUnit)
107 .withBands(bandList)
108 .burst().add();
109 } else {
110 meterRequest = DefaultMeterRequest.builder()
111 .fromApp(appId)
112 .forDevice(deviceId)
113 .withUnit(meterUnit)
114 .withBands(bandList).add();
115 }
116
117 return meterRequest;
118 }
119}