blob: 20607127781aec13c2d30f2748ee50ef07e06d35 [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 if (node.dataIp() != null) {
Jian Lie2a04ce2020-07-01 19:07:02 +090072 printPortState(deviceService, node.intgBridge(), node.vxlanPortName());
73 printPortState(deviceService, node.intgBridge(), node.grePortName());
74 printPortState(deviceService, node.intgBridge(), node.genevePortName());
Jian Lif16e8852019-01-22 22:55:31 +090075 }
76 } else {
77 print("%s %s=%s is not available",
78 MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090079 node.intgBridgeName(),
Jian Lif16e8852019-01-22 22:55:31 +090080 node.intgBridge());
81 }
Jian Li8a988042019-05-03 23:41:19 +090082
83 print("[External Bridge Status]");
84 Device extBridge = deviceService.getDevice(node.extBridge());
85 if (extBridge != null) {
86 print("%s %s=%s available=%s %s",
87 deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090088 node.extBridgeName(),
Jian Li8a988042019-05-03 23:41:19 +090089 extBridge.id(),
90 deviceService.isAvailable(extBridge.id()),
91 extBridge.annotations());
Jian Lie2a04ce2020-07-01 19:07:02 +090092 printPortState(deviceService, node.extBridge(), node.extBridgePortName());
93 printPortState(deviceService, node.extBridge(), node.extToIntgPatchPortName());
Jian Li8a988042019-05-03 23:41:19 +090094 } else {
95 print("%s %s=%s is not available",
96 MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +090097 node.extBridgeName(),
Jian Li8a988042019-05-03 23:41:19 +090098 node.extBridge());
99 }
Jian Li1a2eb5d2019-08-27 02:07:05 +0900100
101 print("[Local Bridge Status]");
102 Device localBridge = deviceService.getDevice(node.localBridge());
103 if (localBridge != null) {
104 print("%s %s=%s available=%s %s",
105 deviceService.isAvailable(localBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lie2a04ce2020-07-01 19:07:02 +0900106 node.localBridgeName(),
Jian Li1a2eb5d2019-08-27 02:07:05 +0900107 localBridge.id(),
108 deviceService.isAvailable(localBridge.id()),
109 localBridge.annotations());
Jian Lie2a04ce2020-07-01 19:07:02 +0900110 printPortState(deviceService, node.localBridge(), node.localBridgePortName());
111 printPortState(deviceService, node.localBridge(), node.localToIntgPatchPortName());
Jian Li1a2eb5d2019-08-27 02:07:05 +0900112 }
Jian Lif16e8852019-01-22 22:55:31 +0900113 }
114
115 private void printPortState(DeviceService deviceService,
116 DeviceId deviceId, String portName) {
117 Port port = deviceService.getPorts(deviceId).stream()
118 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
119 p.isEnabled())
120 .findAny().orElse(null);
121
122 if (port != null) {
123 print("%s %s portNum=%s enabled=%s %s",
124 port.isEnabled() ? MSG_OK : MSG_ERROR,
125 portName,
126 port.number(),
127 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
128 port.annotations());
129 } else {
130 print("%s %s does not exist", MSG_ERROR, portName);
131 }
132 }
133}