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