blob: 56bbdaf8a8c39c376c089faa8af42d1da5b284e3 [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.cli.net;
2
Ray Milkey0742ec92014-10-13 08:39:55 -07003import 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 Milkey0742ec92014-10-13 08:39:55 -070014import org.onlab.onos.net.intent.IntentService;
15import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
16import org.onlab.packet.Ethernet;
17
Thomas Vachuskab97cf282014-10-20 23:31:12 -070018import java.util.HashSet;
19import java.util.Set;
20
21import static org.onlab.onos.net.DeviceId.deviceId;
22import static org.onlab.onos.net.PortNumber.portNumber;
23
Ray Milkey0742ec92014-10-13 08:39:55 -070024/**
25 * Installs point-to-point connectivity intents.
26 */
27@Command(scope = "onos", name = "add-multi-to-single-intent",
28 description = "Installs point-to-point connectivity intent")
29public class AddMultiPointToSinglePointIntentCommand extends AbstractShellCommand {
30
31 @Argument(index = 0, name = "ingressDevices",
32 description = "Ingress Device/Port Description",
33 required = true, multiValued = true)
34 String[] deviceStrings = null;
35
Ray Milkey0742ec92014-10-13 08:39:55 -070036 @Override
37 protected void execute() {
38 IntentService service = get(IntentService.class);
39
40 if (deviceStrings.length < 2) {
41 return;
42 }
43
44 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
46 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070047 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
48 Set<ConnectPoint> ingressPoints = new HashSet<>();
49
50 for (int index = 0; index < deviceStrings.length - 1; index++) {
51 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
53 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070054 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
55 ingressPoints.add(ingress);
56 }
57
Ray Milkey0742ec92014-10-13 08:39:55 -070058 TrafficSelector selector = DefaultTrafficSelector.builder()
59 .matchEthType(Ethernet.TYPE_IPV4)
60 .build();
61 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
62
Thomas Vachuskab97cf282014-10-20 23:31:12 -070063 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
64 ingressPoints, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070065 service.submit(intent);
66 }
67
68 /**
69 * Extracts the port number portion of the ConnectPoint.
70 *
71 * @param deviceString string representing the device/port
72 * @return port number as a string, empty string if the port is not found
73 */
74 private String getPortNumber(String deviceString) {
75 int slash = deviceString.indexOf('/');
76 if (slash <= 0) {
77 return "";
78 }
79 return deviceString.substring(slash + 1, deviceString.length());
80 }
81
82 /**
83 * Extracts the device ID portion of the ConnectPoint.
84 *
85 * @param deviceString string representing the device/port
86 * @return device ID string
87 */
88 private String getDeviceId(String deviceString) {
89 int slash = deviceString.indexOf('/');
90 if (slash <= 0) {
91 return "";
92 }
93 return deviceString.substring(0, slash);
94 }
95}