Jordi Ortiz | 1d8cc49 | 2017-01-27 14:28:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.meter.Band; |
| 24 | import org.onosproject.net.meter.DefaultBand; |
| 25 | import org.onosproject.net.meter.DefaultMeterRequest; |
| 26 | import org.onosproject.net.meter.Meter; |
| 27 | import org.onosproject.net.meter.MeterId; |
| 28 | import org.onosproject.net.meter.MeterRequest; |
| 29 | import org.onosproject.net.meter.MeterService; |
| 30 | |
| 31 | import 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)") |
| 38 | public 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 | |
| 44 | @Argument(index = 1, name = "meterId", description = "Meter ID", |
| 45 | 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); |
| 56 | MeterId meterId = MeterId.meterId(Long.parseLong(meterIdstr)); |
| 57 | |
| 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); |
| 67 | } |
| 68 | } |