Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 18 | import java.util.List; |
| 19 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.ConnectPoint; |
| 23 | import org.onlab.onos.net.DeviceId; |
| 24 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.TrafficSelector; |
| 26 | import org.onlab.onos.net.flow.TrafficTreatment; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 27 | import org.onlab.onos.net.intent.Constraint; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.IntentService; |
| 30 | import org.onlab.onos.net.intent.PointToPointIntent; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 31 | |
| 32 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 33 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 34 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 35 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 36 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Installs point-to-point connectivity intents. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "add-point-intent", |
| 41 | description = "Installs point-to-point connectivity intent") |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 42 | public class AddPointToPointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 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 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 54 | @Override |
| 55 | protected void execute() { |
| 56 | IntentService service = get(IntentService.class); |
| 57 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 58 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 59 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 60 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 61 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 62 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 63 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 64 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 65 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 66 | TrafficSelector selector = buildTrafficSelector(); |
| 67 | TrafficTreatment treatment = builder().build(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 69 | List<Constraint> constraints = buildConstraints(); |
| 70 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 71 | Intent intent = new PointToPointIntent(appId(), selector, treatment, |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 72 | ingress, egress, constraints); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 73 | service.submit(intent); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Extracts the port number portion of the ConnectPoint. |
| 78 | * |
| 79 | * @param deviceString string representing the device/port |
| 80 | * @return port number as a string, empty string if the port is not found |
| 81 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 82 | public static String getPortNumber(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 83 | int slash = deviceString.indexOf('/'); |
| 84 | if (slash <= 0) { |
| 85 | return ""; |
| 86 | } |
| 87 | return deviceString.substring(slash + 1, deviceString.length()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Extracts the device ID portion of the ConnectPoint. |
| 92 | * |
| 93 | * @param deviceString string representing the device/port |
| 94 | * @return device ID string |
| 95 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 96 | public static String getDeviceId(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 97 | int slash = deviceString.indexOf('/'); |
| 98 | if (slash <= 0) { |
| 99 | return ""; |
| 100 | } |
| 101 | return deviceString.substring(0, slash); |
| 102 | } |
| 103 | } |