blob: 8396e9dfb5f252e1c25d9b1adb80d68856dd87b9 [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
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080022/**
23 * Installs MPLS intents.
24 */
Michele Santuari4b6019e2014-12-19 11:31:45 +010025@Command(scope = "onos", name = "add-mpls-intent", description = "Installs mpls connectivity intent")
26public 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 Milkeyebc5d222015-03-18 15:45:36 -070081 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 Santuari4b6019e2014-12-19 11:31:45 +010092 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}