blob: 7814476047ba2cc83183ab55d17ecfb569ce7404 [file] [log] [blame]
Jordi Ortiz1d8cc492017-01-27 14:28:53 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordi Ortiz1d8cc492017-01-27 14:28:53 +01003 *
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;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.core.CoreService;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.meter.Band;
24import org.onosproject.net.meter.DefaultBand;
25import org.onosproject.net.meter.DefaultMeterRequest;
26import org.onosproject.net.meter.Meter;
27import org.onosproject.net.meter.MeterId;
28import org.onosproject.net.meter.MeterRequest;
29import org.onosproject.net.meter.MeterService;
30
31import java.util.Collections;
32
33/**
34 * Remove existing meter from device.
35 */
36@Command(scope = "onos", name = "meter-remove",
37 description = "Removes a meter from a device (currently for testing)")
38public class MeterRemoveCommand extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "uri", description = "Device ID",
41 required = true, multiValued = false)
42 private String uri = null;
43
Jordi Ortiza19c1882017-04-21 19:37:09 +020044 @Argument(index = 1, name = "meterId", description = "Meter ID hexadecimal value",
Jordi Ortiz1d8cc492017-01-27 14:28:53 +010045 required = true, multiValued = false)
46 private String meterIdstr = null;
47
48 private final String appId = "org.onosproject.cli.meterCmd";
49
50 @Override
51 protected void execute() {
52 MeterService service = get(MeterService.class);
53 CoreService coreService = get(CoreService.class);
54
55 DeviceId deviceId = DeviceId.deviceId(uri);
Jordi Ortiza19c1882017-04-21 19:37:09 +020056 MeterId meterId = MeterId.meterId(Long.parseLong(meterIdstr, 16));
Jordi Ortiz1d8cc492017-01-27 14:28:53 +010057
58 Band b = new DefaultBand(Band.Type.DROP, 0L, 0L, (short) 0);
59
60 MeterRequest.Builder builder = DefaultMeterRequest.builder()
61 .forDevice(deviceId)
62 .withBands(Collections.singleton(b))
63 .withUnit(Meter.Unit.PKTS_PER_SEC)
64 .fromApp(coreService.registerApplication(appId));
65 MeterRequest meterRequest = builder.remove();
66 service.withdraw(meterRequest, meterId);
Jordi Ortiz1002b0a2017-02-27 14:25:49 +010067 log.info("Requested meter removal: {}", meterRequest.toString());
68
69 print("Requested meter removal: %s", meterRequest.toString());
Jordi Ortiz1d8cc492017-01-27 14:28:53 +010070 }
71}