Jian Li | 0b93b00 | 2018-07-31 13:41:08 +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 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.packet.IpAddress; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePortService; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService; |
| 27 | import org.onosproject.openstacktroubleshoot.api.Reachability; |
| 28 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 29 | import java.util.Optional; |
| 30 | import java.util.Set; |
| 31 | import java.util.stream.Collectors; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 32 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 33 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
| 34 | |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 35 | /** |
| 36 | * Checks the east-west VMs connectivity. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "openstack-check-east-west", |
| 39 | description = "Checks the east-west VMs connectivity") |
| 40 | public class OpenstackEastWestProbeCommand extends AbstractShellCommand { |
| 41 | |
| 42 | private static final String REACHABLE = "Reachable :)"; |
| 43 | private static final String UNREACHABLE = "Unreachable :("; |
| 44 | private static final String ARROW = "->"; |
| 45 | |
| 46 | private static final String FORMAT = "%-20s%-5s%-20s%-20s"; |
| 47 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 48 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs", |
| 49 | required = false, multiValued = false) |
| 50 | private boolean isAll = false; |
| 51 | |
| 52 | @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses", |
| 53 | required = false, multiValued = true) |
| 54 | private String[] vmIps = null; |
| 55 | |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 56 | @Override |
| 57 | protected void execute() { |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 58 | OpenstackTroubleshootService tsService = |
| 59 | get(OpenstackTroubleshootService.class); |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 60 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 61 | InstancePortService instPortService = get(InstancePortService.class); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 62 | |
| 63 | if (tsService == null) { |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 64 | error("Failed to troubleshoot openstack networking."); |
| 65 | return; |
| 66 | } |
| 67 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 68 | if ((!isAll && vmIps == null) || (isAll && vmIps != null)) { |
| 69 | print("Please specify one of VM IP address or -a option."); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (isAll) { |
| 74 | printHeader(); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 75 | // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE |
| 76 | Set<InstancePort> activePorts = instPortService.instancePorts().stream() |
| 77 | .filter(p -> p.state() == ACTIVE) |
| 78 | .collect(Collectors.toSet()); |
| 79 | |
| 80 | activePorts.forEach(srcPort -> |
| 81 | activePorts.forEach(dstPort -> |
| 82 | printReachability(tsService.probeEastWest(srcPort, dstPort)) |
| 83 | ) |
| 84 | ); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 85 | } else { |
| 86 | if (vmIps.length > 2) { |
| 87 | print("Too many VM IPs. The number of IP should be limited to 2."); |
| 88 | return; |
| 89 | } |
| 90 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 91 | IpAddress srcIp = getIpAddress(vmIps[0]); |
| 92 | |
| 93 | if (srcIp == null) { |
| 94 | return; |
| 95 | } |
| 96 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 97 | InstancePort srcPort = instPort(instPortService, srcIp); |
| 98 | |
| 99 | if (srcPort == null) { |
| 100 | print("Specified source IP is not existing."); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | final Set<IpAddress> dstIps = Sets.newConcurrentHashSet(); |
| 105 | |
| 106 | if (vmIps.length == 2) { |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 107 | IpAddress dstIp = getIpAddress(vmIps[1]); |
| 108 | |
| 109 | if (dstIp == null) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | dstIps.add(dstIp); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (vmIps.length == 1) { |
| 117 | dstIps.addAll(instPortService.instancePorts().stream() |
| 118 | .filter(p -> !p.ipAddress().equals(srcIp)) |
| 119 | .filter(p -> p.state().equals(InstancePort.State.ACTIVE)) |
| 120 | .map(InstancePort::ipAddress) |
| 121 | .collect(Collectors.toSet())); |
| 122 | } |
| 123 | |
| 124 | printHeader(); |
| 125 | dstIps.stream() |
| 126 | .filter(ip -> instPort(instPortService, ip) != null) |
| 127 | .map(ip -> instPort(instPortService, ip)) |
| 128 | .forEach(port -> printReachability(tsService.probeEastWest(srcPort, port))); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private InstancePort instPort(InstancePortService service, IpAddress ip) { |
| 133 | Optional<InstancePort> port = service.instancePorts().stream() |
| 134 | .filter(p -> p.ipAddress().equals(ip)).findFirst(); |
| 135 | |
| 136 | if (port.isPresent()) { |
| 137 | return port.get(); |
| 138 | } else { |
| 139 | print("Specified destination IP is not existing."); |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private void printHeader() { |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 145 | print(FORMAT, "Source IP", "", "Destination IP", "Reachability"); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 146 | } |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 147 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 148 | private void printReachability(Reachability r) { |
| 149 | String result = r.isReachable() ? REACHABLE : UNREACHABLE; |
| 150 | print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result); |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 151 | } |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame^] | 152 | |
| 153 | private IpAddress getIpAddress(String ipString) { |
| 154 | try { |
| 155 | return IpAddress.valueOf(vmIps[0]); |
| 156 | } catch (IllegalArgumentException e) { |
| 157 | error("Invalid IP address string."); |
| 158 | return null; |
| 159 | } |
| 160 | } |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 161 | } |