blob: 9e037d29b6c711dd65f51546302d051906eb2327 [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;
Ray Milkeya058c732014-10-08 13:52:34 -07005import org.onlab.onos.net.ConnectPoint;
6import org.onlab.onos.net.DeviceId;
7import org.onlab.onos.net.PortNumber;
Ray Milkeya058c732014-10-08 13:52:34 -07008import org.onlab.onos.net.flow.TrafficSelector;
9import org.onlab.onos.net.flow.TrafficTreatment;
10import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070011import org.onlab.onos.net.intent.IntentService;
12import org.onlab.onos.net.intent.PointToPointIntent;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070013
14import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeya058c732014-10-08 13:52:34 -070015
Thomas Vachuskab97cf282014-10-20 23:31:12 -070016import static org.onlab.onos.net.DeviceId.deviceId;
17import static org.onlab.onos.net.PortNumber.portNumber;
18
Ray Milkeya058c732014-10-08 13:52:34 -070019/**
20 * Installs point-to-point connectivity intents.
21 */
22@Command(scope = "onos", name = "add-point-intent",
23 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070024public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070025
26 @Argument(index = 0, name = "ingressDevice",
27 description = "Ingress Device/Port Description",
28 required = true, multiValued = false)
29 String ingressDeviceString = null;
30
31 @Argument(index = 1, name = "egressDevice",
32 description = "Egress Device/Port Description",
33 required = true, multiValued = false)
34 String egressDeviceString = null;
35
Ray Milkeya058c732014-10-08 13:52:34 -070036 @Override
37 protected void execute() {
38 IntentService service = get(IntentService.class);
39
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
41 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070042 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
43
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
45 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070046 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
47
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070048 TrafficSelector selector = buildTrafficSelector();
49 TrafficTreatment treatment = builder().build();
Ray Milkeya058c732014-10-08 13:52:34 -070050
Thomas Vachuskab97cf282014-10-20 23:31:12 -070051 Intent intent = new PointToPointIntent(appId(), selector, treatment,
52 ingress, egress);
Ray Milkeya058c732014-10-08 13:52:34 -070053 service.submit(intent);
54 }
55
56 /**
57 * Extracts the port number portion of the ConnectPoint.
58 *
59 * @param deviceString string representing the device/port
60 * @return port number as a string, empty string if the port is not found
61 */
62 private String getPortNumber(String deviceString) {
63 int slash = deviceString.indexOf('/');
64 if (slash <= 0) {
65 return "";
66 }
67 return deviceString.substring(slash + 1, deviceString.length());
68 }
69
70 /**
71 * Extracts the device ID portion of the ConnectPoint.
72 *
73 * @param deviceString string representing the device/port
74 * @return device ID string
75 */
76 private String getDeviceId(String deviceString) {
77 int slash = deviceString.indexOf('/');
78 if (slash <= 0) {
79 return "";
80 }
81 return deviceString.substring(0, slash);
82 }
83}