blob: a35ab1b45108549b56a5fb7001f5aa708de15579 [file] [log] [blame]
Hyunsun Moon34bbe172016-06-28 19:18:40 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16
17package org.onosproject.openstacknode.cli;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Port;
24import org.onosproject.net.Device;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070025import org.onosproject.net.behaviour.BridgeConfig;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070026import org.onosproject.net.device.DeviceService;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070027import org.onosproject.net.device.PortDescription;
Hyunsun Moon0d457362017-06-27 17:19:41 +090028import org.onosproject.openstacknode.api.OpenstackNode;
29import org.onosproject.openstacknode.api.OpenstackNodeService;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070030
Hyunsun Moon052c71f2016-07-11 18:56:18 -070031import java.util.Optional;
32
Hyunsun Moon34bbe172016-06-28 19:18:40 -070033import static org.onosproject.net.AnnotationKeys.PORT_NAME;
Hyunsun Moon0d457362017-06-27 17:19:41 +090034import static org.onosproject.openstacknode.api.Constants.*;
35import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070036
37/**
38 * Checks detailed node init state.
39 */
40@Command(scope = "onos", name = "openstack-node-check",
41 description = "Shows detailed node init state")
42public class OpenstackNodeCheckCommand extends AbstractShellCommand {
43
44 @Argument(index = 0, name = "hostname", description = "Hostname",
45 required = true, multiValued = false)
46 private String hostname = null;
47
48 private static final String MSG_OK = "OK";
49 private static final String MSG_NO = "NO";
50
51 @Override
52 protected void execute() {
Hyunsun Moon0d457362017-06-27 17:19:41 +090053 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070054 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
55
Hyunsun Moon0d457362017-06-27 17:19:41 +090056 OpenstackNode osNode = osNodeService.node(hostname);
57 if (osNode == null) {
58 error("Cannot find %s from registered nodes", hostname);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070059 return;
60 }
61
Hyunsun Moon052c71f2016-07-11 18:56:18 -070062 print("[Integration Bridge Status]");
Hyunsun Moon0d457362017-06-27 17:19:41 +090063 Device device = deviceService.getDevice(osNode.intgBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070064 if (device != null) {
65 print("%s %s=%s available=%s %s",
66 deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO,
67 INTEGRATION_BRIDGE,
68 device.id(),
69 deviceService.isAvailable(device.id()),
70 device.annotations());
Hyunsun Moon0d457362017-06-27 17:19:41 +090071 if (osNode.dataIp() != null) {
72 print(getPortState(deviceService, osNode.intgBridge(), DEFAULT_TUNNEL));
73 }
74 if (osNode.vlanIntf() != null) {
75 print(getPortState(deviceService, osNode.intgBridge(), osNode.vlanIntf()));
76 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -070077 } else {
78 print("%s %s=%s is not available",
79 MSG_NO,
80 INTEGRATION_BRIDGE,
Hyunsun Moon0d457362017-06-27 17:19:41 +090081 osNode.intgBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070082 }
83
Hyunsun Moon0d457362017-06-27 17:19:41 +090084 if (osNode.type() == GATEWAY) {
85 print(getPortState(deviceService, osNode.intgBridge(), PATCH_INTG_BRIDGE));
Hyunsun Moon34bbe172016-06-28 19:18:40 -070086
Hyunsun Moon052c71f2016-07-11 18:56:18 -070087 print("%n[Router Bridge Status]");
Hyunsun Moon0d457362017-06-27 17:19:41 +090088 device = deviceService.getDevice(osNode.ovsdb());
Hyunsun Moon052c71f2016-07-11 18:56:18 -070089 if (device == null || !device.is(BridgeConfig.class)) {
90 print("%s %s=%s is not available(unable to connect OVSDB)",
91 MSG_NO,
92 ROUTER_BRIDGE,
Hyunsun Moon0d457362017-06-27 17:19:41 +090093 osNode.intgBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070094 } else {
Hyunsun Moon052c71f2016-07-11 18:56:18 -070095 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
96 boolean available = bridgeConfig.getBridges().stream()
Hyunsun Moon0d457362017-06-27 17:19:41 +090097 .anyMatch(bridge -> bridge.name().equals(ROUTER_BRIDGE));
Hyunsun Moon052c71f2016-07-11 18:56:18 -070098 print("%s %s=%s available=%s",
99 available ? MSG_OK : MSG_NO,
100 ROUTER_BRIDGE,
Hyunsun Moon0d457362017-06-27 17:19:41 +0900101 osNode.routerBridge(),
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700102 available);
Hyunsun Moon0d457362017-06-27 17:19:41 +0900103 print(getPortStateOvsdb(deviceService, osNode.ovsdb(), PATCH_ROUT_BRIDGE));
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700104 }
105 }
106 }
107
108 private String getPortState(DeviceService deviceService, DeviceId deviceId, String portName) {
109 Port port = deviceService.getPorts(deviceId).stream()
110 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
111 p.isEnabled())
112 .findAny().orElse(null);
113
114 if (port != null) {
115 return String.format("%s %s portNum=%s enabled=%s %s",
116 port.isEnabled() ? MSG_OK : MSG_NO,
117 portName,
118 port.number(),
119 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
120 port.annotations());
121 } else {
122 return String.format("%s %s does not exist", MSG_NO, portName);
123 }
124 }
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700125
126 private String getPortStateOvsdb(DeviceService deviceService, DeviceId deviceId, String portName) {
127 Device device = deviceService.getDevice(deviceId);
128 if (device == null || !device.is(BridgeConfig.class)) {
129 return String.format("%s %s does not exist(unable to connect OVSDB)",
130 MSG_NO, portName);
131 }
132
133 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
134 Optional<PortDescription> port = bridgeConfig.getPorts().stream()
135 .filter(p -> p.annotations().value(PORT_NAME).contains(portName))
136 .findAny();
137
138 if (port.isPresent()) {
139 return String.format("%s %s portNum=%s enabled=%s %s",
140 port.get().isEnabled() ? MSG_OK : MSG_NO,
141 portName,
142 port.get().portNumber(),
143 port.get().isEnabled() ? Boolean.TRUE : Boolean.FALSE,
144 port.get().annotations());
145 } else {
146 return String.format("%s %s does not exist", MSG_NO, portName);
147 }
148 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700149}