blob: a92e96e16613bfb637806753035573dc6eac744b [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();
77 Meter.Unit meterUnit;
78
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:
87 log.warn("The requested unit {} is not defined for meter.", unit);
88 return null;
89 }
90
91 // parse meter bands
92 List<Band> bandList = new ArrayList<>();
93 JsonNode bandsJson = json.get(BANDS);
94 checkNotNull(bandsJson);
95 if (bandsJson != null) {
96 IntStream.range(0, bandsJson.size()).forEach(i -> {
97 ObjectNode bandJson = get(bandsJson, i);
98 bandList.add(meterBandCodec.decode(bandJson, context));
99 });
100 }
101
102 MeterRequest meterRequest;
103 if (burst) {
104 meterRequest = DefaultMeterRequest.builder()
105 .fromApp(appId)
106 .forDevice(deviceId)
107 .withUnit(meterUnit)
108 .withBands(bandList)
109 .burst().add();
110 } else {
111 meterRequest = DefaultMeterRequest.builder()
112 .fromApp(appId)
113 .forDevice(deviceId)
114 .withUnit(meterUnit)
115 .withBands(bandList).add();
116 }
117
118 return meterRequest;
119 }
120}