Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.onlab.onos.cli.net; |
| 20 | |
| 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.onos.net.ConnectPoint; |
| 24 | import org.onlab.onos.net.DeviceId; |
| 25 | import org.onlab.onos.net.PortNumber; |
| 26 | import org.onlab.onos.net.flow.TrafficSelector; |
| 27 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 28 | import org.onlab.onos.net.intent.Intent; |
| 29 | import org.onlab.onos.net.intent.IntentService; |
| 30 | import org.onlab.onos.net.intent.PointToPointIntentWithBandwidthConstraint; |
| 31 | import org.onlab.onos.net.resource.BandwidthResourceRequest; |
| 32 | |
| 33 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 34 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 35 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 36 | |
| 37 | /** |
| 38 | * Installs point-to-point connectivity intents. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "add-point-intent-bw", |
| 41 | description = "Installs point-to-point connectivity intent with bandwidth constraint") |
| 42 | public class AddPointToPointIntentWithBandwidthConstraintCommand extends ConnectivityIntentCommand { |
| 43 | |
| 44 | @Argument(index = 0, name = "ingressDevice", |
| 45 | description = "Ingress Device/Port Description", |
| 46 | required = true, multiValued = false) |
| 47 | String ingressDeviceString = null; |
| 48 | |
| 49 | @Argument(index = 1, name = "egressDevice", |
| 50 | description = "Egress Device/Port Description", |
| 51 | required = true, multiValued = false) |
| 52 | String egressDeviceString = null; |
| 53 | |
| 54 | @Argument(index = 2, name = "bandwidth", |
| 55 | description = "Bandwidth", |
| 56 | required = true, multiValued = false) |
| 57 | String bandwidthString = null; |
| 58 | |
| 59 | @Override |
| 60 | protected void execute() { |
| 61 | IntentService service = get(IntentService.class); |
| 62 | |
| 63 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 64 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
| 65 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 66 | |
| 67 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 68 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
| 69 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 70 | |
| 71 | long bandwidth = Long.parseLong(bandwidthString); |
| 72 | |
| 73 | TrafficSelector selector = buildTrafficSelector(); |
| 74 | TrafficTreatment treatment = builder().build(); |
| 75 | |
| 76 | Intent intent = new PointToPointIntentWithBandwidthConstraint( |
| 77 | appId(), selector, treatment, |
| 78 | ingress, egress, new BandwidthResourceRequest(bandwidth)); |
| 79 | service.submit(intent); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Extracts the port number portion of the ConnectPoint. |
| 84 | * |
| 85 | * @param deviceString string representing the device/port |
| 86 | * @return port number as a string, empty string if the port is not found |
| 87 | */ |
| 88 | private String getPortNumber(String deviceString) { |
| 89 | int slash = deviceString.indexOf('/'); |
| 90 | if (slash <= 0) { |
| 91 | return ""; |
| 92 | } |
| 93 | return deviceString.substring(slash + 1, deviceString.length()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Extracts the device ID portion of the ConnectPoint. |
| 98 | * |
| 99 | * @param deviceString string representing the device/port |
| 100 | * @return device ID string |
| 101 | */ |
| 102 | private String getDeviceId(String deviceString) { |
| 103 | int slash = deviceString.indexOf('/'); |
| 104 | if (slash <= 0) { |
| 105 | return ""; |
| 106 | } |
| 107 | return deviceString.substring(0, slash); |
| 108 | } |
| 109 | } |