blob: bd24366f7d7733a0cb4bb3738240ed3542c118c4 [file] [log] [blame]
Ray Milkeya058c732014-10-08 13:52:34 -07001package org.onlab.onos.cli.net;
2
3import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
5import org.onlab.onos.cli.AbstractShellCommand;
6import org.onlab.onos.net.ConnectPoint;
7import org.onlab.onos.net.DeviceId;
8import org.onlab.onos.net.PortNumber;
9import org.onlab.onos.net.flow.DefaultTrafficSelector;
10import org.onlab.onos.net.flow.DefaultTrafficTreatment;
11import org.onlab.onos.net.flow.TrafficSelector;
12import org.onlab.onos.net.flow.TrafficTreatment;
13import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070014import org.onlab.onos.net.intent.IntentService;
15import org.onlab.onos.net.intent.PointToPointIntent;
Brian O'Connorf10fcf62014-10-08 15:35:59 -070016import org.onlab.packet.Ethernet;
Ray Milkeya058c732014-10-08 13:52:34 -070017
Thomas Vachuskab97cf282014-10-20 23:31:12 -070018import static org.onlab.onos.net.DeviceId.deviceId;
19import static org.onlab.onos.net.PortNumber.portNumber;
20
Ray Milkeya058c732014-10-08 13:52:34 -070021/**
22 * Installs point-to-point connectivity intents.
23 */
24@Command(scope = "onos", name = "add-point-intent",
25 description = "Installs point-to-point connectivity intent")
26public class AddPointToPointIntentCommand extends AbstractShellCommand {
27
28 @Argument(index = 0, name = "ingressDevice",
29 description = "Ingress Device/Port Description",
30 required = true, multiValued = false)
31 String ingressDeviceString = null;
32
33 @Argument(index = 1, name = "egressDevice",
34 description = "Egress Device/Port Description",
35 required = true, multiValued = false)
36 String egressDeviceString = null;
37
Ray Milkeya058c732014-10-08 13:52:34 -070038 @Override
39 protected void execute() {
40 IntentService service = get(IntentService.class);
41
Thomas Vachuskab97cf282014-10-20 23:31:12 -070042 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
43 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070044 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
45
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
47 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070048 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
49
Brian O'Connorf10fcf62014-10-08 15:35:59 -070050 TrafficSelector selector = DefaultTrafficSelector.builder()
51 .matchEthType(Ethernet.TYPE_IPV4)
52 .build();
Ray Milkeya058c732014-10-08 13:52:34 -070053 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
54
Thomas Vachuskab97cf282014-10-20 23:31:12 -070055 Intent intent = new PointToPointIntent(appId(), selector, treatment,
56 ingress, egress);
Ray Milkeya058c732014-10-08 13:52:34 -070057 service.submit(intent);
58 }
59
60 /**
61 * Extracts the port number portion of the ConnectPoint.
62 *
63 * @param deviceString string representing the device/port
64 * @return port number as a string, empty string if the port is not found
65 */
66 private String getPortNumber(String deviceString) {
67 int slash = deviceString.indexOf('/');
68 if (slash <= 0) {
69 return "";
70 }
71 return deviceString.substring(slash + 1, deviceString.length());
72 }
73
74 /**
75 * Extracts the device ID portion of the ConnectPoint.
76 *
77 * @param deviceString string representing the device/port
78 * @return device ID string
79 */
80 private String getDeviceId(String deviceString) {
81 int slash = deviceString.indexOf('/');
82 if (slash <= 0) {
83 return "";
84 }
85 return deviceString.substring(0, slash);
86 }
87}