blob: f421de091ba6c9e201922ff895420c3365de49e1 [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;
Jian Li121ddfe2019-08-27 02:07:05 +090035import static org.onosproject.k8snode.api.Constants.INTEGRATION_TO_LOCAL_BRIDGE;
36import static org.onosproject.k8snode.api.Constants.LOCAL_BRIDGE;
37import static org.onosproject.k8snode.api.Constants.LOCAL_TO_INTEGRATION_BRIDGE;
Jian Li8a988042019-05-03 23:41:19 +090038import static org.onosproject.k8snode.api.Constants.PHYSICAL_EXTERNAL_BRIDGE;
Jian Lif16e8852019-01-22 22:55:31 +090039import static org.onosproject.k8snode.api.Constants.VXLAN_TUNNEL;
40import static org.onosproject.net.AnnotationKeys.PORT_NAME;
41
42/**
43 * Checks detailed node init state.
44 */
45@Service
46@Command(scope = "onos", name = "k8s-node-check",
47 description = "Shows detailed kubernetes node init state")
48public class K8sNodeCheckCommand extends AbstractShellCommand {
49
50 @Argument(index = 0, name = "hostname", description = "Hostname",
51 required = true, multiValued = false)
52 @Completion(K8sHostnameCompleter.class)
53 private String hostname = null;
54
55 private static final String MSG_OK = "OK";
56 private static final String MSG_ERROR = "ERROR";
57
58 @Override
59 protected void doExecute() {
60 K8sNodeService nodeService = get(K8sNodeService.class);
61 DeviceService deviceService = get(DeviceService.class);
62
63 K8sNode node = nodeService.node(hostname);
64 if (node == null) {
65 print("Cannot find %s from registered nodes", hostname);
66 return;
67 }
68
69 print("[Integration Bridge Status]");
Jian Li8a988042019-05-03 23:41:19 +090070 Device intgBridge = deviceService.getDevice(node.intgBridge());
71 if (intgBridge != null) {
Jian Lif16e8852019-01-22 22:55:31 +090072 print("%s %s=%s available=%s %s",
Jian Li8a988042019-05-03 23:41:19 +090073 deviceService.isAvailable(intgBridge.id()) ? MSG_OK : MSG_ERROR,
Jian Lif16e8852019-01-22 22:55:31 +090074 INTEGRATION_BRIDGE,
Jian Li8a988042019-05-03 23:41:19 +090075 intgBridge.id(),
76 deviceService.isAvailable(intgBridge.id()),
77 intgBridge.annotations());
78 printPortState(deviceService, node.intgBridge(), INTEGRATION_BRIDGE);
79 printPortState(deviceService, node.intgBridge(), INTEGRATION_TO_EXTERNAL_BRIDGE);
Jian Li121ddfe2019-08-27 02:07:05 +090080 printPortState(deviceService, node.intgBridge(), INTEGRATION_TO_LOCAL_BRIDGE);
Jian Lif16e8852019-01-22 22:55:31 +090081 if (node.dataIp() != null) {
82 printPortState(deviceService, node.intgBridge(), VXLAN_TUNNEL);
83 printPortState(deviceService, node.intgBridge(), GRE_TUNNEL);
84 printPortState(deviceService, node.intgBridge(), GENEVE_TUNNEL);
85 }
86 } else {
87 print("%s %s=%s is not available",
88 MSG_ERROR,
89 INTEGRATION_BRIDGE,
90 node.intgBridge());
91 }
Jian Li8a988042019-05-03 23:41:19 +090092
93 print("[External Bridge Status]");
94 Device extBridge = deviceService.getDevice(node.extBridge());
95 if (extBridge != null) {
96 print("%s %s=%s available=%s %s",
97 deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR,
98 EXTERNAL_BRIDGE,
99 extBridge.id(),
100 deviceService.isAvailable(extBridge.id()),
101 extBridge.annotations());
102 printPortState(deviceService, node.extBridge(), EXTERNAL_BRIDGE);
103 printPortState(deviceService, node.extBridge(), PHYSICAL_EXTERNAL_BRIDGE);
104 } else {
105 print("%s %s=%s is not available",
106 MSG_ERROR,
107 EXTERNAL_BRIDGE,
108 node.extBridge());
109 }
Jian Li121ddfe2019-08-27 02:07:05 +0900110
111 print("[Local Bridge Status]");
112 Device localBridge = deviceService.getDevice(node.localBridge());
113 if (localBridge != null) {
114 print("%s %s=%s available=%s %s",
115 deviceService.isAvailable(localBridge.id()) ? MSG_OK : MSG_ERROR,
116 LOCAL_BRIDGE,
117 localBridge.id(),
118 deviceService.isAvailable(localBridge.id()),
119 localBridge.annotations());
120 printPortState(deviceService, node.localBridge(), LOCAL_BRIDGE);
121 printPortState(deviceService, node.localBridge(), LOCAL_TO_INTEGRATION_BRIDGE);
122 }
Jian Lif16e8852019-01-22 22:55:31 +0900123 }
124
125 private void printPortState(DeviceService deviceService,
126 DeviceId deviceId, String portName) {
127 Port port = deviceService.getPorts(deviceId).stream()
128 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
129 p.isEnabled())
130 .findAny().orElse(null);
131
132 if (port != null) {
133 print("%s %s portNum=%s enabled=%s %s",
134 port.isEnabled() ? MSG_OK : MSG_ERROR,
135 portName,
136 port.number(),
137 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
138 port.annotations());
139 } else {
140 print("%s %s does not exist", MSG_ERROR, portName);
141 }
142 }
143}