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