alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 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; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 25 | import org.onosproject.net.meter.DefaultMeterRequest; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 26 | import org.onosproject.net.meter.Meter; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 27 | import org.onosproject.net.meter.MeterRequest; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 28 | import org.onosproject.net.meter.MeterService; |
| 29 | |
| 30 | import java.util.Collections; |
| 31 | |
| 32 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 33 | * Add a meter to a device. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 34 | */ |
| 35 | @Command(scope = "onos", name = "add-meter", |
| 36 | description = "Adds a meter to a device (currently for testing)") |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 37 | public class AddMeterCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 38 | |
| 39 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 40 | required = true, multiValued = false) |
| 41 | String uri = null; |
| 42 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 43 | private final String appId = "org.onosproject.cli.meterCmd"; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | MeterService service = get(MeterService.class); |
| 48 | CoreService coreService = get(CoreService.class); |
| 49 | |
| 50 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 51 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 52 | Band band = DefaultBand.builder() |
| 53 | .ofType(Band.Type.DROP) |
| 54 | .withRate(500) |
| 55 | .build(); |
| 56 | |
| 57 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 58 | MeterRequest request = DefaultMeterRequest.builder() |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 59 | .forDevice(deviceId) |
| 60 | .fromApp(coreService.registerApplication(appId)) |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 61 | .withUnit(Meter.Unit.KB_PER_SEC) |
| 62 | .withBands(Collections.singleton(band)) |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 63 | .add(); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 64 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 65 | service.submit(request); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 66 | |
| 67 | } |
| 68 | } |