Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.openstacknode.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.Device; |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.BridgeConfig; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 27 | import org.onosproject.net.device.PortDescription; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 28 | import org.onosproject.openstacknode.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.OpenstackNodeService; |
| 30 | |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 31 | import java.util.Optional; |
| 32 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 33 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 34 | import static org.onosproject.openstacknode.Constants.*; |
| 35 | import 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") |
| 42 | public 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 67 | print("[Integration Bridge Status]"); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 68 | 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 86 | print(getPortState(deviceService, node.intBridge(), PATCH_INTG_BRIDGE)); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 87 | |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 88 | 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 95 | } else { |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 96 | 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 110 | } |
| 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 131 | |
| 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 155 | } |