blob: 69c64b918a7eacf5fac789a8da2d818b97215030 [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 Li8a988042019-05-03 23:41:19 +090030import static org.onosproject.k8snode.api.Constants.EXTERNAL_BRIDGE;
Jian Lif16e8852019-01-22 22:55:31 +090031import static org.onosproject.k8snode.api.Constants.GENEVE_TUNNEL;
32import static org.onosproject.k8snode.api.Constants.GRE_TUNNEL;
33import static org.onosproject.k8snode.api.Constants.INTEGRATION_BRIDGE;
Jian Li8a988042019-05-03 23:41:19 +090034import static org.onosproject.k8snode.api.Constants.INTEGRATION_TO_EXTERNAL_BRIDGE;
35import static org.onosproject.k8snode.api.Constants.PHYSICAL_EXTERNAL_BRIDGE;
Jian Lif16e8852019-01-22 22:55:31 +090036import static org.onosproject.k8snode.api.Constants.VXLAN_TUNNEL;
37import static org.onosproject.net.AnnotationKeys.PORT_NAME;
38
39/**
40 * Checks detailed node init state.
41 */
42@Service
43@Command(scope = "onos", name = "k8s-node-check",
44 description = "Shows detailed kubernetes node init state")
45public class K8sNodeCheckCommand extends AbstractShellCommand {
46
47 @Argument(index = 0, name = "hostname", description = "Hostname",
48 required = true, multiValued = false)
49 @Completion(K8sHostnameCompleter.class)
50 private String hostname = null;
51
52 private static final String MSG_OK = "OK";
53 private static final String MSG_ERROR = "ERROR";
54
55 @Override
56 protected void doExecute() {
57 K8sNodeService nodeService = get(K8sNodeService.class);
58 DeviceService deviceService = get(DeviceService.class);
59
60 K8sNode node = nodeService.node(hostname);
61 if (node == null) {
62 print("Cannot find %s from registered nodes", hostname);
63 return;
64 }
65
66 print("[Integration Bridge Status]");
Jian Li8a988042019-05-03 23:41:19 +090067 Device intgBridge = deviceService.getDevice(node.intgBridge());
68 if (intgBridge != null) {
Jian Lif16e8852019-01-22 22:55:31 +090069 print("%s %s=%s available=%s %s",
Jian Li8a988042019-05-03 23:41:19 +090070 deviceService.isAvailable(intgBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lif16e8852019-01-22 22:55:31 +090071 INTEGRATION_BRIDGE,
Jian Li8a988042019-05-03 23:41:19 +090072 intgBridge.id(),
73 deviceService.isAvailable(intgBridge.id()),
74 intgBridge.annotations());
75 printPortState(deviceService, node.intgBridge(), INTEGRATION_BRIDGE);
76 printPortState(deviceService, node.intgBridge(), INTEGRATION_TO_EXTERNAL_BRIDGE);
Jian Lif16e8852019-01-22 22:55:31 +090077 if (node.dataIp() != null) {
78 printPortState(deviceService, node.intgBridge(), VXLAN_TUNNEL);
79 printPortState(deviceService, node.intgBridge(), GRE_TUNNEL);
80 printPortState(deviceService, node.intgBridge(), GENEVE_TUNNEL);
81 }
82 } else {
83 print("%s %s=%s is not available",
84 MSG_ERROR,
85 INTEGRATION_BRIDGE,
86 node.intgBridge());
87 }
Jian Li8a988042019-05-03 23:41:19 +090088
89 print("[External Bridge Status]");
90 Device extBridge = deviceService.getDevice(node.extBridge());
91 if (extBridge != null) {
92 print("%s %s=%s available=%s %s",
93 deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR,
94 EXTERNAL_BRIDGE,
95 extBridge.id(),
96 deviceService.isAvailable(extBridge.id()),
97 extBridge.annotations());
98 printPortState(deviceService, node.extBridge(), EXTERNAL_BRIDGE);
99 printPortState(deviceService, node.extBridge(), PHYSICAL_EXTERNAL_BRIDGE);
100 } else {
101 print("%s %s=%s is not available",
102 MSG_ERROR,
103 EXTERNAL_BRIDGE,
104 node.extBridge());
105 }
Jian Lif16e8852019-01-22 22:55:31 +0900106 }
107
108 private void printPortState(DeviceService deviceService,
109 DeviceId deviceId, String portName) {
110 Port port = deviceService.getPorts(deviceId).stream()
111 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
112 p.isEnabled())
113 .findAny().orElse(null);
114
115 if (port != null) {
116 print("%s %s portNum=%s enabled=%s %s",
117 port.isEnabled() ? MSG_OK : MSG_ERROR,
118 portName,
119 port.number(),
120 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
121 port.annotations());
122 } else {
123 print("%s %s does not exist", MSG_ERROR, portName);
124 }
125 }
126}