Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame^] | 1 | package org.onosproject.cli.net; |
| 2 | |
| 3 | import static org.onosproject.net.DeviceId.deviceId; |
| 4 | import static org.onosproject.net.PortNumber.portNumber; |
| 5 | |
| 6 | import java.util.List; |
| 7 | import java.util.Optional; |
| 8 | |
| 9 | import org.apache.karaf.shell.commands.Argument; |
| 10 | import org.apache.karaf.shell.commands.Command; |
| 11 | import org.apache.karaf.shell.commands.Option; |
| 12 | |
| 13 | import org.onlab.packet.MplsLabel; |
| 14 | import org.onosproject.net.ConnectPoint; |
| 15 | import org.onosproject.net.DeviceId; |
| 16 | import org.onosproject.net.PortNumber; |
| 17 | import org.onosproject.net.flow.TrafficSelector; |
| 18 | import org.onosproject.net.flow.TrafficTreatment; |
| 19 | import org.onosproject.net.intent.Constraint; |
| 20 | import org.onosproject.net.intent.IntentService; |
| 21 | import org.onosproject.net.intent.MplsIntent; |
| 22 | |
| 23 | @Command(scope = "onos", name = "add-mpls-intent", description = "Installs mpls connectivity intent") |
| 24 | public class AddMplsIntent extends ConnectivityIntentCommand { |
| 25 | |
| 26 | @Argument(index = 0, name = "ingressDevice", |
| 27 | description = "Ingress Device/Port Description", |
| 28 | required = true, |
| 29 | multiValued = false) |
| 30 | private String ingressDeviceString = null; |
| 31 | |
| 32 | @Option(name = "--ingressLabel", |
| 33 | description = "Ingress Mpls label", |
| 34 | required = false, |
| 35 | multiValued = false) |
| 36 | private String ingressLabelString = ""; |
| 37 | |
| 38 | @Argument(index = 1, name = "egressDevice", |
| 39 | description = "Egress Device/Port Description", |
| 40 | required = true, |
| 41 | multiValued = false) |
| 42 | private String egressDeviceString = null; |
| 43 | |
| 44 | @Option(name = "--egressLabel", |
| 45 | description = "Egress Mpls label", |
| 46 | required = false, |
| 47 | multiValued = false) |
| 48 | private String egressLabelString = ""; |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | IntentService service = get(IntentService.class); |
| 53 | |
| 54 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 55 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
| 56 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, |
| 57 | ingressPortNumber); |
| 58 | Optional<MplsLabel> ingressLabel = Optional.empty(); |
| 59 | if (!ingressLabelString.isEmpty()) { |
| 60 | ingressLabel = Optional |
| 61 | .ofNullable(MplsLabel.mplsLabel(parseInt(ingressLabelString))); |
| 62 | } |
| 63 | |
| 64 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 65 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
| 66 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 67 | |
| 68 | Optional<MplsLabel> egressLabel = Optional.empty(); |
| 69 | if (!ingressLabelString.isEmpty()) { |
| 70 | egressLabel = Optional |
| 71 | .ofNullable(MplsLabel.mplsLabel(parseInt(egressLabelString))); |
| 72 | } |
| 73 | |
| 74 | TrafficSelector selector = buildTrafficSelector(); |
| 75 | TrafficTreatment treatment = buildTrafficTreatment(); |
| 76 | |
| 77 | List<Constraint> constraints = buildConstraints(); |
| 78 | |
| 79 | MplsIntent intent = new MplsIntent(appId(), selector, treatment, |
| 80 | ingress, ingressLabel, egress, |
| 81 | egressLabel, constraints); |
| 82 | service.submit(intent); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Extracts the port number portion of the ConnectPoint. |
| 87 | * |
| 88 | * @param deviceString string representing the device/port |
| 89 | * @return port number as a string, empty string if the port is not found |
| 90 | */ |
| 91 | public static String getPortNumber(String deviceString) { |
| 92 | int slash = deviceString.indexOf('/'); |
| 93 | if (slash <= 0) { |
| 94 | return ""; |
| 95 | } |
| 96 | return deviceString.substring(slash + 1, deviceString.length()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Extracts the device ID portion of the ConnectPoint. |
| 101 | * |
| 102 | * @param deviceString string representing the device/port |
| 103 | * @return device ID string |
| 104 | */ |
| 105 | public static String getDeviceId(String deviceString) { |
| 106 | int slash = deviceString.indexOf('/'); |
| 107 | if (slash <= 0) { |
| 108 | return ""; |
| 109 | } |
| 110 | return deviceString.substring(0, slash); |
| 111 | } |
| 112 | |
| 113 | protected Integer parseInt(String value) { |
| 114 | try { |
| 115 | return Integer.parseInt(value); |
| 116 | } catch (NumberFormatException nfe) { |
| 117 | return null; |
| 118 | } |
| 119 | } |
| 120 | } |