blob: 6a27c0e465a29c06a058869d3fe9be3eb5131b25 [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;
25import org.onosproject.net.device.DeviceService;
Hyunsun Moona9465642017-06-29 16:28:58 +090026import org.onosproject.net.group.Group;
27import org.onosproject.net.group.GroupBucket;
28import org.onosproject.net.group.GroupService;
Hyunsun Moon0d457362017-06-27 17:19:41 +090029import org.onosproject.openstacknode.api.OpenstackNode;
30import org.onosproject.openstacknode.api.OpenstackNodeService;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070031
32import static org.onosproject.net.AnnotationKeys.PORT_NAME;
Hyunsun Moon0d457362017-06-27 17:19:41 +090033import static org.onosproject.openstacknode.api.Constants.*;
Hyunsun Moona9465642017-06-29 16:28:58 +090034import static org.onosproject.openstacknode.api.OpenstackNode.NetworkMode.VLAN;
35import static org.onosproject.openstacknode.api.OpenstackNode.NetworkMode.VXLAN;
Hyunsun Moon0d457362017-06-27 17:19:41 +090036import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070037
38/**
39 * Checks detailed node init state.
40 */
41@Command(scope = "onos", name = "openstack-node-check",
42 description = "Shows detailed node init state")
43public 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 Moona9465642017-06-29 16:28:58 +090051 private static final String BUCKET_FORMAT =
52 " bucket=%s, bytes=%s, packets=%s, actions=%s";
Hyunsun Moon34bbe172016-06-28 19:18:40 -070053
54 @Override
55 protected void execute() {
Hyunsun Moon0d457362017-06-27 17:19:41 +090056 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070057 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
Hyunsun Moona9465642017-06-29 16:28:58 +090058 GroupService groupService = AbstractShellCommand.get(GroupService.class);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070059
Hyunsun Moon0d457362017-06-27 17:19:41 +090060 OpenstackNode osNode = osNodeService.node(hostname);
61 if (osNode == null) {
Hyunsun Moona9465642017-06-29 16:28:58 +090062 print("Cannot find %s from registered nodes", hostname);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070063 return;
64 }
65
Hyunsun Moon052c71f2016-07-11 18:56:18 -070066 print("[Integration Bridge Status]");
Hyunsun Moon0d457362017-06-27 17:19:41 +090067 Device device = deviceService.getDevice(osNode.intgBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070068 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 Moon0d457362017-06-27 17:19:41 +090075 if (osNode.dataIp() != null) {
Hyunsun Moona9465642017-06-29 16:28:58 +090076 printPortState(deviceService, osNode.intgBridge(), DEFAULT_TUNNEL);
Hyunsun Moon0d457362017-06-27 17:19:41 +090077 }
78 if (osNode.vlanIntf() != null) {
Hyunsun Moona9465642017-06-29 16:28:58 +090079 printPortState(deviceService, osNode.intgBridge(), osNode.vlanIntf());
Hyunsun Moon0d457362017-06-27 17:19:41 +090080 }
Hyunsun Moona9465642017-06-29 16:28:58 +090081 printGatewayGroupState(osNodeService, groupService, osNode);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070082 } else {
83 print("%s %s=%s is not available",
84 MSG_NO,
85 INTEGRATION_BRIDGE,
Hyunsun Moon0d457362017-06-27 17:19:41 +090086 osNode.intgBridge());
Hyunsun Moon34bbe172016-06-28 19:18:40 -070087 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -070088 }
89
Hyunsun Moona9465642017-06-29 16:28:58 +090090 private void printPortState(DeviceService deviceService, DeviceId deviceId, String portName) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070091 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 Moona9465642017-06-29 16:28:58 +090097 print("%s %s portNum=%s enabled=%s %s",
Hyunsun Moon34bbe172016-06-28 19:18:40 -070098 port.isEnabled() ? MSG_OK : MSG_NO,
99 portName,
100 port.number(),
101 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
102 port.annotations());
103 } else {
Hyunsun Moona9465642017-06-29 16:28:58 +0900104 print("%s %s does not exist", MSG_NO, portName);
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700105 }
106 }
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700107
Hyunsun Moona9465642017-06-29 16:28:58 +0900108 private void printGatewayGroupState(OpenstackNodeService osNodeService,
109 GroupService groupService, OpenstackNode osNode) {
110 if (osNode.type() == GATEWAY) {
111 return;
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700112 }
Hyunsun Moona9465642017-06-29 16:28:58 +0900113 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 Moon052c71f2016-07-11 18:56:18 -0700144 }
145 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700146}