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