blob: 975da59081d96c620826dd282819afd81592621a [file] [log] [blame]
Jian Li4fe40e52021-01-06 03:29:58 +09001/*
2 * Copyright 2021-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.kubevirtnode.cli;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.kubevirtnode.api.KubevirtNode;
24import org.onosproject.kubevirtnode.api.KubevirtNodeService;
Jian Li8ffb2c02022-10-19 18:01:48 +090025import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
Jian Li4fe40e52021-01-06 03:29:58 +090026import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Port;
29import org.onosproject.net.device.DeviceService;
30
31import static org.onosproject.kubevirtnode.api.Constants.GENEVE;
32import static org.onosproject.kubevirtnode.api.Constants.GRE;
33import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_BRIDGE;
Jian Li4b3436a2022-03-23 13:07:19 +090034import static org.onosproject.kubevirtnode.api.Constants.STT;
Jian Li4fe40e52021-01-06 03:29:58 +090035import static org.onosproject.kubevirtnode.api.Constants.TUNNEL_BRIDGE;
36import static org.onosproject.kubevirtnode.api.Constants.VXLAN;
37import static org.onosproject.net.AnnotationKeys.PORT_NAME;
38
39/**
40 * Checks detailed node init state.
41 */
42@Service
43@Command(scope = "onos", name = "kubevirt-check-node",
44 description = "Shows detailed kubevirt nodes status")
45public class KubevirtCheckNodeCommand extends AbstractShellCommand {
46
47 @Argument(index = 0, name = "hostname", description = "Hostname",
48 required = true, multiValued = false)
49 @Completion(KubevirtHostnameCompleter.class)
50 private String hostname = null;
51
52 private static final String MSG_PASS = "PASS";
53 private static final String MSG_FAIL = "FAIL";
54
Jian Li8ffb2c02022-10-19 18:01:48 +090055 private static final String BRIDGE_PREFIX = "br-";
56
Jian Li4fe40e52021-01-06 03:29:58 +090057 @Override
58 protected void doExecute() throws Exception {
59 KubevirtNodeService nodeService = get(KubevirtNodeService.class);
60 DeviceService deviceService = get(DeviceService.class);
61
62 KubevirtNode node = nodeService.node(hostname);
63 if (node == null) {
64 print("Cannot find %s from registered nodes", hostname);
65 return;
66 }
67
68 print("[Integration Bridge Status]");
69 Device intgBridge = deviceService.getDevice(node.intgBridge());
70 if (intgBridge != null) {
71 print("%s %s=%s available=%s %s",
72 deviceService.isAvailable(intgBridge.id()) ? MSG_PASS : MSG_FAIL,
73 INTEGRATION_BRIDGE,
74 intgBridge.id(),
75 deviceService.isAvailable(intgBridge.id()),
76 intgBridge.annotations());
77 }
78
79 print("");
80 print("[Tunnel Bridge Status]");
81 Device tunBridge = deviceService.getDevice(node.tunBridge());
82 if (tunBridge != null) {
83 print("%s %s=%s available=%s %s",
84 deviceService.isAvailable(tunBridge.id()) ? MSG_PASS : MSG_FAIL,
85 TUNNEL_BRIDGE,
86 tunBridge.id(),
87 deviceService.isAvailable(tunBridge.id()),
88 tunBridge.annotations());
89
90 if (node.dataIp() != null) {
91 printPortState(deviceService, node.tunBridge(), VXLAN);
92 printPortState(deviceService, node.tunBridge(), GRE);
93 printPortState(deviceService, node.tunBridge(), GENEVE);
Jian Li4b3436a2022-03-23 13:07:19 +090094 printPortState(deviceService, node.tunBridge(), STT);
Jian Li4fe40e52021-01-06 03:29:58 +090095 }
96 }
Jian Li8ffb2c02022-10-19 18:01:48 +090097
98 if (node.phyIntfs().size() > 0) {
99 print("");
100 print("[Physical Network Bridge Status]");
101 for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
102 Device physBridge = deviceService.getDevice(phyIntf.physBridge());
103 if (physBridge != null) {
104 print("%s %s=%s available=%s %s",
105 deviceService.isAvailable(physBridge.id()) ? MSG_PASS : MSG_FAIL,
106 BRIDGE_PREFIX + phyIntf.network(),
107 physBridge.id(),
108 deviceService.isAvailable(physBridge.id()),
109 physBridge.annotations());
110 printPortState(deviceService, physBridge.id(), phyIntf.intf());
111 }
112 }
113 }
Jian Li4fe40e52021-01-06 03:29:58 +0900114 }
115
116 private void printPortState(DeviceService deviceService,
117 DeviceId deviceId, String portName) {
118 Port port = deviceService.getPorts(deviceId).stream()
119 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
120 p.isEnabled())
121 .findAny().orElse(null);
122
123 if (port != null) {
124 print("%s %s portNum=%s enabled=%s %s",
125 port.isEnabled() ? MSG_PASS : MSG_FAIL,
126 portName,
127 port.number(),
128 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
129 port.annotations());
130 } else {
131 print("%s %s does not exist", MSG_FAIL, portName);
132 }
133 }
134}