blob: 0b832dab8a0c42b7c20bbb553ef2bd74cf054594 [file] [log] [blame]
Jian Li0b93b002018-07-31 13:41:08 +09001/*
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 */
16package org.onosproject.openstacktroubleshoot.cli;
17
Jian Lie189c1c2018-08-08 15:55:08 +090018import com.google.common.collect.Sets;
19import org.apache.karaf.shell.commands.Argument;
Jian Li0b93b002018-07-31 13:41:08 +090020import org.apache.karaf.shell.commands.Command;
Jian Lie189c1c2018-08-08 15:55:08 +090021import org.apache.karaf.shell.commands.Option;
22import org.onlab.packet.IpAddress;
Jian Li0b93b002018-07-31 13:41:08 +090023import org.onosproject.cli.AbstractShellCommand;
Jian Lie189c1c2018-08-08 15:55:08 +090024import org.onosproject.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.api.InstancePortService;
Jian Li0b93b002018-07-31 13:41:08 +090026import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService;
27import org.onosproject.openstacktroubleshoot.api.Reachability;
28
Jian Lie189c1c2018-08-08 15:55:08 +090029import java.util.Optional;
30import java.util.Set;
31import java.util.stream.Collectors;
Jian Li0b93b002018-07-31 13:41:08 +090032
Jian Lic38e9032018-08-09 17:08:38 +090033import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
34
Jian Li0b93b002018-07-31 13:41:08 +090035/**
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")
40public 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 Lie189c1c2018-08-08 15:55:08 +090048 @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 Li0b93b002018-07-31 13:41:08 +090056 @Override
57 protected void execute() {
Jian Lie189c1c2018-08-08 15:55:08 +090058 OpenstackTroubleshootService tsService =
59 get(OpenstackTroubleshootService.class);
Jian Li0b93b002018-07-31 13:41:08 +090060
Jian Lic38e9032018-08-09 17:08:38 +090061 InstancePortService instPortService = get(InstancePortService.class);
Jian Lie189c1c2018-08-08 15:55:08 +090062
63 if (tsService == null) {
Jian Li0b93b002018-07-31 13:41:08 +090064 error("Failed to troubleshoot openstack networking.");
65 return;
66 }
67
Jian Lie189c1c2018-08-08 15:55:08 +090068 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 Lic38e9032018-08-09 17:08:38 +090075 // 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 Lie189c1c2018-08-08 15:55:08 +090085 } 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 Lic38e9032018-08-09 17:08:38 +090091 IpAddress srcIp = getIpAddress(vmIps[0]);
92
93 if (srcIp == null) {
94 return;
95 }
96
Jian Lie189c1c2018-08-08 15:55:08 +090097 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 Lic38e9032018-08-09 17:08:38 +0900107 IpAddress dstIp = getIpAddress(vmIps[1]);
108
109 if (dstIp == null) {
110 return;
111 }
112
113 dstIps.add(dstIp);
Jian Lie189c1c2018-08-08 15:55:08 +0900114 }
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 Li0b93b002018-07-31 13:41:08 +0900145 print(FORMAT, "Source IP", "", "Destination IP", "Reachability");
Jian Lie189c1c2018-08-08 15:55:08 +0900146 }
Jian Li0b93b002018-07-31 13:41:08 +0900147
Jian Lie189c1c2018-08-08 15:55:08 +0900148 private void printReachability(Reachability r) {
149 String result = r.isReachable() ? REACHABLE : UNREACHABLE;
150 print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result);
Jian Li0b93b002018-07-31 13:41:08 +0900151 }
Jian Lic38e9032018-08-09 17:08:38 +0900152
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 Li0b93b002018-07-31 13:41:08 +0900161}