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