blob: 06297c2f679383b7eae0a0f93908e0eeeade0660 [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;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.device.DeviceService;
29
30import static org.onosproject.kubevirtnode.api.Constants.GENEVE;
31import static org.onosproject.kubevirtnode.api.Constants.GRE;
32import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_BRIDGE;
33import static org.onosproject.kubevirtnode.api.Constants.TUNNEL_BRIDGE;
34import static org.onosproject.kubevirtnode.api.Constants.VXLAN;
35import static org.onosproject.net.AnnotationKeys.PORT_NAME;
36
37/**
38 * Checks detailed node init state.
39 */
40@Service
41@Command(scope = "onos", name = "kubevirt-check-node",
42 description = "Shows detailed kubevirt nodes status")
43public class KubevirtCheckNodeCommand extends AbstractShellCommand {
44
45 @Argument(index = 0, name = "hostname", description = "Hostname",
46 required = true, multiValued = false)
47 @Completion(KubevirtHostnameCompleter.class)
48 private String hostname = null;
49
50 private static final String MSG_PASS = "PASS";
51 private static final String MSG_FAIL = "FAIL";
52
53 @Override
54 protected void doExecute() throws Exception {
55 KubevirtNodeService nodeService = get(KubevirtNodeService.class);
56 DeviceService deviceService = get(DeviceService.class);
57
58 KubevirtNode node = nodeService.node(hostname);
59 if (node == null) {
60 print("Cannot find %s from registered nodes", hostname);
61 return;
62 }
63
64 print("[Integration Bridge Status]");
65 Device intgBridge = deviceService.getDevice(node.intgBridge());
66 if (intgBridge != null) {
67 print("%s %s=%s available=%s %s",
68 deviceService.isAvailable(intgBridge.id()) ? MSG_PASS : MSG_FAIL,
69 INTEGRATION_BRIDGE,
70 intgBridge.id(),
71 deviceService.isAvailable(intgBridge.id()),
72 intgBridge.annotations());
73 }
74
75 print("");
76 print("[Tunnel Bridge Status]");
77 Device tunBridge = deviceService.getDevice(node.tunBridge());
78 if (tunBridge != null) {
79 print("%s %s=%s available=%s %s",
80 deviceService.isAvailable(tunBridge.id()) ? MSG_PASS : MSG_FAIL,
81 TUNNEL_BRIDGE,
82 tunBridge.id(),
83 deviceService.isAvailable(tunBridge.id()),
84 tunBridge.annotations());
85
86 if (node.dataIp() != null) {
87 printPortState(deviceService, node.tunBridge(), VXLAN);
88 printPortState(deviceService, node.tunBridge(), GRE);
89 printPortState(deviceService, node.tunBridge(), GENEVE);
90 }
91 }
92 }
93
94 private void printPortState(DeviceService deviceService,
95 DeviceId deviceId, String portName) {
96 Port port = deviceService.getPorts(deviceId).stream()
97 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
98 p.isEnabled())
99 .findAny().orElse(null);
100
101 if (port != null) {
102 print("%s %s portNum=%s enabled=%s %s",
103 port.isEnabled() ? MSG_PASS : MSG_FAIL,
104 portName,
105 port.number(),
106 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
107 port.annotations());
108 } else {
109 print("%s %s does not exist", MSG_FAIL, portName);
110 }
111 }
112}