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; |
| 26 | import org.onosproject.openstacknode.OpenstackNode; |
| 27 | import org.onosproject.openstacknode.OpenstackNodeService; |
| 28 | |
| 29 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 30 | import static org.onosproject.openstacknode.Constants.*; |
| 31 | import static org.onosproject.openstacknode.OpenstackNodeService.NodeType.GATEWAY; |
| 32 | |
| 33 | /** |
| 34 | * Checks detailed node init state. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "openstack-node-check", |
| 37 | description = "Shows detailed node init state") |
| 38 | public class OpenstackNodeCheckCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "hostname", description = "Hostname", |
| 41 | required = true, multiValued = false) |
| 42 | private String hostname = null; |
| 43 | |
| 44 | private static final String MSG_OK = "OK"; |
| 45 | private static final String MSG_NO = "NO"; |
| 46 | |
| 47 | @Override |
| 48 | protected void execute() { |
| 49 | OpenstackNodeService nodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
| 50 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 51 | |
| 52 | OpenstackNode node = nodeService.nodes() |
| 53 | .stream() |
| 54 | .filter(n -> n.hostname().equals(hostname)) |
| 55 | .findFirst() |
| 56 | .orElse(null); |
| 57 | |
| 58 | if (node == null) { |
| 59 | print("Cannot find %s from registered nodes", hostname); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | print("%n[Integration Bridge Status]"); |
| 64 | Device device = deviceService.getDevice(node.intBridge()); |
| 65 | if (device != null) { |
| 66 | print("%s %s=%s available=%s %s", |
| 67 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO, |
| 68 | INTEGRATION_BRIDGE, |
| 69 | device.id(), |
| 70 | deviceService.isAvailable(device.id()), |
| 71 | device.annotations()); |
| 72 | |
| 73 | print(getPortState(deviceService, node.intBridge(), DEFAULT_TUNNEL)); |
| 74 | } else { |
| 75 | print("%s %s=%s is not available", |
| 76 | MSG_NO, |
| 77 | INTEGRATION_BRIDGE, |
| 78 | node.intBridge()); |
| 79 | } |
| 80 | |
| 81 | if (node.type().equals(GATEWAY)) { |
| 82 | print("%n[Router Bridge Status]"); |
| 83 | device = deviceService.getDevice(node.routerBridge().get()); |
| 84 | if (device != null) { |
| 85 | print("%s %s=%s available=%s %s", |
| 86 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO, |
| 87 | ROUTER_BRIDGE, |
| 88 | device.id(), |
| 89 | deviceService.isAvailable(device.id()), |
| 90 | device.annotations()); |
| 91 | |
| 92 | print(getPortState(deviceService, node.routerBridge().get(), PATCH_ROUT_BRIDGE)); |
| 93 | print(getPortState(deviceService, node.intBridge(), PATCH_INTG_BRIDGE)); |
| 94 | } else { |
| 95 | print("%s %s=%s is not available", |
| 96 | MSG_NO, |
| 97 | ROUTER_BRIDGE, |
| 98 | node.intBridge()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private String getPortState(DeviceService deviceService, DeviceId deviceId, String portName) { |
| 104 | Port port = deviceService.getPorts(deviceId).stream() |
| 105 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 106 | p.isEnabled()) |
| 107 | .findAny().orElse(null); |
| 108 | |
| 109 | if (port != null) { |
| 110 | return String.format("%s %s portNum=%s enabled=%s %s", |
| 111 | port.isEnabled() ? MSG_OK : MSG_NO, |
| 112 | portName, |
| 113 | port.number(), |
| 114 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 115 | port.annotations()); |
| 116 | } else { |
| 117 | return String.format("%s %s does not exist", MSG_NO, portName); |
| 118 | } |
| 119 | } |
| 120 | } |