Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -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 |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -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. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 15 | */ |
| 16 | package org.onlab.onos.cli.net; |
| 17 | |
| 18 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 19 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 20 | |
| 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.onos.net.ConnectPoint; |
| 24 | import org.onlab.onos.net.DeviceId; |
| 25 | import org.onlab.onos.net.PortNumber; |
| 26 | import org.onlab.onos.net.intent.Intent; |
| 27 | import org.onlab.onos.net.intent.IntentService; |
| 28 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 29 | |
| 30 | /** |
| 31 | * Installs optical connectivity intents. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "add-optical-intent", |
| 34 | description = "Installs optical connectivity intent") |
| 35 | public class AddOpticalIntentCommand extends ConnectivityIntentCommand { |
| 36 | |
| 37 | @Argument(index = 0, name = "ingressDevice", |
| 38 | description = "Ingress Device/Port Description", |
| 39 | required = true, multiValued = false) |
| 40 | String ingressDeviceString = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "egressDevice", |
| 43 | description = "Egress Device/Port Description", |
| 44 | required = true, multiValued = false) |
| 45 | String egressDeviceString = null; |
| 46 | |
| 47 | @Override |
| 48 | protected void execute() { |
| 49 | IntentService service = get(IntentService.class); |
| 50 | |
| 51 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 52 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
| 53 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 54 | |
| 55 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 56 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
| 57 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 58 | |
| 59 | Intent intent = new OpticalConnectivityIntent(appId(), ingress, egress); |
| 60 | service.submit(intent); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Extracts the port number portion of the ConnectPoint. |
| 65 | * |
| 66 | * @param deviceString string representing the device/port |
| 67 | * @return port number as a string, empty string if the port is not found |
| 68 | */ |
| 69 | private String getPortNumber(String deviceString) { |
| 70 | int slash = deviceString.indexOf('/'); |
| 71 | if (slash <= 0) { |
| 72 | return ""; |
| 73 | } |
| 74 | return deviceString.substring(slash + 1, deviceString.length()); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Extracts the device ID portion of the ConnectPoint. |
| 79 | * |
| 80 | * @param deviceString string representing the device/port |
| 81 | * @return device ID string |
| 82 | */ |
| 83 | private String getDeviceId(String deviceString) { |
| 84 | int slash = deviceString.indexOf('/'); |
| 85 | if (slash <= 0) { |
| 86 | return ""; |
| 87 | } |
| 88 | return deviceString.substring(0, slash); |
| 89 | } |
| 90 | } |