blob: 0b23f882cdd7b208827e6369b14d13d2279cdce8 [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
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;
Jordi Ortiz161445a2017-02-15 18:29:04 +010032
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010033import java.util.HashSet;
34import java.util.Set;
alshabib58fe6dc2015-08-19 17:16:13 -070035
36/**
sangyun-han483731c2016-06-06 12:03:16 +090037 * Add a meter to a device.
alshabib58fe6dc2015-08-19 17:16:13 -070038 */
Jordi Ortiz4c93e272017-01-30 13:25:51 +010039@Command(scope = "onos", name = "meter-add",
alshabib58fe6dc2015-08-19 17:16:13 -070040 description = "Adds a meter to a device (currently for testing)")
Jordi Ortiz4c93e272017-01-30 13:25:51 +010041public class MeterAddCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070042
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010043 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
alshabib58fe6dc2015-08-19 17:16:13 -070082 @Argument(index = 0, name = "uri", description = "Device ID",
83 required = true, multiValued = false)
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010084 private String uri = null;
alshabib58fe6dc2015-08-19 17:16:13 -070085
alshabibe1248b62015-08-20 17:21:55 -070086 private final String appId = "org.onosproject.cli.meterCmd";
alshabib58fe6dc2015-08-19 17:16:13 -070087
Jordi Ortiz3bbfd992017-01-21 19:29:52 +010088 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
alshabib58fe6dc2015-08-19 17:16:13 -0700141 @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 Ortiz3bbfd992017-01-21 19:29:52 +0100148 checkOptions();
alshabib58fe6dc2015-08-19 17:16:13 -0700149
150
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100151 MeterRequest.Builder builder = DefaultMeterRequest.builder()
alshabib58fe6dc2015-08-19 17:16:13 -0700152 .forDevice(deviceId)
153 .fromApp(coreService.registerApplication(appId))
Jordi Ortiz3bbfd992017-01-21 19:29:52 +0100154 .withUnit(unit)
155 .withBands(bands);
156
157
158 if (isBurst) {
159 builder = builder.burst();
160 }
161
162 MeterRequest request = builder.add();
alshabib58fe6dc2015-08-19 17:16:13 -0700163
Jordi Ortiz161445a2017-02-15 18:29:04 +0100164 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());
alshabib58fe6dc2015-08-19 17:16:13 -0700167 }
168}