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 | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 18 | import java.util.List; |
| 19 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 22 | import org.apache.karaf.shell.commands.Option; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -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 | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.TrafficSelector; |
| 27 | import org.onlab.onos.net.flow.TrafficTreatment; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 28 | import org.onlab.onos.net.intent.Constraint; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.intent.IntentService; |
| 31 | import org.onlab.onos.net.intent.PointToPointIntent; |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 32 | import org.onlab.packet.MacAddress; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 33 | |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 35 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 36 | import static org.onlab.onos.net.PortNumber.portNumber; |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 37 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 38 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Installs point-to-point connectivity intents. |
| 41 | */ |
| 42 | @Command(scope = "onos", name = "add-point-intent", |
| 43 | description = "Installs point-to-point connectivity intent") |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 44 | public class AddPointToPointIntentCommand extends ConnectivityIntentCommand { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 45 | |
| 46 | @Argument(index = 0, name = "ingressDevice", |
| 47 | description = "Ingress Device/Port Description", |
| 48 | required = true, multiValued = false) |
| 49 | String ingressDeviceString = null; |
| 50 | |
| 51 | @Argument(index = 1, name = "egressDevice", |
| 52 | description = "Egress Device/Port Description", |
| 53 | required = true, multiValued = false) |
| 54 | String egressDeviceString = null; |
| 55 | |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 56 | @Option(name = "--srcMacRewrite", description = "Source MAC address to rewrite", |
| 57 | required = false, multiValued = false) |
| 58 | private String rewriteSrcMacAddressString = null; |
| 59 | |
| 60 | @Option(name = "--dstMacRewrite", description = "Destination MAC address to rewrite", |
| 61 | required = false, multiValued = false) |
| 62 | private String rewriteDstMacAddressString = null; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Generates a traffic treatment for this intent. If the mac address rewrite |
| 67 | * argument is specified the treatment is updated |
| 68 | * to implement the rewrite rule if necessary. |
| 69 | */ |
| 70 | private TrafficTreatment buildTrafficTreatment() { |
| 71 | final TrafficTreatment.Builder builder = builder(); |
| 72 | |
| 73 | if (!isNullOrEmpty(rewriteSrcMacAddressString)) { |
| 74 | final MacAddress rewriteSrcMacAddress = |
| 75 | MacAddress.valueOf(rewriteSrcMacAddressString); |
| 76 | builder.setEthSrc(rewriteSrcMacAddress); |
| 77 | } |
| 78 | if (!isNullOrEmpty(rewriteDstMacAddressString)) { |
| 79 | final MacAddress rewriteDstMacAddress = |
| 80 | MacAddress.valueOf(rewriteDstMacAddressString); |
| 81 | builder.setEthDst(rewriteDstMacAddress); |
| 82 | } |
| 83 | return builder.build(); |
| 84 | } |
| 85 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 86 | @Override |
| 87 | protected void execute() { |
| 88 | IntentService service = get(IntentService.class); |
| 89 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 90 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 91 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 92 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 93 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 94 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 95 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 96 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 97 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 98 | TrafficSelector selector = buildTrafficSelector(); |
Ray Milkey | ac1441d | 2014-11-13 09:31:47 -0800 | [diff] [blame^] | 99 | TrafficTreatment treatment = buildTrafficTreatment(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 100 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 101 | List<Constraint> constraints = buildConstraints(); |
| 102 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 103 | Intent intent = new PointToPointIntent(appId(), selector, treatment, |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 104 | ingress, egress, constraints); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 105 | service.submit(intent); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Extracts the port number portion of the ConnectPoint. |
| 110 | * |
| 111 | * @param deviceString string representing the device/port |
| 112 | * @return port number as a string, empty string if the port is not found |
| 113 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 114 | public static String getPortNumber(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 115 | int slash = deviceString.indexOf('/'); |
| 116 | if (slash <= 0) { |
| 117 | return ""; |
| 118 | } |
| 119 | return deviceString.substring(slash + 1, deviceString.length()); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Extracts the device ID portion of the ConnectPoint. |
| 124 | * |
| 125 | * @param deviceString string representing the device/port |
| 126 | * @return device ID string |
| 127 | */ |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 128 | public static String getDeviceId(String deviceString) { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 129 | int slash = deviceString.indexOf('/'); |
| 130 | if (slash <= 0) { |
| 131 | return ""; |
| 132 | } |
| 133 | return deviceString.substring(0, slash); |
| 134 | } |
| 135 | } |