blob: 4d54abd8d8eee6bd30ebe7a19851269c675a3038 [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
daniel park917beb42017-03-16 18:07:15 +090077 node.dataIp().ifPresent(ip -> print(getPortState(deviceService, node.intBridge(), DEFAULT_TUNNEL)));
78 node.vlanPort().ifPresent(p -> print(getPortState(deviceService, node.intBridge(), p)));
Hyunsun Moon34bbe172016-06-28 19:18:40 -070079 } else {
80 print("%s %s=%s is not available",
81 MSG_NO,
82 INTEGRATION_BRIDGE,
83 node.intBridge());
84 }
85
86 if (node.type().equals(GATEWAY)) {
Hyunsun Moon052c71f2016-07-11 18:56:18 -070087 print(getPortState(deviceService, node.intBridge(), PATCH_INTG_BRIDGE));
Hyunsun Moon34bbe172016-06-28 19:18:40 -070088
Hyunsun Moon052c71f2016-07-11 18:56:18 -070089 print("%n[Router Bridge Status]");
90 device = deviceService.getDevice(node.ovsdbId());
91 if (device == null || !device.is(BridgeConfig.class)) {
92 print("%s %s=%s is not available(unable to connect OVSDB)",
93 MSG_NO,
94 ROUTER_BRIDGE,
95 node.intBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070096 } else {
Hyunsun Moon052c71f2016-07-11 18:56:18 -070097 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
98 boolean available = bridgeConfig.getBridges().stream()
99 .filter(bridge -> bridge.name().equals(ROUTER_BRIDGE))
100 .findAny()
101 .isPresent();
102
103 print("%s %s=%s available=%s",
104 available ? MSG_OK : MSG_NO,
105 ROUTER_BRIDGE,
106 node.routerBridge().get(),
107 available);
108
109 print(getPortStateOvsdb(deviceService, node.ovsdbId(), PATCH_ROUT_BRIDGE));
110 print(getPortStateOvsdb(deviceService, node.ovsdbId(), node.uplink().get()));
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700111 }
112 }
113 }
114
115 private String getPortState(DeviceService deviceService, DeviceId deviceId, String portName) {
116 Port port = deviceService.getPorts(deviceId).stream()
117 .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
118 p.isEnabled())
119 .findAny().orElse(null);
120
121 if (port != null) {
122 return String.format("%s %s portNum=%s enabled=%s %s",
123 port.isEnabled() ? MSG_OK : MSG_NO,
124 portName,
125 port.number(),
126 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
127 port.annotations());
128 } else {
129 return String.format("%s %s does not exist", MSG_NO, portName);
130 }
131 }
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700132
133 private String getPortStateOvsdb(DeviceService deviceService, DeviceId deviceId, String portName) {
134 Device device = deviceService.getDevice(deviceId);
135 if (device == null || !device.is(BridgeConfig.class)) {
136 return String.format("%s %s does not exist(unable to connect OVSDB)",
137 MSG_NO, portName);
138 }
139
140 BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
141 Optional<PortDescription> port = bridgeConfig.getPorts().stream()
142 .filter(p -> p.annotations().value(PORT_NAME).contains(portName))
143 .findAny();
144
145 if (port.isPresent()) {
146 return String.format("%s %s portNum=%s enabled=%s %s",
147 port.get().isEnabled() ? MSG_OK : MSG_NO,
148 portName,
149 port.get().portNumber(),
150 port.get().isEnabled() ? Boolean.TRUE : Boolean.FALSE,
151 port.get().annotations());
152 } else {
153 return String.format("%s %s does not exist", MSG_NO, portName);
154 }
155 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700156}