blob: 37240b77e571bebe77f9e0a2ecbdc0abe862a41e [file] [log] [blame]
Jian Lic38e9032018-08-09 17:08:38 +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
18import 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;
Jian Lidb521c12018-11-19 17:42:35 +090021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lic38e9032018-08-09 17:08:38 +090024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.mastership.MastershipService;
28import org.onosproject.openstacknetworking.api.InstancePort;
29import org.onosproject.openstacknetworking.api.InstancePortService;
30import org.onosproject.openstacknode.api.OpenstackNode;
31import org.onosproject.openstacknode.api.OpenstackNodeService;
32import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService;
33import org.onosproject.openstacktroubleshoot.api.Reachability;
34
35import java.util.Set;
Jian Li9171c002018-08-19 22:58:40 +090036import java.util.concurrent.ExecutorService;
Jian Lic38e9032018-08-09 17:08:38 +090037
Jian Li9171c002018-08-19 22:58:40 +090038import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
39import static org.onlab.util.Tools.groupedThreads;
Jian Lic38e9032018-08-09 17:08:38 +090040import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
41import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
42
43/**
44 * Checks the north-south VM connectivity.
45 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070046@Service
Jian Lic38e9032018-08-09 17:08:38 +090047@Command(scope = "onos", name = "openstack-check-north-south",
48 description = "Checks the north-south VMs connectivity")
49public class OpenstackNorthSouthProbeCommand extends AbstractShellCommand {
50
51 private static final String REACHABLE = "Reachable :)";
52 private static final String UNREACHABLE = "Unreachable :(";
53 private static final String ARROW = "->";
54
55 private static final String FORMAT = "%-20s%-5s%-20s%-20s";
56
57 @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs",
58 required = false, multiValued = false)
59 private boolean isAll = false;
60
61 @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses",
62 required = false, multiValued = true)
Jian Lidb521c12018-11-19 17:42:35 +090063 @Completion(ActiveFloatingIpCompleter.class)
Jian Lic38e9032018-08-09 17:08:38 +090064 private String[] vmIps = null;
65
Jian Li9171c002018-08-19 22:58:40 +090066 private final ExecutorService probeExecutor = newSingleThreadScheduledExecutor(
67 groupedThreads(this.getClass().getSimpleName(), "probe-handler", log));
68
Jian Lic38e9032018-08-09 17:08:38 +090069 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070070 protected void doExecute() {
Jian Lic38e9032018-08-09 17:08:38 +090071 OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class);
72 InstancePortService instPortService = get(InstancePortService.class);
73 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
74 MastershipService mastershipService = get(MastershipService.class);
75 ClusterService clusterService = get(ClusterService.class);
76
77 if (tsService == null || osNodeService == null ||
78 instPortService == null || mastershipService == null) {
79 error("Failed to troubleshoot openstack networking.");
80 return;
81 }
82
83 if ((!isAll && vmIps == null) || (isAll && vmIps != null)) {
84 print("Please specify one of VM IP address or -a option.");
85 return;
86 }
87
88 NodeId localNodeId = clusterService.getLocalNode().id();
89
90 for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) {
91 if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) {
Jian Li43d04282018-08-22 14:31:21 +090092 error("Current node is not the master for all gateway nodes. " +
93 "Please enforce mastership first using openstack-reset-mastership -c !");
Jian Lic38e9032018-08-09 17:08:38 +090094 return;
95 }
96 }
97
98 if (isAll) {
99 printHeader();
100
101 // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE
102 instPortService.instancePorts().stream()
103 .filter(p -> p.state() == ACTIVE)
104 .filter(p -> instPortService.floatingIp(p.portId()) != null)
105 .forEach(port -> printReachability(tsService.probeNorthSouth(port)));
106 } else {
107
108 final Set<InstancePort> ports = Sets.newConcurrentHashSet();
109
110 for (String ip : vmIps) {
111 instPortService.instancePorts().stream()
112 .filter(p -> p.state().equals(InstancePort.State.ACTIVE))
113 .filter(p -> instPortService.floatingIp(p.portId()) != null)
114 .filter(p -> ip.equals(instPortService.floatingIp(p.portId()).toString()))
115 .forEach(ports::add);
116 }
117
118 printHeader();
Jian Li9171c002018-08-19 22:58:40 +0900119 ports.forEach(port -> probeExecutor.execute(() ->
120 printReachability(tsService.probeNorthSouth(port))));
Jian Lic38e9032018-08-09 17:08:38 +0900121 }
122 }
123
124 private void printHeader() {
125 print(FORMAT, "Source IP", "", "Destination IP", "Reachability");
126 }
127
128 private void printReachability(Reachability r) {
129 if (r == null) {
130 return;
131 }
132 String result = r.isReachable() ? REACHABLE : UNREACHABLE;
133 print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result);
134 }
135}