blob: 1929b726547efcf8ab42b215e41f379b101735d8 [file] [log] [blame]
Michele Santuari4b6019e2014-12-19 11:31:45 +01001package org.onosproject.cli.net;
2
Michele Santuari4b6019e2014-12-19 11:31:45 +01003import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
5import org.apache.karaf.shell.commands.Option;
Michele Santuari4b6019e2014-12-19 11:31:45 +01006import org.onlab.packet.MplsLabel;
7import org.onosproject.net.ConnectPoint;
Michele Santuari4b6019e2014-12-19 11:31:45 +01008import org.onosproject.net.flow.TrafficSelector;
9import org.onosproject.net.flow.TrafficTreatment;
10import org.onosproject.net.intent.Constraint;
11import org.onosproject.net.intent.IntentService;
12import org.onosproject.net.intent.MplsIntent;
13
Jonathan Hartc3af35a2015-04-30 22:20:29 -070014import java.util.List;
15import java.util.Optional;
Ray Milkey50a9b722015-03-12 10:38:55 -070016
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080017/**
18 * Installs MPLS intents.
19 */
Michele Santuari4b6019e2014-12-19 11:31:45 +010020@Command(scope = "onos", name = "add-mpls-intent", description = "Installs mpls connectivity intent")
21public class AddMplsIntent extends ConnectivityIntentCommand {
22
23 @Argument(index = 0, name = "ingressDevice",
24 description = "Ingress Device/Port Description",
25 required = true,
26 multiValued = false)
27 private String ingressDeviceString = null;
28
29 @Option(name = "--ingressLabel",
30 description = "Ingress Mpls label",
31 required = false,
32 multiValued = false)
33 private String ingressLabelString = "";
34
35 @Argument(index = 1, name = "egressDevice",
36 description = "Egress Device/Port Description",
37 required = true,
38 multiValued = false)
39 private String egressDeviceString = null;
40
41 @Option(name = "--egressLabel",
42 description = "Egress Mpls label",
43 required = false,
44 multiValued = false)
45 private String egressLabelString = "";
46
47 @Override
48 protected void execute() {
49 IntentService service = get(IntentService.class);
50
Jonathan Hartc3af35a2015-04-30 22:20:29 -070051 ConnectPoint ingress = ConnectPoint.deviceConnectPoint(ingressDeviceString);
Michele Santuari4b6019e2014-12-19 11:31:45 +010052 Optional<MplsLabel> ingressLabel = Optional.empty();
53 if (!ingressLabelString.isEmpty()) {
54 ingressLabel = Optional
55 .ofNullable(MplsLabel.mplsLabel(parseInt(ingressLabelString)));
56 }
57
Jonathan Hartc3af35a2015-04-30 22:20:29 -070058 ConnectPoint egress = ConnectPoint.deviceConnectPoint(egressDeviceString);
Michele Santuari4b6019e2014-12-19 11:31:45 +010059 Optional<MplsLabel> egressLabel = Optional.empty();
Charles Chan4b003c52015-08-23 23:04:27 +080060 if (!egressLabelString.isEmpty()) {
Michele Santuari4b6019e2014-12-19 11:31:45 +010061 egressLabel = Optional
62 .ofNullable(MplsLabel.mplsLabel(parseInt(egressLabelString)));
63 }
64
65 TrafficSelector selector = buildTrafficSelector();
66 TrafficTreatment treatment = buildTrafficTreatment();
67
68 List<Constraint> constraints = buildConstraints();
69
Ray Milkeyebc5d222015-03-18 15:45:36 -070070 MplsIntent intent = MplsIntent.builder()
71 .appId(appId())
72 .selector(selector)
73 .treatment(treatment)
74 .ingressPoint(ingress)
75 .ingressLabel(ingressLabel)
76 .egressPoint(egress)
77 .egressLabel(egressLabel)
78 .constraints(constraints)
79 .priority(priority())
80 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +010081 service.submit(intent);
82 }
83
Michele Santuari4b6019e2014-12-19 11:31:45 +010084 protected Integer parseInt(String value) {
85 try {
86 return Integer.parseInt(value);
87 } catch (NumberFormatException nfe) {
88 return null;
89 }
90 }
91}