blob: 6a9787f338897753397e75e39e1e09a49cb453cc [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ray Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.cli.net;
17
Jonathan Hart2c25e362014-11-17 18:14:19 -080018import static org.onlab.onos.net.DeviceId.deviceId;
19import static org.onlab.onos.net.PortNumber.portNumber;
20
21import java.util.HashSet;
22import java.util.List;
23import java.util.Set;
24
Ray Milkey0742ec92014-10-13 08:39:55 -070025import org.apache.karaf.shell.commands.Argument;
26import org.apache.karaf.shell.commands.Command;
Ray Milkey0742ec92014-10-13 08:39:55 -070027import org.onlab.onos.net.ConnectPoint;
28import org.onlab.onos.net.DeviceId;
29import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070030import org.onlab.onos.net.flow.TrafficSelector;
31import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080032import org.onlab.onos.net.intent.Constraint;
Ray Milkey0742ec92014-10-13 08:39:55 -070033import org.onlab.onos.net.intent.Intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070034import org.onlab.onos.net.intent.IntentService;
35import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070036
37/**
38 * Installs point-to-point connectivity intents.
39 */
40@Command(scope = "onos", name = "add-multi-to-single-intent",
41 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070042public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070043
44 @Argument(index = 0, name = "ingressDevices",
45 description = "Ingress Device/Port Description",
46 required = true, multiValued = true)
47 String[] deviceStrings = null;
48
Ray Milkey0742ec92014-10-13 08:39:55 -070049 @Override
50 protected void execute() {
51 IntentService service = get(IntentService.class);
52
53 if (deviceStrings.length < 2) {
54 return;
55 }
56
57 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
59 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070060 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
61 Set<ConnectPoint> ingressPoints = new HashSet<>();
62
63 for (int index = 0; index < deviceStrings.length - 1; index++) {
64 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
66 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070067 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
68 ingressPoints.add(ingress);
69 }
70
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070071 TrafficSelector selector = buildTrafficSelector();
Jonathan Hart2c25e362014-11-17 18:14:19 -080072 TrafficTreatment treatment = buildTrafficTreatment();
Ray Milkey460f4022014-11-05 15:41:43 -080073 List<Constraint> constraints = buildConstraints();
Ray Milkey0742ec92014-10-13 08:39:55 -070074
Thomas Vachuskab97cf282014-10-20 23:31:12 -070075 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
Ray Milkey460f4022014-11-05 15:41:43 -080076 ingressPoints, egress,
77 constraints);
Ray Milkey0742ec92014-10-13 08:39:55 -070078 service.submit(intent);
79 }
80
81 /**
82 * Extracts the port number portion of the ConnectPoint.
83 *
84 * @param deviceString string representing the device/port
85 * @return port number as a string, empty string if the port is not found
86 */
87 private String getPortNumber(String deviceString) {
88 int slash = deviceString.indexOf('/');
89 if (slash <= 0) {
90 return "";
91 }
92 return deviceString.substring(slash + 1, deviceString.length());
93 }
94
95 /**
96 * Extracts the device ID portion of the ConnectPoint.
97 *
98 * @param deviceString string representing the device/port
99 * @return device ID string
100 */
101 private String getDeviceId(String deviceString) {
102 int slash = deviceString.indexOf('/');
103 if (slash <= 0) {
104 return "";
105 }
106 return deviceString.substring(0, slash);
107 }
108}