blob: 15cf1769bd8549bbceb98e6300a7429f399010c3 [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;
17
18/**
19 * Installs point-to-point connectivity intents.
20 */
21@Command(scope = "onos", name = "add-point-intent",
22 description = "Installs point-to-point connectivity intent")
23public class AddPointToPointIntentCommand extends AbstractShellCommand {
24
25 @Argument(index = 0, name = "ingressDevice",
26 description = "Ingress Device/Port Description",
27 required = true, multiValued = false)
28 String ingressDeviceString = null;
29
30 @Argument(index = 1, name = "egressDevice",
31 description = "Egress Device/Port Description",
32 required = true, multiValued = false)
33 String egressDeviceString = null;
34
35 private static long id = 1;
36
37 @Override
38 protected void execute() {
39 IntentService service = get(IntentService.class);
40
41 DeviceId ingressDeviceId = DeviceId.deviceId(getDeviceId(ingressDeviceString));
42 PortNumber ingressPortNumber =
43 PortNumber.portNumber(getPortNumber(ingressDeviceString));
44 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
45
46 DeviceId egressDeviceId = DeviceId.deviceId(getDeviceId(egressDeviceString));
47 PortNumber egressPortNumber =
48 PortNumber.portNumber(getPortNumber(egressDeviceString));
49 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
50
51 TrafficSelector selector = DefaultTrafficSelector.builder().build();
52 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
53
54 Intent intent =
55 new PointToPointIntent(new IntentId(id++),
56 selector,
57 treatment,
58 ingress,
59 egress);
60 service.submit(intent);
61 }
62
63 /**
64 * Extracts the port number portion of the ConnectPoint.
65 *
66 * @param deviceString string representing the device/port
67 * @return port number as a string, empty string if the port is not found
68 */
69 private String getPortNumber(String deviceString) {
70 int slash = deviceString.indexOf('/');
71 if (slash <= 0) {
72 return "";
73 }
74 return deviceString.substring(slash + 1, deviceString.length());
75 }
76
77 /**
78 * Extracts the device ID portion of the ConnectPoint.
79 *
80 * @param deviceString string representing the device/port
81 * @return device ID string
82 */
83 private String getDeviceId(String deviceString) {
84 int slash = deviceString.indexOf('/');
85 if (slash <= 0) {
86 return "";
87 }
88 return deviceString.substring(0, slash);
89 }
90}