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; |
| 25 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 26 | import org.onosproject.net.group.Group; |
| 27 | import org.onosproject.net.group.GroupBucket; |
| 28 | import org.onosproject.net.group.GroupService; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 31 | |
| 32 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 33 | import static org.onosproject.openstacknode.api.Constants.*; |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 34 | import static org.onosproject.openstacknode.api.OpenstackNode.NetworkMode.VLAN; |
| 35 | import static org.onosproject.openstacknode.api.OpenstackNode.NetworkMode.VXLAN; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 36 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
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"; |
| 50 | private static final String MSG_NO = "NO"; |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 51 | private static final String BUCKET_FORMAT = |
| 52 | " bucket=%s, bytes=%s, packets=%s, actions=%s"; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 53 | |
| 54 | @Override |
| 55 | protected void execute() { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 56 | OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 57 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 58 | GroupService groupService = AbstractShellCommand.get(GroupService.class); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 59 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 60 | OpenstackNode osNode = osNodeService.node(hostname); |
| 61 | if (osNode == null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 62 | print("Cannot find %s from registered nodes", hostname); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 66 | print("[Integration Bridge Status]"); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 67 | Device device = deviceService.getDevice(osNode.intgBridge()); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 68 | if (device != null) { |
| 69 | print("%s %s=%s available=%s %s", |
| 70 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO, |
| 71 | INTEGRATION_BRIDGE, |
| 72 | device.id(), |
| 73 | deviceService.isAvailable(device.id()), |
| 74 | device.annotations()); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 75 | if (osNode.dataIp() != null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 76 | printPortState(deviceService, osNode.intgBridge(), DEFAULT_TUNNEL); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 77 | } |
| 78 | if (osNode.vlanIntf() != null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 79 | printPortState(deviceService, osNode.intgBridge(), osNode.vlanIntf()); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 80 | } |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 81 | printGatewayGroupState(osNodeService, groupService, osNode); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 82 | } else { |
| 83 | print("%s %s=%s is not available", |
| 84 | MSG_NO, |
| 85 | INTEGRATION_BRIDGE, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 86 | osNode.intgBridge()); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 87 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 90 | private void printPortState(DeviceService deviceService, DeviceId deviceId, String portName) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 91 | Port port = deviceService.getPorts(deviceId).stream() |
| 92 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 93 | p.isEnabled()) |
| 94 | .findAny().orElse(null); |
| 95 | |
| 96 | if (port != null) { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 97 | print("%s %s portNum=%s enabled=%s %s", |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 98 | port.isEnabled() ? MSG_OK : MSG_NO, |
| 99 | portName, |
| 100 | port.number(), |
| 101 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 102 | port.annotations()); |
| 103 | } else { |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 104 | print("%s %s does not exist", MSG_NO, portName); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 107 | |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 108 | private void printGatewayGroupState(OpenstackNodeService osNodeService, |
| 109 | GroupService groupService, OpenstackNode osNode) { |
| 110 | if (osNode.type() == GATEWAY) { |
| 111 | return; |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 112 | } |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 113 | if (osNodeService.completeNodes(GATEWAY).isEmpty()) { |
| 114 | print("N/A No complete state gateway nodes exist"); |
| 115 | return; |
| 116 | } |
| 117 | if (osNode.dataIp() != null) { |
| 118 | Group osGroup = groupService.getGroup(osNode.intgBridge(), |
| 119 | osNode.gatewayGroupKey(VXLAN)); |
| 120 | if (osGroup == null || osGroup.state() != Group.GroupState.ADDED) { |
| 121 | print("%s VXLAN gateway group does not exist", MSG_NO); |
| 122 | } else { |
| 123 | print("%s VXLAN group 0x%s added", MSG_OK, Integer.toHexString(osGroup.id().id())); |
| 124 | int i = 0; |
| 125 | for (GroupBucket bucket : osGroup.buckets().buckets()) { |
| 126 | print(BUCKET_FORMAT, ++i, bucket.bytes(), bucket.packets(), |
| 127 | bucket.treatment().allInstructions()); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | if (osNode.vlanIntf() != null) { |
| 132 | Group osGroup = groupService.getGroup(osNode.intgBridge(), |
| 133 | osNode.gatewayGroupKey(VLAN)); |
| 134 | if (osGroup == null || osGroup.state() != Group.GroupState.ADDED) { |
| 135 | print("\n%s VLAN gateway group does not exist", MSG_NO); |
| 136 | } else { |
| 137 | print("\n%s VLAN group 0x%s added", MSG_OK, Integer.toHexString(osGroup.id().id())); |
| 138 | int i = 0; |
| 139 | for (GroupBucket bucket : osGroup.buckets().buckets()) { |
| 140 | print(BUCKET_FORMAT, ++i, bucket.bytes(), bucket.packets(), |
| 141 | bucket.treatment().allInstructions()); |
| 142 | } |
| 143 | } |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 146 | } |