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