blob: 84803c14affb2c4474ce819ec73fd7c2273dc274 [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 Moon34bbe172016-06-28 19:18:40 -070028import org.onosproject.openstacknode.OpenstackNode;
29import org.onosproject.openstacknode.OpenstackNodeService;
30
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;
34import static org.onosproject.openstacknode.Constants.*;
35import static org.onosproject.openstacknode.OpenstackNodeService.NodeType.GATEWAY;
36
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() {
53 OpenstackNodeService nodeService = AbstractShellCommand.get(OpenstackNodeService.class);
54 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
55
56 OpenstackNode node = nodeService.nodes()
57 .stream()
58 .filter(n -> n.hostname().equals(hostname))
59 .findFirst()
60 .orElse(null);
61
62 if (node == null) {
63 print("Cannot find %s from registered nodes", hostname);
64 return;
65 }
66
Hyunsun Moon052c71f2016-07-11 18:56:18 -070067 print("[Integration Bridge Status]");
Hyunsun Moon34bbe172016-06-28 19:18:40 -070068 Device device = deviceService.getDevice(node.intBridge());
69 if (device != null) {
70 print("%s %s=%s available=%s %s",
71 deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO,
72 INTEGRATION_BRIDGE,
73 device.id(),
74 deviceService.isAvailable(device.id()),
75 device.annotations());
76
77 print(getPortState(deviceService, node.intBridge(), DEFAULT_TUNNEL));
78 } else {
79 print("%s %s=%s is not available",
80 MSG_NO,
81 INTEGRATION_BRIDGE,
82 node.intBridge());
83 }
84
85 if (node.type().equals(GATEWAY)) {
Hyunsun Moon052c71f2016-07-11 18:56:18 -070086 print(getPortState(deviceService, node.intBridge(), PATCH_INTG_BRIDGE));
Hyunsun Moon34bbe172016-06-28 19:18:40 -070087
Hyunsun Moon052c71f2016-07-11 18:56:18 -070088 print("%n[Router Bridge Status]");
89 device = deviceService.getDevice(node.ovsdbId());
90 if (device == null || !device.is(BridgeConfig.class)) {
91 print("%s %s=%s is not available(unable to connect OVSDB)",
92 MSG_NO,
93 ROUTER_BRIDGE,
94 node.intBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070095 } else {
Hyunsun Moon052c71f2016-07-11 18:56:18 -070096 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
97 boolean available = bridgeConfig.getBridges().stream()
98 .filter(bridge -> bridge.name().equals(ROUTER_BRIDGE))
99 .findAny()
100 .isPresent();
101
102 print("%s %s=%s available=%s",
103 available ? MSG_OK : MSG_NO,
104 ROUTER_BRIDGE,
105 node.routerBridge().get(),
106 available);
107
108 print(getPortStateOvsdb(deviceService, node.ovsdbId(), PATCH_ROUT_BRIDGE));
109 print(getPortStateOvsdb(deviceService, node.ovsdbId(), node.uplink().get()));
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700110 }
111 }
112 }
113
114 private String getPortState(DeviceService deviceService, DeviceId deviceId, String portName) {
115 Port port = deviceService.getPorts(deviceId).stream()
116 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
117 p.isEnabled())
118 .findAny().orElse(null);
119
120 if (port != null) {
121 return String.format("%s %s portNum=%s enabled=%s %s",
122 port.isEnabled() ? MSG_OK : MSG_NO,
123 portName,
124 port.number(),
125 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
126 port.annotations());
127 } else {
128 return String.format("%s %s does not exist", MSG_NO, portName);
129 }
130 }
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700131
132 private String getPortStateOvsdb(DeviceService deviceService, DeviceId deviceId, String portName) {
133 Device device = deviceService.getDevice(deviceId);
134 if (device == null || !device.is(BridgeConfig.class)) {
135 return String.format("%s %s does not exist(unable to connect OVSDB)",
136 MSG_NO, portName);
137 }
138
139 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
140 Optional<PortDescription> port = bridgeConfig.getPorts().stream()
141 .filter(p -> p.annotations().value(PORT_NAME).contains(portName))
142 .findAny();
143
144 if (port.isPresent()) {
145 return String.format("%s %s portNum=%s enabled=%s %s",
146 port.get().isEnabled() ? MSG_OK : MSG_NO,
147 portName,
148 port.get().portNumber(),
149 port.get().isEnabled() ? Boolean.TRUE : Boolean.FALSE,
150 port.get().annotations());
151 } else {
152 return String.format("%s %s does not exist", MSG_NO, portName);
153 }
154 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700155}