blob: 25a7726bda7a022af1bdca0064a48bcb12199b08 [file] [log] [blame]
alshabib58fe6dc2015-08-19 17:16:13 -07001/*
2 * Copyright 2015 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 */
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.DefaultMeter;
26import org.onosproject.net.meter.Meter;
27import org.onosproject.net.meter.MeterId;
28import org.onosproject.net.meter.MeterOperation;
29import org.onosproject.net.meter.MeterService;
30
31import java.util.Collections;
32
33/**
34 * Add a meter.
35 */
36@Command(scope = "onos", name = "add-meter",
37 description = "Adds a meter to a device (currently for testing)")
38public class AddMeter extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "uri", description = "Device ID",
41 required = true, multiValued = false)
42 String uri = null;
43
44 private final String appId = "org.onosproject.cli.addMeter";
45
46 @Override
47 protected void execute() {
48 MeterService service = get(MeterService.class);
49 CoreService coreService = get(CoreService.class);
50
51 DeviceId deviceId = DeviceId.deviceId(uri);
52
53 MeterId meterId = service.allocateMeterId();
54
55 Band band = DefaultBand.builder()
56 .ofType(Band.Type.DROP)
57 .withRate(500)
58 .build();
59
60
61 Meter meter = DefaultMeter.builder()
62 .forDevice(deviceId)
63 .fromApp(coreService.registerApplication(appId))
64 .withId(meterId)
65 .withUnit(Meter.Unit.KB_PER_SEC)
66 .withBands(Collections.singleton(band))
67 .build();
68
69 MeterOperation op = new MeterOperation(meter, MeterOperation.Type.ADD, null);
70
71 service.addMeter(op);
72
73 }
74}