blob: 6285316544e654d9d4b22cedf0dadbaf0b4d48ab [file] [log] [blame]
alshabib58fe6dc2015-08-19 17:16:13 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
alshabib58fe6dc2015-08-19 17:16:13 -07003 *
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010020import org.apache.karaf.shell.commands.Option;
alshabib58fe6dc2015-08-19 17:16:13 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.meter.Band;
25import org.onosproject.net.meter.DefaultBand;
alshabibe1248b62015-08-20 17:21:55 -070026import org.onosproject.net.meter.DefaultMeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070027import org.onosproject.net.meter.Meter;
alshabibe1248b62015-08-20 17:21:55 -070028import org.onosproject.net.meter.MeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070029import org.onosproject.net.meter.MeterService;
30
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010031import static com.google.common.base.Strings.isNullOrEmpty;
32import java.util.HashSet;
33import java.util.Set;
alshabib58fe6dc2015-08-19 17:16:13 -070034
35/**
sangyun-han483731c2016-06-06 12:03:16 +090036 * Add a meter to a device.
alshabib58fe6dc2015-08-19 17:16:13 -070037 */
38@Command(scope = "onos", name = "add-meter",
39 description = "Adds a meter to a device (currently for testing)")
sangyun-han483731c2016-06-06 12:03:16 +090040public class AddMeterCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070041
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010042 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
alshabib58fe6dc2015-08-19 17:16:13 -070081 @Argument(index = 0, name = "uri", description = "Device ID",
82 required = true, multiValued = false)
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010083 private String uri = null;
alshabib58fe6dc2015-08-19 17:16:13 -070084
alshabibe1248b62015-08-20 17:21:55 -070085 private final String appId = "org.onosproject.cli.meterCmd";
alshabib58fe6dc2015-08-19 17:16:13 -070086
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010087 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
alshabib58fe6dc2015-08-19 17:16:13 -0700140 @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 Ortiz3bbfd992017-01-21 19:29:52 +0100147 checkOptions();
alshabib58fe6dc2015-08-19 17:16:13 -0700148
149
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100150 MeterRequest.Builder builder = DefaultMeterRequest.builder()
alshabib58fe6dc2015-08-19 17:16:13 -0700151 .forDevice(deviceId)
152 .fromApp(coreService.registerApplication(appId))
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100153 .withUnit(unit)
154 .withBands(bands);
155
156
157 if (isBurst) {
158 builder = builder.burst();
159 }
160
161 MeterRequest request = builder.add();
alshabib58fe6dc2015-08-19 17:16:13 -0700162
alshabibe1248b62015-08-20 17:21:55 -0700163 service.submit(request);
alshabib58fe6dc2015-08-19 17:16:13 -0700164
165 }
166}