blob: c627a15cd476cbb8d1c18fc76cdb6acf792e2d1b [file] [log] [blame]
Daniel Park95f73312018-07-31 15:48:34 +09001/*
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 */
16package org.onosproject.openstacknetworking.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Jian Lidb521c12018-11-19 17:42:35 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Park95f73312018-07-31 15:48:34 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.device.DeviceService;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
25import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil;
26import org.onosproject.openstacknode.api.OpenstackNode;
27import org.onosproject.openstacknode.api.OpenstackNodeService;
28import org.openstack4j.model.network.Port;
29
30import java.util.Optional;
31
Daniel Parkec9d1132018-08-19 11:18:03 +090032import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR;
33import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getIntfNameFromPciAddress;
Daniel Park95f73312018-07-31 15:48:34 +090034import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
35
36/**
37 * When SR-IOV-based VM is instantiated while the ovsdb connection to the device is lost,
38 * VM is instantiated but the related VF port can't be added.
39 * After recovering ovsdb connection, you can manually add VF ports by this CLI.
40 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070041@Service
Daniel Park95f73312018-07-31 15:48:34 +090042@Command(scope = "onos", name = "openstack-direct-port-add",
43 description = "Manually adds OpenStack direct ports to the device")
44public class OpenstackDirectPortAddCommand extends AbstractShellCommand {
45 @Argument(index = 0, name = "port ID", description = "port ID", required = true)
Jian Lidb521c12018-11-19 17:42:35 +090046 @Completion(DirectPortListCompleter.class)
Daniel Park95f73312018-07-31 15:48:34 +090047 private String portId = null;
48
49 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 protected void doExecute() {
Daniel Park95f73312018-07-31 15:48:34 +090051 OpenstackNetworkService osNetService = AbstractShellCommand.get(OpenstackNetworkService.class);
52 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
53 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
54
55 Port port = osNetService.port(portId);
56 if (port == null) {
57 log.error("There's no port that matches the port ID {}", portId);
58 return;
59 }
60
61 Optional<OpenstackNode> osNode = osNodeService.completeNodes(COMPUTE).stream()
62 .filter(node -> node.hostname().equals(port.getHostId()))
63 .findAny();
64 if (!osNode.isPresent()) {
65 log.error("There's no openstackNode that matches hostname {}",
66 port.getHostId());
67 return;
68 }
69
Daniel Parkec9d1132018-08-19 11:18:03 +090070 String intfName = getIntfNameFromPciAddress(port);
Daniel Park95f73312018-07-31 15:48:34 +090071 if (intfName == null) {
72 log.error("Failed to retrieve interface name from a port {}", portId);
73 return;
Daniel Parkec9d1132018-08-19 11:18:03 +090074 } else if (intfName.equals(UNSUPPORTED_VENDOR)) {
Daniel Parkec9d1132018-08-19 11:18:03 +090075 return;
Daniel Park95f73312018-07-31 15:48:34 +090076 }
77
78 if (OpenstackNetworkingUtil.hasIntfAleadyInDevice(osNode.get().intgBridge(),
79 intfName, deviceService)) {
80 log.error("Interface {} is already added to the device {}", osNode.get().intgBridge());
81 return;
82 }
83
84 log.info("Adding interface {} to the device {}..", intfName,
85 osNode.get().intgBridge());
86
87 osNodeService.addVfPort(osNode.get(), intfName);
88 }
89}