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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.ConnectPoint; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.TrafficSelector; |
| 24 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 25 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.intent.IntentService; |
| 27 | import org.onlab.onos.net.intent.PointToPointIntent; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 28 | |
| 29 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 31 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 32 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 33 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Installs point-to-point connectivity intents. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "add-point-intent", |
| 38 | description = "Installs point-to-point connectivity intent") |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 39 | public class AddPointToPointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 0, name = "ingressDevice", |
| 42 | description = "Ingress Device/Port Description", |
| 43 | required = true, multiValued = false) |
| 44 | String ingressDeviceString = null; |
| 45 | |
| 46 | @Argument(index = 1, name = "egressDevice", |
| 47 | description = "Egress Device/Port Description", |
| 48 | required = true, multiValued = false) |
| 49 | String egressDeviceString = null; |
| 50 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 51 | @Override |
| 52 | protected void execute() { |
| 53 | IntentService service = get(IntentService.class); |
| 54 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 55 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 56 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 57 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 58 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 59 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 60 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 61 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 62 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 63 | TrafficSelector selector = buildTrafficSelector(); |
| 64 | TrafficTreatment treatment = builder().build(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 65 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 66 | Intent intent = new PointToPointIntent(appId(), selector, treatment, |
| 67 | ingress, egress); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 68 | service.submit(intent); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Extracts the port number portion of the ConnectPoint. |
| 73 | * |
| 74 | * @param deviceString string representing the device/port |
| 75 | * @return port number as a string, empty string if the port is not found |
| 76 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 77 | public static String getPortNumber(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 78 | int slash = deviceString.indexOf('/'); |
| 79 | if (slash <= 0) { |
| 80 | return ""; |
| 81 | } |
| 82 | return deviceString.substring(slash + 1, deviceString.length()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Extracts the device ID portion of the ConnectPoint. |
| 87 | * |
| 88 | * @param deviceString string representing the device/port |
| 89 | * @return device ID string |
| 90 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 91 | public static String getDeviceId(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 92 | int slash = deviceString.indexOf('/'); |
| 93 | if (slash <= 0) { |
| 94 | return ""; |
| 95 | } |
| 96 | return deviceString.substring(0, slash); |
| 97 | } |
| 98 | } |