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