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; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.meter.MeterService; |
| 32 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 33 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jordi Ortiz | 161445a | 2017-02-15 18:29:04 +0100 | [diff] [blame] | 34 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 35 | import java.util.HashSet; |
| 36 | import java.util.Set; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 39 | * Add a meter to a device. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 40 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | @Service |
Jordi Ortiz | 4c93e27 | 2017-01-30 13:25:51 +0100 | [diff] [blame] | 42 | @Command(scope = "onos", name = "meter-add", |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 43 | description = "Adds a meter to a device (currently for testing)") |
Jordi Ortiz | 4c93e27 | 2017-01-30 13:25:51 +0100 | [diff] [blame] | 44 | public class MeterAddCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 45 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 46 | private Meter.Unit unit; |
| 47 | private Set<Band> bands = new HashSet<>(); |
| 48 | private Long rate; |
| 49 | private Long burstSize; |
| 50 | |
| 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 | |
| 62 | @Option(name = "-up", aliases = "--unitPkts", |
| 63 | description = "Assign unit Packets per Second to this meter", |
| 64 | required = false, multiValued = false) |
| 65 | private boolean hasPkts = false; |
| 66 | |
| 67 | @Option(name = "-uk", aliases = "--unitKbps", |
| 68 | description = "Assign unit Kilobits per Second to this meter", |
| 69 | required = false, multiValued = false) |
| 70 | private boolean hasKbps = false; |
| 71 | |
| 72 | @Option(name = "-ib", aliases = "--isBurst", |
| 73 | description = "Set meter applicable only to burst", |
| 74 | required = false, multiValued = false) |
| 75 | private boolean isBurst = false; |
| 76 | |
| 77 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
| 78 | required = false, multiValued = false) |
| 79 | private String bandwidthString = null; |
| 80 | |
| 81 | @Option(name = "-bs", aliases = "--burstSize", description = "Burst size", |
| 82 | required = false, multiValued = false) |
| 83 | private String burstSizeString = null; |
| 84 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 85 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 86 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 87 | @Completion(DeviceIdCompleter.class) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 88 | private String uri = null; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 89 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 90 | private final String appId = "org.onosproject.cli.meterCmd"; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 91 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 92 | private void checkOptions() { |
| 93 | // check units |
| 94 | if (hasPkts) { |
| 95 | unit = Meter.Unit.PKTS_PER_SEC; |
| 96 | } else { |
| 97 | unit = Meter.Unit.KB_PER_SEC; |
| 98 | } |
| 99 | |
| 100 | // check rate (does not take into account if it is kbps or pkts) |
| 101 | if (!isNullOrEmpty(bandwidthString)) { |
| 102 | rate = Long.parseLong(bandwidthString); |
| 103 | } else { |
| 104 | rate = 500L; |
| 105 | } |
| 106 | |
| 107 | // burst size |
| 108 | if (!isNullOrEmpty(burstSizeString)) { |
| 109 | burstSize = Long.parseLong(burstSizeString); |
| 110 | } else { |
| 111 | burstSize = 0L; |
| 112 | } |
| 113 | |
| 114 | // Create bands |
| 115 | if (hasBandDrop) { |
| 116 | Band band = DefaultBand.builder() |
| 117 | .ofType(Band.Type.DROP) |
| 118 | .withRate(rate) |
| 119 | .burstSize(burstSize) |
| 120 | .build(); |
| 121 | bands.add(band); |
| 122 | } |
| 123 | if (hasBandRemark) { |
| 124 | Band band = DefaultBand.builder() |
| 125 | .ofType(Band.Type.REMARK) |
| 126 | .withRate(rate) |
| 127 | .burstSize(burstSize) |
| 128 | .build(); |
| 129 | bands.add(band); |
| 130 | } |
| 131 | // default band is drop |
| 132 | if (bands.size() == 0) { |
| 133 | Band band = DefaultBand.builder() |
| 134 | .ofType(Band.Type.DROP) |
| 135 | .withRate(rate) |
| 136 | .burstSize(burstSize) |
| 137 | .build(); |
| 138 | bands.add(band); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | } |
| 144 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 145 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 146 | protected void doExecute() { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 147 | MeterService service = get(MeterService.class); |
| 148 | CoreService coreService = get(CoreService.class); |
| 149 | |
| 150 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 151 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 152 | checkOptions(); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 153 | |
| 154 | |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 155 | MeterRequest.Builder builder = DefaultMeterRequest.builder() |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 156 | .forDevice(deviceId) |
| 157 | .fromApp(coreService.registerApplication(appId)) |
Jordi Ortiz | 3bbfd99 | 2017-01-21 19:29:52 +0100 | [diff] [blame] | 158 | .withUnit(unit) |
| 159 | .withBands(bands); |
| 160 | |
| 161 | |
| 162 | if (isBurst) { |
| 163 | builder = builder.burst(); |
| 164 | } |
| 165 | |
| 166 | MeterRequest request = builder.add(); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 167 | |
Jordi Ortiz | 161445a | 2017-02-15 18:29:04 +0100 | [diff] [blame] | 168 | Meter m = service.submit(request); |
| 169 | log.info("Requested meter with id {}: {}", m.id().toString(), m.toString()); |
| 170 | print("Requested meter with id %s: %s", m.id().toString(), m.toString()); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 171 | } |
| 172 | } |