Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 23 | import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 25 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 26 | import org.openstack4j.model.network.Port; |
| 27 | |
| 28 | import java.util.Optional; |
| 29 | |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame] | 30 | import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR; |
| 31 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getIntfNameFromPciAddress; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 32 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
| 33 | |
| 34 | /** |
| 35 | * When SR-IOV-based VM is instantiated while the ovsdb connection to the device is lost, |
| 36 | * VM is instantiated but the related VF port can't be added. |
| 37 | * After recovering ovsdb connection, you can manually add VF ports by this CLI. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "openstack-direct-port-add", |
| 40 | description = "Manually adds OpenStack direct ports to the device") |
| 41 | public class OpenstackDirectPortAddCommand extends AbstractShellCommand { |
| 42 | @Argument(index = 0, name = "port ID", description = "port ID", required = true) |
| 43 | private String portId = null; |
| 44 | |
| 45 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 46 | protected void doExecute() { |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 47 | OpenstackNetworkService osNetService = AbstractShellCommand.get(OpenstackNetworkService.class); |
| 48 | OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
| 49 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 50 | |
| 51 | Port port = osNetService.port(portId); |
| 52 | if (port == null) { |
| 53 | log.error("There's no port that matches the port ID {}", portId); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | Optional<OpenstackNode> osNode = osNodeService.completeNodes(COMPUTE).stream() |
| 58 | .filter(node -> node.hostname().equals(port.getHostId())) |
| 59 | .findAny(); |
| 60 | if (!osNode.isPresent()) { |
| 61 | log.error("There's no openstackNode that matches hostname {}", |
| 62 | port.getHostId()); |
| 63 | return; |
| 64 | } |
| 65 | |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame] | 66 | String intfName = getIntfNameFromPciAddress(port); |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 67 | if (intfName == null) { |
| 68 | log.error("Failed to retrieve interface name from a port {}", portId); |
| 69 | return; |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame] | 70 | } else if (intfName.equals(UNSUPPORTED_VENDOR)) { |
| 71 | log.warn("Failed to retrieve interface name from a port {} because of unsupported ovs-based sr-iov"); |
| 72 | return; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (OpenstackNetworkingUtil.hasIntfAleadyInDevice(osNode.get().intgBridge(), |
| 76 | intfName, deviceService)) { |
| 77 | log.error("Interface {} is already added to the device {}", osNode.get().intgBridge()); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | log.info("Adding interface {} to the device {}..", intfName, |
| 82 | osNode.get().intgBridge()); |
| 83 | |
| 84 | osNodeService.addVfPort(osNode.get(), intfName); |
| 85 | } |
| 86 | } |