blob: 943290bd15d23232e7066e78f46ccaa0c76ee1d1 [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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lie189c1c2018-08-08 15:55:08 +090023import org.onlab.packet.IpAddress;
Jian Li0b93b002018-07-31 13:41:08 +090024import org.onosproject.cli.AbstractShellCommand;
Jian Li43d04282018-08-22 14:31:21 +090025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.mastership.MastershipService;
Jian Lie189c1c2018-08-08 15:55:08 +090028import org.onosproject.openstacknetworking.api.InstancePort;
29import org.onosproject.openstacknetworking.api.InstancePortService;
Jian Li43d04282018-08-22 14:31:21 +090030import org.onosproject.openstacknode.api.OpenstackNode;
31import org.onosproject.openstacknode.api.OpenstackNodeService;
Jian Li0b93b002018-07-31 13:41:08 +090032import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService;
33import org.onosproject.openstacktroubleshoot.api.Reachability;
34
Jian Lie189c1c2018-08-08 15:55:08 +090035import java.util.Optional;
36import java.util.Set;
37import java.util.stream.Collectors;
Jian Li0b93b002018-07-31 13:41:08 +090038
Jian Lic38e9032018-08-09 17:08:38 +090039import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
Jian Li43d04282018-08-22 14:31:21 +090040import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
Jian Lic38e9032018-08-09 17:08:38 +090041
Jian Li0b93b002018-07-31 13:41:08 +090042/**
43 * Checks the east-west VMs connectivity.
44 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070045@Service
Jian Li0b93b002018-07-31 13:41:08 +090046@Command(scope = "onos", name = "openstack-check-east-west",
47 description = "Checks the east-west VMs connectivity")
48public class OpenstackEastWestProbeCommand extends AbstractShellCommand {
49
50 private static final String REACHABLE = "Reachable :)";
51 private static final String UNREACHABLE = "Unreachable :(";
52 private static final String ARROW = "->";
53
54 private static final String FORMAT = "%-20s%-5s%-20s%-20s";
55
Jian Lie189c1c2018-08-08 15:55:08 +090056 @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs",
57 required = false, multiValued = false)
58 private boolean isAll = false;
59
60 @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses",
61 required = false, multiValued = true)
62 private String[] vmIps = null;
63
Jian Li0b93b002018-07-31 13:41:08 +090064 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Jian Li43d04282018-08-22 14:31:21 +090066 OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class);
Jian Lic38e9032018-08-09 17:08:38 +090067 InstancePortService instPortService = get(InstancePortService.class);
Jian Li43d04282018-08-22 14:31:21 +090068 MastershipService mastershipService = get(MastershipService.class);
69 ClusterService clusterService = get(ClusterService.class);
70 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
Jian Lie189c1c2018-08-08 15:55:08 +090071
72 if (tsService == null) {
Jian Li0b93b002018-07-31 13:41:08 +090073 error("Failed to troubleshoot openstack networking.");
74 return;
75 }
76
Jian Lie189c1c2018-08-08 15:55:08 +090077 if ((!isAll && vmIps == null) || (isAll && vmIps != null)) {
78 print("Please specify one of VM IP address or -a option.");
79 return;
80 }
81
Jian Li43d04282018-08-22 14:31:21 +090082 NodeId localNodeId = clusterService.getLocalNode().id();
83
84 for (OpenstackNode node : osNodeService.completeNodes(COMPUTE)) {
85 if (!localNodeId.equals(mastershipService.getMasterFor(node.intgBridge()))) {
86 error("Current node is not the master for all compute nodes. " +
87 "Please enforce mastership first using openstack-reset-mastership -c !");
88 return;
89 }
90 }
91
Jian Lie189c1c2018-08-08 15:55:08 +090092 if (isAll) {
93 printHeader();
Jian Lic38e9032018-08-09 17:08:38 +090094 // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE
95 Set<InstancePort> activePorts = instPortService.instancePorts().stream()
96 .filter(p -> p.state() == ACTIVE)
97 .collect(Collectors.toSet());
98
99 activePorts.forEach(srcPort ->
100 activePorts.forEach(dstPort ->
101 printReachability(tsService.probeEastWest(srcPort, dstPort))
102 )
103 );
Jian Lie189c1c2018-08-08 15:55:08 +0900104 } else {
105 if (vmIps.length > 2) {
106 print("Too many VM IPs. The number of IP should be limited to 2.");
107 return;
108 }
109
Jian Lic38e9032018-08-09 17:08:38 +0900110 IpAddress srcIp = getIpAddress(vmIps[0]);
111
112 if (srcIp == null) {
113 return;
114 }
115
Jian Lie189c1c2018-08-08 15:55:08 +0900116 InstancePort srcPort = instPort(instPortService, srcIp);
117
118 if (srcPort == null) {
119 print("Specified source IP is not existing.");
120 return;
121 }
122
123 final Set<IpAddress> dstIps = Sets.newConcurrentHashSet();
124
125 if (vmIps.length == 2) {
Jian Lic38e9032018-08-09 17:08:38 +0900126 IpAddress dstIp = getIpAddress(vmIps[1]);
127
128 if (dstIp == null) {
129 return;
130 }
131
132 dstIps.add(dstIp);
Jian Lie189c1c2018-08-08 15:55:08 +0900133 }
134
135 if (vmIps.length == 1) {
136 dstIps.addAll(instPortService.instancePorts().stream()
137 .filter(p -> !p.ipAddress().equals(srcIp))
138 .filter(p -> p.state().equals(InstancePort.State.ACTIVE))
139 .map(InstancePort::ipAddress)
140 .collect(Collectors.toSet()));
141 }
142
143 printHeader();
144 dstIps.stream()
145 .filter(ip -> instPort(instPortService, ip) != null)
146 .map(ip -> instPort(instPortService, ip))
147 .forEach(port -> printReachability(tsService.probeEastWest(srcPort, port)));
148 }
149 }
150
151 private InstancePort instPort(InstancePortService service, IpAddress ip) {
152 Optional<InstancePort> port = service.instancePorts().stream()
153 .filter(p -> p.ipAddress().equals(ip)).findFirst();
154
155 if (port.isPresent()) {
156 return port.get();
157 } else {
158 print("Specified destination IP is not existing.");
159 return null;
160 }
161 }
162
163 private void printHeader() {
Jian Li0b93b002018-07-31 13:41:08 +0900164 print(FORMAT, "Source IP", "", "Destination IP", "Reachability");
Jian Lie189c1c2018-08-08 15:55:08 +0900165 }
Jian Li0b93b002018-07-31 13:41:08 +0900166
Jian Lie189c1c2018-08-08 15:55:08 +0900167 private void printReachability(Reachability r) {
168 String result = r.isReachable() ? REACHABLE : UNREACHABLE;
169 print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result);
Jian Li0b93b002018-07-31 13:41:08 +0900170 }
Jian Lic38e9032018-08-09 17:08:38 +0900171
172 private IpAddress getIpAddress(String ipString) {
173 try {
174 return IpAddress.valueOf(vmIps[0]);
175 } catch (IllegalArgumentException e) {
176 error("Invalid IP address string.");
177 return null;
178 }
179 }
Jian Li0b93b002018-07-31 13:41:08 +0900180}