Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.ConnectPoint; |
| 24 | import org.onlab.onos.net.DeviceId; |
| 25 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onlab.onos.net.flow.TrafficSelector; |
| 28 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 29 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.intent.IntentService; |
| 31 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 32 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 33 | import java.util.HashSet; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 37 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 38 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Installs point-to-point connectivity intents. |
| 41 | */ |
| 42 | @Command(scope = "onos", name = "add-multi-to-single-intent", |
| 43 | description = "Installs point-to-point connectivity intent") |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 44 | public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 45 | |
| 46 | @Argument(index = 0, name = "ingressDevices", |
| 47 | description = "Ingress Device/Port Description", |
| 48 | required = true, multiValued = true) |
| 49 | String[] deviceStrings = null; |
| 50 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 51 | @Override |
| 52 | protected void execute() { |
| 53 | IntentService service = get(IntentService.class); |
| 54 | |
| 55 | if (deviceStrings.length < 2) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | String egressDeviceString = deviceStrings[deviceStrings.length - 1]; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 60 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 61 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 62 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 63 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 64 | |
| 65 | for (int index = 0; index < deviceStrings.length - 1; index++) { |
| 66 | String ingressDeviceString = deviceStrings[index]; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 67 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 68 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 69 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 70 | ingressPoints.add(ingress); |
| 71 | } |
| 72 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 73 | TrafficSelector selector = buildTrafficSelector(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 74 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 75 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 76 | Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment, |
| 77 | ingressPoints, egress); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 78 | 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 | } |