Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 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; |
| 25 | import org.onosproject.net.device.DeviceService; |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacknode.api.NodeState; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 28 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 29 | import org.openstack4j.api.OSClient; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 30 | |
| 31 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Jian Li | 5afbea4 | 2018-02-28 10:37:03 +0900 | [diff] [blame] | 32 | import static org.onosproject.openstacknode.api.Constants.DEFAULT_TUNNEL; |
| 33 | import static org.onosproject.openstacknode.api.Constants.INTEGRATION_BRIDGE; |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 34 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
| 35 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
| 36 | import static org.onosproject.openstacknode.util.OpenstackNodeUtil.getConnectedClient; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Checks detailed node init state. |
| 40 | */ |
| 41 | @Command(scope = "onos", name = "openstack-node-check", |
| 42 | description = "Shows detailed node init state") |
| 43 | public class OpenstackNodeCheckCommand extends AbstractShellCommand { |
| 44 | |
| 45 | @Argument(index = 0, name = "hostname", description = "Hostname", |
| 46 | required = true, multiValued = false) |
| 47 | private String hostname = null; |
| 48 | |
| 49 | private static final String MSG_OK = "OK"; |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 50 | private static final String MSG_ERROR = "ERROR"; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 54 | OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 55 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 56 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 57 | OpenstackNode osNode = osNodeService.node(hostname); |
| 58 | if (osNode == null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 59 | print("Cannot find %s from registered nodes", hostname); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 63 | if (osNode.type() == CONTROLLER) { |
| 64 | print("[Openstack Controller Status]"); |
| 65 | |
| 66 | OSClient client = getConnectedClient(osNode); |
| 67 | if (client == null) { |
| 68 | error("The given keystone info is incorrect to get authorized to openstack"); |
| 69 | print("keystoneConfig=%s", osNode.keystoneConfig()); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 70 | } |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 71 | |
| 72 | if (osNode.keystoneConfig() != null) { |
| 73 | print("%s keystoneConfig=%s, neutronConfig=%s", |
| 74 | osNode.state() == NodeState.COMPLETE && client != null ? |
| 75 | MSG_OK : MSG_ERROR, |
| 76 | osNode.keystoneConfig(), |
| 77 | osNode.neutronConfig()); |
| 78 | } else { |
| 79 | print("%s keystoneConfig is missing", MSG_ERROR); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame] | 80 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 81 | } else { |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 82 | print("[Integration Bridge Status]"); |
| 83 | Device device = deviceService.getDevice(osNode.intgBridge()); |
| 84 | if (device != null) { |
| 85 | print("%s %s=%s available=%s %s", |
| 86 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_ERROR, |
| 87 | INTEGRATION_BRIDGE, |
| 88 | device.id(), |
| 89 | deviceService.isAvailable(device.id()), |
| 90 | device.annotations()); |
| 91 | if (osNode.dataIp() != null) { |
| 92 | printPortState(deviceService, osNode.intgBridge(), DEFAULT_TUNNEL); |
| 93 | } |
| 94 | if (osNode.vlanIntf() != null) { |
| 95 | printPortState(deviceService, osNode.intgBridge(), osNode.vlanIntf()); |
| 96 | } |
| 97 | if (osNode.type() == GATEWAY) { |
| 98 | printPortState(deviceService, osNode.intgBridge(), osNode.uplinkPort()); |
| 99 | } |
| 100 | } else { |
| 101 | print("%s %s=%s is not available", |
| 102 | MSG_ERROR, |
| 103 | INTEGRATION_BRIDGE, |
| 104 | osNode.intgBridge()); |
| 105 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 106 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 109 | private void printPortState(DeviceService deviceService, DeviceId deviceId, String portName) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 110 | Port port = deviceService.getPorts(deviceId).stream() |
| 111 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 112 | p.isEnabled()) |
| 113 | .findAny().orElse(null); |
| 114 | |
| 115 | if (port != null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 116 | print("%s %s portNum=%s enabled=%s %s", |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 117 | port.isEnabled() ? MSG_OK : MSG_ERROR, |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 118 | portName, |
| 119 | port.number(), |
| 120 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 121 | port.annotations()); |
| 122 | } else { |
Jian Li | a09f3c3 | 2018-10-08 10:48:34 +0900 | [diff] [blame] | 123 | print("%s %s does not exist", MSG_ERROR, portName); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 126 | } |