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 | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 28 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 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; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 34 | import static org.onosproject.openstacknode.api.Constants.*; |
| 35 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 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() { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 53 | OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 54 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 55 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 56 | OpenstackNode osNode = osNodeService.node(hostname); |
| 57 | if (osNode == null) { |
| 58 | error("Cannot find %s from registered nodes", hostname); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 62 | print("[Integration Bridge Status]"); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 63 | Device device = deviceService.getDevice(osNode.intgBridge()); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 64 | 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 Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 71 | 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 77 | } else { |
| 78 | print("%s %s=%s is not available", |
| 79 | MSG_NO, |
| 80 | INTEGRATION_BRIDGE, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 81 | osNode.intgBridge()); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 84 | if (osNode.type() == GATEWAY) { |
| 85 | print(getPortState(deviceService, osNode.intgBridge(), PATCH_INTG_BRIDGE)); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 86 | |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 87 | print("%n[Router Bridge Status]"); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 88 | device = deviceService.getDevice(osNode.ovsdb()); |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 89 | 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 Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 93 | osNode.intgBridge()); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 94 | } else { |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 95 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 96 | boolean available = bridgeConfig.getBridges().stream() |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 97 | .anyMatch(bridge -> bridge.name().equals(ROUTER_BRIDGE)); |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 98 | print("%s %s=%s available=%s", |
| 99 | available ? MSG_OK : MSG_NO, |
| 100 | ROUTER_BRIDGE, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 101 | osNode.routerBridge(), |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 102 | available); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame^] | 103 | print(getPortStateOvsdb(deviceService, osNode.ovsdb(), PATCH_ROUT_BRIDGE)); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 104 | } |
| 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 125 | |
| 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 149 | } |