blob: 32c32c05092199476ab362ac212e9bcbe47c5d94 [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;
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 Lic38e9032018-08-09 17:08:38 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.mastership.MastershipService;
27import org.onosproject.openstacknetworking.api.InstancePort;
28import org.onosproject.openstacknetworking.api.InstancePortService;
29import org.onosproject.openstacknode.api.OpenstackNode;
30import org.onosproject.openstacknode.api.OpenstackNodeService;
31import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService;
32import org.onosproject.openstacktroubleshoot.api.Reachability;
33
34import java.util.Set;
Jian Li9171c002018-08-19 22:58:40 +090035import java.util.concurrent.ExecutorService;
Jian Lic38e9032018-08-09 17:08:38 +090036
Jian Li9171c002018-08-19 22:58:40 +090037import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
38import static org.onlab.util.Tools.groupedThreads;
Jian Lic38e9032018-08-09 17:08:38 +090039import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
40import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
41
42/**
43 * Checks the north-south VM connectivity.
44 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070045@Service
Jian Lic38e9032018-08-09 17:08:38 +090046@Command(scope = "onos", name = "openstack-check-north-south",
47 description = "Checks the north-south VMs connectivity")
48public class OpenstackNorthSouthProbeCommand 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
56 @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 Li9171c002018-08-19 22:58:40 +090064 private final ExecutorService probeExecutor = newSingleThreadScheduledExecutor(
65 groupedThreads(this.getClass().getSimpleName(), "probe-handler", log));
66
Jian Lic38e9032018-08-09 17:08:38 +090067 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070068 protected void doExecute() {
Jian Lic38e9032018-08-09 17:08:38 +090069 OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class);
70 InstancePortService instPortService = get(InstancePortService.class);
71 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
72 MastershipService mastershipService = get(MastershipService.class);
73 ClusterService clusterService = get(ClusterService.class);
74
75 if (tsService == null || osNodeService == null ||
76 instPortService == null || mastershipService == null) {
77 error("Failed to troubleshoot openstack networking.");
78 return;
79 }
80
81 if ((!isAll && vmIps == null) || (isAll && vmIps != null)) {
82 print("Please specify one of VM IP address or -a option.");
83 return;
84 }
85
86 NodeId localNodeId = clusterService.getLocalNode().id();
87
88 for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) {
89 if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) {
Jian Li43d04282018-08-22 14:31:21 +090090 error("Current node is not the master for all gateway nodes. " +
91 "Please enforce mastership first using openstack-reset-mastership -c !");
Jian Lic38e9032018-08-09 17:08:38 +090092 return;
93 }
94 }
95
96 if (isAll) {
97 printHeader();
98
99 // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE
100 instPortService.instancePorts().stream()
101 .filter(p -> p.state() == ACTIVE)
102 .filter(p -> instPortService.floatingIp(p.portId()) != null)
103 .forEach(port -> printReachability(tsService.probeNorthSouth(port)));
104 } else {
105
106 final Set<InstancePort> ports = Sets.newConcurrentHashSet();
107
108 for (String ip : vmIps) {
109 instPortService.instancePorts().stream()
110 .filter(p -> p.state().equals(InstancePort.State.ACTIVE))
111 .filter(p -> instPortService.floatingIp(p.portId()) != null)
112 .filter(p -> ip.equals(instPortService.floatingIp(p.portId()).toString()))
113 .forEach(ports::add);
114 }
115
116 printHeader();
Jian Li9171c002018-08-19 22:58:40 +0900117 ports.forEach(port -> probeExecutor.execute(() ->
118 printReachability(tsService.probeNorthSouth(port))));
Jian Lic38e9032018-08-09 17:08:38 +0900119 }
120 }
121
122 private void printHeader() {
123 print(FORMAT, "Source IP", "", "Destination IP", "Reachability");
124 }
125
126 private void printReachability(Reachability r) {
127 if (r == null) {
128 return;
129 }
130 String result = r.isReachable() ? REACHABLE : UNREACHABLE;
131 print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result);
132 }
133}