blob: 577743a2e7e10aecf681a002c7025a8ad512dd93 [file] [log] [blame]
Michele Santuari4b6019e2014-12-19 11:31:45 +01001package org.onosproject.cli.net;
2
3import static org.onosproject.net.DeviceId.deviceId;
4import static org.onosproject.net.PortNumber.portNumber;
5
6import java.util.List;
7import java.util.Optional;
8
9import org.apache.karaf.shell.commands.Argument;
10import org.apache.karaf.shell.commands.Command;
11import org.apache.karaf.shell.commands.Option;
12
13import org.onlab.packet.MplsLabel;
14import org.onosproject.net.ConnectPoint;
15import org.onosproject.net.DeviceId;
16import org.onosproject.net.PortNumber;
17import org.onosproject.net.flow.TrafficSelector;
18import org.onosproject.net.flow.TrafficTreatment;
19import org.onosproject.net.intent.Constraint;
20import org.onosproject.net.intent.IntentService;
21import org.onosproject.net.intent.MplsIntent;
22
23@Command(scope = "onos", name = "add-mpls-intent", description = "Installs mpls connectivity intent")
24public 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}