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