Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.meter.Band; |
| 26 | import org.onosproject.net.meter.DefaultMeterRequest; |
| 27 | import org.onosproject.net.meter.Meter; |
| 28 | import org.onosproject.net.meter.MeterRequest; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 29 | import org.onosproject.net.meter.MeterScope; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | import java.util.stream.IntStream; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 37 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * MeterRequest JSON codec. |
| 41 | */ |
| 42 | public final class MeterRequestCodec extends JsonCodec<MeterRequest> { |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 43 | |
| 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"; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 48 | 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 Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 51 | private static final String MISSING_MEMBER_MESSAGE = " member is required in MeterRequest"; |
| 52 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 53 | private ApplicationId applicationId; |
| 54 | |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 55 | @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); |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 62 | |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 63 | |
| 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 |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 69 | if (applicationId == null) { |
| 70 | CoreService coreService = context.getService(CoreService.class); |
| 71 | applicationId = coreService.registerApplication(REST_APP_ID); |
| 72 | } |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 73 | |
| 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 Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 83 | Meter.Unit meterUnit = null; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 84 | |
| 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; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 92 | case "BYTES_PER_SEC": |
| 93 | meterUnit = Meter.Unit.BYTES_PER_SEC; |
| 94 | break; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 95 | default: |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 96 | nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter."); |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 97 | } |
| 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 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 110 | // 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 Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 115 | } |
| 116 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 117 | 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 Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 143 | } |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 144 | |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 145 | } |