blob: de8f38cae273a71755fd3a2241abfe2f2ddae3d4 [file] [log] [blame]
Jian Lif16e8852019-01-22 22:55:31 +09001/*
2 * Copyright 2019-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.k8snode.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.k8snode.api.K8sNode;
24import org.onosproject.k8snode.api.K8sNodeService;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.device.DeviceService;
29
Jian Lif16e8852019-01-22 22:55:31 +090030import static org.onosproject.net.AnnotationKeys.PORT_NAME;
31
32/**
33 * Checks detailed node init state.
34 */
35@Service
36@Command(scope = "onos", name = "k8s-node-check",
37 description = "Shows detailed kubernetes node init state")
38public class K8sNodeCheckCommand extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "hostname", description = "Hostname",
41 required = true, multiValued = false)
42 @Completion(K8sHostnameCompleter.class)
43 private String hostname = null;
44
45 private static final String MSG_OK = "OK";
46 private static final String MSG_ERROR = "ERROR";
47
48 @Override
49 protected void doExecute() {
50 K8sNodeService nodeService = get(K8sNodeService.class);
51 DeviceService deviceService = get(DeviceService.class);
52
53 K8sNode node = nodeService.node(hostname);
54 if (node == null) {
55 print("Cannot find %s from registered nodes", hostname);
56 return;
57 }
58
59 print("[Integration Bridge Status]");
Jian Li8a988042019-05-03 23:41:19 +090060 Device intgBridge = deviceService.getDevice(node.intgBridge());
61 if (intgBridge != null) {
Jian Lif16e8852019-01-22 22:55:31 +090062 print("%s %s=%s available=%s %s",
Jian Li8a988042019-05-03 23:41:19 +090063 deviceService.isAvailable(intgBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090064 node.intgBridgeName(),
Jian Li8a988042019-05-03 23:41:19 +090065 intgBridge.id(),
66 deviceService.isAvailable(intgBridge.id()),
67 intgBridge.annotations());
Jian Lie2a04ce2020-07-01 19:07:02 +090068 printPortState(deviceService, node.intgBridge(), node.intgBridgePortName());
69 printPortState(deviceService, node.intgBridge(), node.intgToExtPatchPortName());
70 printPortState(deviceService, node.intgBridge(), node.intgToLocalPatchPortName());
Jian Lif16e8852019-01-22 22:55:31 +090071 } else {
72 print("%s %s=%s is not available",
73 MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090074 node.intgBridgeName(),
Jian Lif16e8852019-01-22 22:55:31 +090075 node.intgBridge());
76 }
Jian Li8a988042019-05-03 23:41:19 +090077
Jian Li619fa282020-09-02 14:45:35 +090078 print("");
Jian Li8a988042019-05-03 23:41:19 +090079 print("[External Bridge Status]");
80 Device extBridge = deviceService.getDevice(node.extBridge());
81 if (extBridge != null) {
82 print("%s %s=%s available=%s %s",
83 deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090084 node.extBridgeName(),
Jian Li8a988042019-05-03 23:41:19 +090085 extBridge.id(),
86 deviceService.isAvailable(extBridge.id()),
87 extBridge.annotations());
Jian Lie2a04ce2020-07-01 19:07:02 +090088 printPortState(deviceService, node.extBridge(), node.extToIntgPatchPortName());
Jian Li8a988042019-05-03 23:41:19 +090089 } else {
90 print("%s %s=%s is not available",
91 MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090092 node.extBridgeName(),
Jian Li8a988042019-05-03 23:41:19 +090093 node.extBridge());
94 }
Jian Li1a2eb5d2019-08-27 02:07:05 +090095
Jian Li619fa282020-09-02 14:45:35 +090096 print("");
Jian Li1a2eb5d2019-08-27 02:07:05 +090097 print("[Local Bridge Status]");
98 Device localBridge = deviceService.getDevice(node.localBridge());
99 if (localBridge != null) {
100 print("%s %s=%s available=%s %s",
101 deviceService.isAvailable(localBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +0900102 node.localBridgeName(),
Jian Li1a2eb5d2019-08-27 02:07:05 +0900103 localBridge.id(),
104 deviceService.isAvailable(localBridge.id()),
105 localBridge.annotations());
Jian Lie2a04ce2020-07-01 19:07:02 +0900106 printPortState(deviceService, node.localBridge(), node.localToIntgPatchPortName());
Jian Li1a2eb5d2019-08-27 02:07:05 +0900107 }
Jian Li619fa282020-09-02 14:45:35 +0900108
109 print("");
110 print("[Tunnel Bridge Status]");
111 Device tunBridge = deviceService.getDevice(node.tunBridge());
112 if (tunBridge != null) {
113 print("%s %s=%s available=%s %s",
114 deviceService.isAvailable(tunBridge.id()) ? MSG_OK : MSG_ERROR,
115 node.tunBridgeName(),
116 tunBridge.id(),
117 deviceService.isAvailable(tunBridge.id()),
118 tunBridge.annotations());
119 printPortState(deviceService, node.tunBridge(), node.tunToIntgPatchPortName());
120
121 if (node.dataIp() != null) {
122 printPortState(deviceService, node.tunBridge(), node.vxlanPortName());
123 printPortState(deviceService, node.tunBridge(), node.grePortName());
124 printPortState(deviceService, node.tunBridge(), node.genevePortName());
125 }
126 }
Jian Lif16e8852019-01-22 22:55:31 +0900127 }
128
129 private void printPortState(DeviceService deviceService,
130 DeviceId deviceId, String portName) {
131 Port port = deviceService.getPorts(deviceId).stream()
132 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
133 p.isEnabled())
134 .findAny().orElse(null);
135
136 if (port != null) {
137 print("%s %s portNum=%s enabled=%s %s",
138 port.isEnabled() ? MSG_OK : MSG_ERROR,
139 portName,
140 port.number(),
141 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
142 port.annotations());
143 } else {
144 print("%s %s does not exist", MSG_ERROR, portName);
145 }
146 }
147}