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