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 | */ |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 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; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.ConnectPoint; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 24 | import org.onlab.onos.net.flow.TrafficSelector; |
| 25 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 26 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.intent.IntentService; |
| 28 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 30 | import java.util.HashSet; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 34 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 35 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Installs point-to-point connectivity intents. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "add-multi-to-single-intent", |
| 40 | description = "Installs point-to-point connectivity intent") |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 41 | public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 42 | |
| 43 | @Argument(index = 0, name = "ingressDevices", |
| 44 | description = "Ingress Device/Port Description", |
| 45 | required = true, multiValued = true) |
| 46 | String[] deviceStrings = null; |
| 47 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 48 | @Override |
| 49 | protected void execute() { |
| 50 | IntentService service = get(IntentService.class); |
| 51 | |
| 52 | if (deviceStrings.length < 2) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | String egressDeviceString = deviceStrings[deviceStrings.length - 1]; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 58 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 59 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 60 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 61 | |
| 62 | for (int index = 0; index < deviceStrings.length - 1; index++) { |
| 63 | String ingressDeviceString = deviceStrings[index]; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 64 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 65 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 66 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 67 | ingressPoints.add(ingress); |
| 68 | } |
| 69 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 70 | TrafficSelector selector = buildTrafficSelector(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 71 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 72 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 73 | Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment, |
| 74 | ingressPoints, egress); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 75 | service.submit(intent); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Extracts the port number portion of the ConnectPoint. |
| 80 | * |
| 81 | * @param deviceString string representing the device/port |
| 82 | * @return port number as a string, empty string if the port is not found |
| 83 | */ |
| 84 | private String getPortNumber(String deviceString) { |
| 85 | int slash = deviceString.indexOf('/'); |
| 86 | if (slash <= 0) { |
| 87 | return ""; |
| 88 | } |
| 89 | return deviceString.substring(slash + 1, deviceString.length()); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Extracts the device ID portion of the ConnectPoint. |
| 94 | * |
| 95 | * @param deviceString string representing the device/port |
| 96 | * @return device ID string |
| 97 | */ |
| 98 | private String getDeviceId(String deviceString) { |
| 99 | int slash = deviceString.indexOf('/'); |
| 100 | if (slash <= 0) { |
| 101 | return ""; |
| 102 | } |
| 103 | return deviceString.substring(0, slash); |
| 104 | } |
| 105 | } |