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; |
| 29 | import org.slf4j.Logger; |
| 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; |
| 36 | import static org.onlab.util.Tools.nullIsIllegal; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * MeterRequest JSON codec. |
| 41 | */ |
| 42 | public 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 Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 77 | Meter.Unit meterUnit = null; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 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: |
Jayasree Ghosh | 02a417f | 2016-08-30 01:24:45 +0530 | [diff] [blame] | 87 | nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter."); |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 88 | } |
| 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 | } |