Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.intent.Constraint; |
| 26 | import org.onosproject.net.intent.Intent; |
| 27 | import org.onosproject.net.intent.IntentService; |
| 28 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 29 | |
Jonathan Hart | 9a4157b | 2015-02-05 10:19:29 -0800 | [diff] [blame] | 30 | import java.util.HashSet; |
| 31 | import java.util.List; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import static org.onosproject.net.DeviceId.deviceId; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
| 36 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 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 Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 42 | public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 9a4157b | 2015-02-05 10:19:29 -0800 | [diff] [blame] | 44 | @Argument(index = 0, name = "ingressDevices egressDevice", |
| 45 | description = "ingressDevice/Port..ingressDevice/Port egressDevice/Port", |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 46 | required = true, multiValued = true) |
| 47 | String[] deviceStrings = null; |
| 48 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | @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 Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 58 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 59 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 60 | 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 Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 65 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 66 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 67 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 68 | ingressPoints.add(ingress); |
| 69 | } |
| 70 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 71 | TrafficSelector selector = buildTrafficSelector(); |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 72 | TrafficTreatment treatment = buildTrafficTreatment(); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 73 | List<Constraint> constraints = buildConstraints(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 74 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 75 | Intent intent = new MultiPointToSinglePointIntent(appId(), key(), |
| 76 | selector, treatment, |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 77 | ingressPoints, egress, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 78 | constraints, |
| 79 | priority()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 80 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 81 | print("Multipoint to single point intent submitted:\n%s", intent.toString()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Extracts the port number portion of the ConnectPoint. |
| 86 | * |
| 87 | * @param deviceString string representing the device/port |
| 88 | * @return port number as a string, empty string if the port is not found |
| 89 | */ |
| 90 | private String getPortNumber(String deviceString) { |
| 91 | int slash = deviceString.indexOf('/'); |
| 92 | if (slash <= 0) { |
| 93 | return ""; |
| 94 | } |
| 95 | return deviceString.substring(slash + 1, deviceString.length()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Extracts the device ID portion of the ConnectPoint. |
| 100 | * |
| 101 | * @param deviceString string representing the device/port |
| 102 | * @return device ID string |
| 103 | */ |
| 104 | private String getDeviceId(String deviceString) { |
| 105 | int slash = deviceString.indexOf('/'); |
| 106 | if (slash <= 0) { |
| 107 | return ""; |
| 108 | } |
| 109 | return deviceString.substring(0, slash); |
| 110 | } |
| 111 | } |