blob: 3e71ad98f3034ab445d106b352234845163c8be2 [file] [log] [blame]
alshabib58fe6dc2015-08-19 17:16:13 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
alshabib58fe6dc2015-08-19 17:16:13 -070023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.meter.Band;
27import org.onosproject.net.meter.DefaultBand;
alshabibe1248b62015-08-20 17:21:55 -070028import org.onosproject.net.meter.DefaultMeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070029import org.onosproject.net.meter.Meter;
alshabibe1248b62015-08-20 17:21:55 -070030import org.onosproject.net.meter.MeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070031import org.onosproject.net.meter.MeterService;
32
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010033import static com.google.common.base.Strings.isNullOrEmpty;
Jordi Ortiz161445a2017-02-15 18:29:04 +010034
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010035import java.util.HashSet;
36import java.util.Set;
alshabib58fe6dc2015-08-19 17:16:13 -070037
38/**
sangyun-han483731c2016-06-06 12:03:16 +090039 * Add a meter to a device.
alshabib58fe6dc2015-08-19 17:16:13 -070040 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Jordi Ortiz4c93e272017-01-30 13:25:51 +010042@Command(scope = "onos", name = "meter-add",
alshabib58fe6dc2015-08-19 17:16:13 -070043 description = "Adds a meter to a device (currently for testing)")
Jordi Ortiz4c93e272017-01-30 13:25:51 +010044public class MeterAddCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070045
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010046 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
alshabib58fe6dc2015-08-19 17:16:13 -070085 @Argument(index = 0, name = "uri", description = "Device ID",
86 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070087 @Completion(DeviceIdCompleter.class)
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010088 private String uri = null;
alshabib58fe6dc2015-08-19 17:16:13 -070089
alshabibe1248b62015-08-20 17:21:55 -070090 private final String appId = "org.onosproject.cli.meterCmd";
alshabib58fe6dc2015-08-19 17:16:13 -070091
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010092 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
alshabib58fe6dc2015-08-19 17:16:13 -0700145 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146 protected void doExecute() {
alshabib58fe6dc2015-08-19 17:16:13 -0700147 MeterService service = get(MeterService.class);
148 CoreService coreService = get(CoreService.class);
149
150 DeviceId deviceId = DeviceId.deviceId(uri);
151
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100152 checkOptions();
alshabib58fe6dc2015-08-19 17:16:13 -0700153
154
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100155 MeterRequest.Builder builder = DefaultMeterRequest.builder()
alshabib58fe6dc2015-08-19 17:16:13 -0700156 .forDevice(deviceId)
157 .fromApp(coreService.registerApplication(appId))
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100158 .withUnit(unit)
159 .withBands(bands);
160
161
162 if (isBurst) {
163 builder = builder.burst();
164 }
165
166 MeterRequest request = builder.add();
alshabib58fe6dc2015-08-19 17:16:13 -0700167
Jordi Ortiz161445a2017-02-15 18:29:04 +0100168 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());
alshabib58fe6dc2015-08-19 17:16:13 -0700171 }
172}