blob: 089db9954483e499498cd055036b1705bae5adf4 [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;
Ray Milkey0742ec92014-10-13 08:39:55 -07005import org.onlab.onos.net.ConnectPoint;
6import org.onlab.onos.net.DeviceId;
7import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -07008import org.onlab.onos.net.flow.DefaultTrafficTreatment;
9import org.onlab.onos.net.flow.TrafficSelector;
10import org.onlab.onos.net.flow.TrafficTreatment;
11import org.onlab.onos.net.intent.Intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070012import org.onlab.onos.net.intent.IntentService;
13import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070014
Thomas Vachuskab97cf282014-10-20 23:31:12 -070015import java.util.HashSet;
16import java.util.Set;
17
18import static org.onlab.onos.net.DeviceId.deviceId;
19import static org.onlab.onos.net.PortNumber.portNumber;
20
Ray Milkey0742ec92014-10-13 08:39:55 -070021/**
22 * Installs point-to-point connectivity intents.
23 */
24@Command(scope = "onos", name = "add-multi-to-single-intent",
25 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070026public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070027
28 @Argument(index = 0, name = "ingressDevices",
29 description = "Ingress Device/Port Description",
30 required = true, multiValued = true)
31 String[] deviceStrings = null;
32
Ray Milkey0742ec92014-10-13 08:39:55 -070033 @Override
34 protected void execute() {
35 IntentService service = get(IntentService.class);
36
37 if (deviceStrings.length < 2) {
38 return;
39 }
40
41 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070042 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
43 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070044 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
45 Set<ConnectPoint> ingressPoints = new HashSet<>();
46
47 for (int index = 0; index < deviceStrings.length - 1; index++) {
48 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
50 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070051 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
52 ingressPoints.add(ingress);
53 }
54
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070055 TrafficSelector selector = buildTrafficSelector();
Ray Milkey0742ec92014-10-13 08:39:55 -070056 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
57
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
59 ingressPoints, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070060 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}