blob: a892b1744dd5906ed04bdb864c6f46f1d8baaeec [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;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21import org.apache.karaf.shell.api.action.Option;
alshabib58fe6dc2015-08-19 17:16:13 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.meter.Band;
26import org.onosproject.net.meter.DefaultBand;
alshabibe1248b62015-08-20 17:21:55 -070027import org.onosproject.net.meter.DefaultMeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070028import org.onosproject.net.meter.Meter;
alshabibe1248b62015-08-20 17:21:55 -070029import org.onosproject.net.meter.MeterRequest;
alshabib58fe6dc2015-08-19 17:16:13 -070030import org.onosproject.net.meter.MeterService;
31
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010032import static com.google.common.base.Strings.isNullOrEmpty;
Jordi Ortiz161445a2017-02-15 18:29:04 +010033
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010034import java.util.HashSet;
35import java.util.Set;
alshabib58fe6dc2015-08-19 17:16:13 -070036
37/**
sangyun-han483731c2016-06-06 12:03:16 +090038 * Add a meter to a device.
alshabib58fe6dc2015-08-19 17:16:13 -070039 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Jordi Ortiz4c93e272017-01-30 13:25:51 +010041@Command(scope = "onos", name = "meter-add",
alshabib58fe6dc2015-08-19 17:16:13 -070042 description = "Adds a meter to a device (currently for testing)")
Jordi Ortiz4c93e272017-01-30 13:25:51 +010043public class MeterAddCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070044
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010045 private Meter.Unit unit;
46 private Set<Band> bands = new HashSet<>();
47 private Long rate;
48 private Long burstSize;
49
50
51 @Option(name = "-bd", aliases = "--bandDrop",
52 description = "Assign band DROP to this meter",
53 required = false, multiValued = false)
54 private boolean hasBandDrop = false;
55
56 @Option(name = "-br", aliases = "--bandRemark",
57 description = "Assign band REMARK to this meter",
58 required = false, multiValued = false)
59 private boolean hasBandRemark = false;
60
61 @Option(name = "-up", aliases = "--unitPkts",
62 description = "Assign unit Packets per Second to this meter",
63 required = false, multiValued = false)
64 private boolean hasPkts = false;
65
66 @Option(name = "-uk", aliases = "--unitKbps",
67 description = "Assign unit Kilobits per Second to this meter",
68 required = false, multiValued = false)
69 private boolean hasKbps = false;
70
71 @Option(name = "-ib", aliases = "--isBurst",
72 description = "Set meter applicable only to burst",
73 required = false, multiValued = false)
74 private boolean isBurst = false;
75
76 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
77 required = false, multiValued = false)
78 private String bandwidthString = null;
79
80 @Option(name = "-bs", aliases = "--burstSize", description = "Burst size",
81 required = false, multiValued = false)
82 private String burstSizeString = null;
83
alshabib58fe6dc2015-08-19 17:16:13 -070084 @Argument(index = 0, name = "uri", description = "Device ID",
85 required = true, multiValued = false)
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010086 private String uri = null;
alshabib58fe6dc2015-08-19 17:16:13 -070087
alshabibe1248b62015-08-20 17:21:55 -070088 private final String appId = "org.onosproject.cli.meterCmd";
alshabib58fe6dc2015-08-19 17:16:13 -070089
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010090 private void checkOptions() {
91 // check units
92 if (hasPkts) {
93 unit = Meter.Unit.PKTS_PER_SEC;
94 } else {
95 unit = Meter.Unit.KB_PER_SEC;
96 }
97
98 // check rate (does not take into account if it is kbps or pkts)
99 if (!isNullOrEmpty(bandwidthString)) {
100 rate = Long.parseLong(bandwidthString);
101 } else {
102 rate = 500L;
103 }
104
105 // burst size
106 if (!isNullOrEmpty(burstSizeString)) {
107 burstSize = Long.parseLong(burstSizeString);
108 } else {
109 burstSize = 0L;
110 }
111
112 // Create bands
113 if (hasBandDrop) {
114 Band band = DefaultBand.builder()
115 .ofType(Band.Type.DROP)
116 .withRate(rate)
117 .burstSize(burstSize)
118 .build();
119 bands.add(band);
120 }
121 if (hasBandRemark) {
122 Band band = DefaultBand.builder()
123 .ofType(Band.Type.REMARK)
124 .withRate(rate)
125 .burstSize(burstSize)
126 .build();
127 bands.add(band);
128 }
129 // default band is drop
130 if (bands.size() == 0) {
131 Band band = DefaultBand.builder()
132 .ofType(Band.Type.DROP)
133 .withRate(rate)
134 .burstSize(burstSize)
135 .build();
136 bands.add(band);
137 }
138
139
140
141 }
142
alshabib58fe6dc2015-08-19 17:16:13 -0700143 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700144 protected void doExecute() {
alshabib58fe6dc2015-08-19 17:16:13 -0700145 MeterService service = get(MeterService.class);
146 CoreService coreService = get(CoreService.class);
147
148 DeviceId deviceId = DeviceId.deviceId(uri);
149
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100150 checkOptions();
alshabib58fe6dc2015-08-19 17:16:13 -0700151
152
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100153 MeterRequest.Builder builder = DefaultMeterRequest.builder()
alshabib58fe6dc2015-08-19 17:16:13 -0700154 .forDevice(deviceId)
155 .fromApp(coreService.registerApplication(appId))
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100156 .withUnit(unit)
157 .withBands(bands);
158
159
160 if (isBurst) {
161 builder = builder.burst();
162 }
163
164 MeterRequest request = builder.add();
alshabib58fe6dc2015-08-19 17:16:13 -0700165
Jordi Ortiz161445a2017-02-15 18:29:04 +0100166 Meter m = service.submit(request);
167 log.info("Requested meter with id {}: {}", m.id().toString(), m.toString());
168 print("Requested meter with id %s: %s", m.id().toString(), m.toString());
alshabib58fe6dc2015-08-19 17:16:13 -0700169 }
170}