Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.openstacktroubleshoot.cli; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.mastership.MastershipService; |
| 27 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 28 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 31 | import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService; |
| 32 | import org.onosproject.openstacktroubleshoot.api.Reachability; |
| 33 | |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
| 37 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
| 38 | |
| 39 | /** |
| 40 | * Checks the north-south VM connectivity. |
| 41 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 42 | @Service |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 43 | @Command(scope = "onos", name = "openstack-check-north-south", |
| 44 | description = "Checks the north-south VMs connectivity") |
| 45 | public class OpenstackNorthSouthProbeCommand extends AbstractShellCommand { |
| 46 | |
| 47 | private static final String REACHABLE = "Reachable :)"; |
| 48 | private static final String UNREACHABLE = "Unreachable :("; |
| 49 | private static final String ARROW = "->"; |
| 50 | |
| 51 | private static final String FORMAT = "%-20s%-5s%-20s%-20s"; |
| 52 | |
| 53 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs", |
| 54 | required = false, multiValued = false) |
| 55 | private boolean isAll = false; |
| 56 | |
| 57 | @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses", |
| 58 | required = false, multiValued = true) |
| 59 | private String[] vmIps = null; |
| 60 | |
| 61 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 63 | OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class); |
| 64 | InstancePortService instPortService = get(InstancePortService.class); |
| 65 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
| 66 | MastershipService mastershipService = get(MastershipService.class); |
| 67 | ClusterService clusterService = get(ClusterService.class); |
| 68 | |
| 69 | if (tsService == null || osNodeService == null || |
| 70 | instPortService == null || mastershipService == null) { |
| 71 | error("Failed to troubleshoot openstack networking."); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if ((!isAll && vmIps == null) || (isAll && vmIps != null)) { |
| 76 | print("Please specify one of VM IP address or -a option."); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 81 | |
| 82 | for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) { |
| 83 | if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) { |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 84 | error("Current node is not the master for all gateway nodes. " + |
| 85 | "Please enforce mastership first using openstack-reset-mastership -c !"); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (isAll) { |
| 91 | printHeader(); |
| 92 | |
| 93 | // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE |
| 94 | instPortService.instancePorts().stream() |
| 95 | .filter(p -> p.state() == ACTIVE) |
| 96 | .filter(p -> instPortService.floatingIp(p.portId()) != null) |
| 97 | .forEach(port -> printReachability(tsService.probeNorthSouth(port))); |
| 98 | } else { |
| 99 | |
| 100 | final Set<InstancePort> ports = Sets.newConcurrentHashSet(); |
| 101 | |
| 102 | for (String ip : vmIps) { |
| 103 | instPortService.instancePorts().stream() |
| 104 | .filter(p -> p.state().equals(InstancePort.State.ACTIVE)) |
| 105 | .filter(p -> instPortService.floatingIp(p.portId()) != null) |
| 106 | .filter(p -> ip.equals(instPortService.floatingIp(p.portId()).toString())) |
| 107 | .forEach(ports::add); |
| 108 | } |
| 109 | |
| 110 | printHeader(); |
| 111 | ports.forEach(port -> printReachability(tsService.probeNorthSouth(port))); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void printHeader() { |
| 116 | print(FORMAT, "Source IP", "", "Destination IP", "Reachability"); |
| 117 | } |
| 118 | |
| 119 | private void printReachability(Reachability r) { |
| 120 | if (r == null) { |
| 121 | return; |
| 122 | } |
| 123 | String result = r.isReachable() ? REACHABLE : UNREACHABLE; |
| 124 | print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result); |
| 125 | } |
| 126 | } |