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