blob: 85da7320b8f769c55b662e808f3b5b6d433cf752 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Park95f73312018-07-31 15:48:34 +090021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
24import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil;
25import org.onosproject.openstacknode.api.OpenstackNode;
26import org.onosproject.openstacknode.api.OpenstackNodeService;
27import org.openstack4j.model.network.Port;
28
29import java.util.Optional;
30
Daniel Parkec9d1132018-08-19 11:18:03 +090031import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR;
32import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getIntfNameFromPciAddress;
Daniel Park95f73312018-07-31 15:48:34 +090033import 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 Milkey7a2dee52018-09-28 10:58:28 -070040@Service
Daniel Park95f73312018-07-31 15:48:34 +090041@Command(scope = "onos", name = "openstack-direct-port-add",
42 description = "Manually adds OpenStack direct ports to the device")
43public 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 Milkey86ad7bb2018-09-27 12:32:28 -070048 protected void doExecute() {
Daniel Park95f73312018-07-31 15:48:34 +090049 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 Parkec9d1132018-08-19 11:18:03 +090068 String intfName = getIntfNameFromPciAddress(port);
Daniel Park95f73312018-07-31 15:48:34 +090069 if (intfName == null) {
70 log.error("Failed to retrieve interface name from a port {}", portId);
71 return;
Daniel Parkec9d1132018-08-19 11:18:03 +090072 } 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 Park95f73312018-07-31 15:48:34 +090075 }
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}