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