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