blob: 89ec7f679f72b5e5988fba1ebf2c5571390daf87 [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;
14import org.onlab.onos.net.intent.IntentId;
15import org.onlab.onos.net.intent.IntentService;
16import org.onlab.onos.net.intent.PointToPointIntent;
Brian O'Connorf10fcf62014-10-08 15:35:59 -070017import org.onlab.packet.Ethernet;
Ray Milkeya058c732014-10-08 13:52:34 -070018
19/**
20 * Installs point-to-point connectivity intents.
21 */
22@Command(scope = "onos", name = "add-point-intent",
23 description = "Installs point-to-point connectivity intent")
24public class AddPointToPointIntentCommand extends AbstractShellCommand {
25
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
Brian O'Connorf10fcf62014-10-08 15:35:59 -070036 private static long id = 0x7470001;
Ray Milkeya058c732014-10-08 13:52:34 -070037
38 @Override
39 protected void execute() {
40 IntentService service = get(IntentService.class);
41
42 DeviceId ingressDeviceId = DeviceId.deviceId(getDeviceId(ingressDeviceString));
43 PortNumber ingressPortNumber =
44 PortNumber.portNumber(getPortNumber(ingressDeviceString));
45 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
46
47 DeviceId egressDeviceId = DeviceId.deviceId(getDeviceId(egressDeviceString));
48 PortNumber egressPortNumber =
49 PortNumber.portNumber(getPortNumber(egressDeviceString));
50 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
51
Brian O'Connorf10fcf62014-10-08 15:35:59 -070052 TrafficSelector selector = DefaultTrafficSelector.builder()
53 .matchEthType(Ethernet.TYPE_IPV4)
54 .build();
Ray Milkeya058c732014-10-08 13:52:34 -070055 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
56
57 Intent intent =
58 new PointToPointIntent(new IntentId(id++),
59 selector,
60 treatment,
61 ingress,
62 egress);
63 service.submit(intent);
64 }
65
66 /**
67 * Extracts the port number portion of the ConnectPoint.
68 *
69 * @param deviceString string representing the device/port
70 * @return port number as a string, empty string if the port is not found
71 */
72 private String getPortNumber(String deviceString) {
73 int slash = deviceString.indexOf('/');
74 if (slash <= 0) {
75 return "";
76 }
77 return deviceString.substring(slash + 1, deviceString.length());
78 }
79
80 /**
81 * Extracts the device ID portion of the ConnectPoint.
82 *
83 * @param deviceString string representing the device/port
84 * @return device ID string
85 */
86 private String getDeviceId(String deviceString) {
87 int slash = deviceString.indexOf('/');
88 if (slash <= 0) {
89 return "";
90 }
91 return deviceString.substring(0, slash);
92 }
93}