alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [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.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.meter.Band; |
| 27 | import org.onosproject.net.meter.DefaultBand; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 28 | import org.onosproject.net.meter.DefaultMeterRequest; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 29 | import org.onosproject.net.meter.Meter; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 30 | import org.onosproject.net.meter.MeterRequest; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 31 | import org.onosproject.net.meter.MeterScope; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 32 | import org.onosproject.net.meter.MeterService; |
| 33 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jordi Ortiz | 161445a | 2017-02-15 18:29:04 +0100 | [diff] [blame] | 35 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 36 | import java.util.HashSet; |
| 37 | import java.util.Set; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 40 | * Add a meter to a device. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Service |
Jordi Ortiz | 4c93e27 | 2017-01-30 13:25:51 +0100 | [diff] [blame] | 43 | @Command(scope = "onos", name = "meter-add", |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 44 | description = "Adds a meter to a device (currently for testing)") |
Jordi Ortiz | 4c93e27 | 2017-01-30 13:25:51 +0100 | [diff] [blame] | 45 | public class MeterAddCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 46 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 47 | private Meter.Unit unit; |
| 48 | private Set<Band> bands = new HashSet<>(); |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 49 | private MeterScope scope; |
| 50 | private Long index; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 51 | |
| 52 | @Option(name = "-bd", aliases = "--bandDrop", |
| 53 | description = "Assign band DROP to this meter", |
| 54 | required = false, multiValued = false) |
| 55 | private boolean hasBandDrop = false; |
| 56 | |
| 57 | @Option(name = "-br", aliases = "--bandRemark", |
| 58 | description = "Assign band REMARK to this meter", |
| 59 | required = false, multiValued = false) |
| 60 | private boolean hasBandRemark = false; |
| 61 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 62 | @Option(name = "-by", aliases = "--bandYel", |
| 63 | description = "Assign band MARK_YELLOW to this meter", |
| 64 | required = false, multiValued = false) |
| 65 | private boolean hasBandYel = false; |
| 66 | |
| 67 | @Option(name = "-bre", aliases = "--bandRed", |
| 68 | description = "Assign band MARK_RED to this meter", |
| 69 | required = false, multiValued = false) |
| 70 | private boolean hasBandRed = false; |
| 71 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 72 | @Option(name = "-up", aliases = "--unitPkts", |
| 73 | description = "Assign unit Packets per Second to this meter", |
| 74 | required = false, multiValued = false) |
| 75 | private boolean hasPkts = false; |
| 76 | |
| 77 | @Option(name = "-uk", aliases = "--unitKbps", |
| 78 | description = "Assign unit Kilobits per Second to this meter", |
| 79 | required = false, multiValued = false) |
| 80 | private boolean hasKbps = false; |
| 81 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 82 | @Option(name = "-ub", aliases = "--unitBytes", |
| 83 | description = "Assign unit Bytes per Second to this meter", |
| 84 | required = false, multiValued = false) |
| 85 | private boolean hasBytes = false; |
| 86 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 87 | @Option(name = "-ib", aliases = "--isBurst", |
| 88 | description = "Set meter applicable only to burst", |
| 89 | required = false, multiValued = false) |
| 90 | private boolean isBurst = false; |
| 91 | |
| 92 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 93 | required = false, multiValued = true) |
| 94 | private String[] bandwidthString = null; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 95 | |
| 96 | @Option(name = "-bs", aliases = "--burstSize", description = "Burst size", |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 97 | required = false, multiValued = true) |
| 98 | private String[] burstSizeString = null; |
| 99 | |
| 100 | @Option(name = "-sc", aliases = "--scope", description = "Scope", |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 101 | required = false, multiValued = false) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 102 | private String scopeString = null; |
| 103 | |
| 104 | @Option(name = "-id", aliases = "--index", description = "Index", |
| 105 | required = false, multiValued = false) |
| 106 | private String indexString = null; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 107 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 108 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 109 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 110 | @Completion(DeviceIdCompleter.class) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 111 | private String uri = null; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 112 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 113 | private final String appId = "org.onosproject.cli.meterCmd"; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 114 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 115 | private void checkOptions() { |
| 116 | // check units |
| 117 | if (hasPkts) { |
| 118 | unit = Meter.Unit.PKTS_PER_SEC; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 119 | } else if (hasKbps) { |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 120 | unit = Meter.Unit.KB_PER_SEC; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 121 | } else if (hasBytes) { |
| 122 | unit = Meter.Unit.BYTES_PER_SEC; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 123 | } |
| 124 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 125 | int numBands = 0; |
| 126 | if (hasBandDrop) { |
| 127 | numBands++; |
| 128 | } |
| 129 | if (hasBandRemark) { |
| 130 | numBands++; |
| 131 | } |
| 132 | if (hasBandYel) { |
| 133 | numBands++; |
| 134 | } |
| 135 | if (hasBandRed) { |
| 136 | numBands++; |
| 137 | } |
| 138 | |
| 139 | long[] rates = new long[numBands]; |
| 140 | long[] bursts = new long[numBands]; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 141 | // check rate (does not take into account if it is kbps or pkts) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 142 | if (bandwidthString != null && bandwidthString.length == numBands && |
| 143 | burstSizeString != null && burstSizeString.length == numBands) { |
| 144 | for (int i = 0; i < bandwidthString.length; i++) { |
| 145 | rates[i] = 500L; |
| 146 | bursts[i] = 0L; |
| 147 | if (!isNullOrEmpty(bandwidthString[i])) { |
| 148 | rates[i] = Long.parseLong(bandwidthString[i]); |
| 149 | } |
| 150 | if (!isNullOrEmpty(burstSizeString[i])) { |
| 151 | bursts[i] = Long.parseLong(burstSizeString[i]); |
| 152 | } |
| 153 | } |
| 154 | } else if (bandwidthString != null && bandwidthString.length < numBands && |
| 155 | burstSizeString != null && burstSizeString.length < numBands) { |
| 156 | for (int i = 0; i < numBands; i++) { |
| 157 | rates[i] = 500L; |
| 158 | bursts[i] = 0L; |
| 159 | if (i < bandwidthString.length && !isNullOrEmpty(bandwidthString[i])) { |
| 160 | rates[i] = Long.parseLong(bandwidthString[i]); |
| 161 | } |
| 162 | if (i < burstSizeString.length && !isNullOrEmpty(burstSizeString[i])) { |
| 163 | bursts[i] = Long.parseLong(burstSizeString[i]); |
| 164 | } |
| 165 | } |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Create bands |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 169 | int i = 0; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 170 | if (hasBandDrop) { |
| 171 | Band band = DefaultBand.builder() |
| 172 | .ofType(Band.Type.DROP) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 173 | .withRate(rates[i]) |
| 174 | .burstSize(bursts[i]) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 175 | .build(); |
| 176 | bands.add(band); |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 177 | i++; |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 178 | } |
| 179 | if (hasBandRemark) { |
| 180 | Band band = DefaultBand.builder() |
| 181 | .ofType(Band.Type.REMARK) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 182 | .withRate(rates[i]) |
| 183 | .burstSize(bursts[i]) |
| 184 | .build(); |
| 185 | bands.add(band); |
| 186 | i++; |
| 187 | } |
| 188 | if (hasBandYel) { |
| 189 | Band band = DefaultBand.builder() |
| 190 | .ofType(Band.Type.MARK_YELLOW) |
| 191 | .withRate(rates[i]) |
| 192 | .burstSize(bursts[i]) |
| 193 | .build(); |
| 194 | bands.add(band); |
| 195 | i++; |
| 196 | } |
| 197 | if (hasBandRed) { |
| 198 | Band band = DefaultBand.builder() |
| 199 | .ofType(Band.Type.MARK_RED) |
| 200 | .withRate(rates[i]) |
| 201 | .burstSize(bursts[i]) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 202 | .build(); |
| 203 | bands.add(band); |
| 204 | } |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 205 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 206 | // default band is drop |
| 207 | if (bands.size() == 0) { |
| 208 | Band band = DefaultBand.builder() |
| 209 | .ofType(Band.Type.DROP) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 210 | .withRate(500L) |
| 211 | .burstSize(0L) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 212 | .build(); |
| 213 | bands.add(band); |
| 214 | } |
| 215 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 216 | if (!isNullOrEmpty(scopeString)) { |
| 217 | scope = MeterScope.of(scopeString); |
| 218 | } |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 219 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 220 | if (!isNullOrEmpty(indexString) && scope != null) { |
| 221 | index = Long.parseLong(indexString); |
| 222 | } |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 223 | } |
| 224 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 225 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 226 | protected void doExecute() { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 227 | MeterService service = get(MeterService.class); |
| 228 | CoreService coreService = get(CoreService.class); |
| 229 | |
| 230 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 231 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 232 | checkOptions(); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 233 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 234 | MeterRequest.Builder builder = DefaultMeterRequest.builder() |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 235 | .forDevice(deviceId) |
| 236 | .fromApp(coreService.registerApplication(appId)) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 237 | .withUnit(unit) |
| 238 | .withBands(bands); |
| 239 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 240 | if (isBurst) { |
| 241 | builder = builder.burst(); |
| 242 | } |
| 243 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 244 | // Scope is by default global but we can still provide the index |
| 245 | // otherwise we can specify both scope and index or let the meter |
| 246 | // service allocate the meter for us. User defined index requires |
| 247 | // the user defined mode being active. |
| 248 | if (scope != null) { |
| 249 | builder = builder.withScope(scope); |
| 250 | } |
| 251 | |
| 252 | if (index != null) { |
| 253 | builder = builder.withIndex(index); |
| 254 | } |
| 255 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 256 | MeterRequest request = builder.add(); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 257 | |
Jordi Ortiz | 161445a | 2017-02-15 18:29:04 +0100 | [diff] [blame] | 258 | Meter m = service.submit(request); |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 259 | log.info("Requested meter with cellId {}: {}", m.meterCellId().toString(), m.toString()); |
| 260 | print("Requested meter with cellId %s: %s", m.meterCellId().toString(), m.toString()); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 261 | } |
| 262 | } |