blob: edef1c2b773e4c70f793537e654dea873619192d [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;
35
36import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
37import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
38
39/**
40 * Checks the north-south VM connectivity.
41 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Jian Lic38e9032018-08-09 17:08:38 +090043@Command(scope = "onos", name = "openstack-check-north-south",
44 description = "Checks the north-south VMs connectivity")
45public class OpenstackNorthSouthProbeCommand extends AbstractShellCommand {
46
47 private static final String REACHABLE = "Reachable :)";
48 private static final String UNREACHABLE = "Unreachable :(";
49 private static final String ARROW = "->";
50
51 private static final String FORMAT = "%-20s%-5s%-20s%-20s";
52
53 @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs",
54 required = false, multiValued = false)
55 private boolean isAll = false;
56
57 @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses",
58 required = false, multiValued = true)
59 private String[] vmIps = null;
60
61 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070062 protected void doExecute() {
Jian Lic38e9032018-08-09 17:08:38 +090063 OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class);
64 InstancePortService instPortService = get(InstancePortService.class);
65 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
66 MastershipService mastershipService = get(MastershipService.class);
67 ClusterService clusterService = get(ClusterService.class);
68
69 if (tsService == null || osNodeService == null ||
70 instPortService == null || mastershipService == null) {
71 error("Failed to troubleshoot openstack networking.");
72 return;
73 }
74
75 if ((!isAll && vmIps == null) || (isAll && vmIps != null)) {
76 print("Please specify one of VM IP address or -a option.");
77 return;
78 }
79
80 NodeId localNodeId = clusterService.getLocalNode().id();
81
82 for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) {
83 if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) {
Jian Li43d04282018-08-22 14:31:21 +090084 error("Current node is not the master for all gateway nodes. " +
85 "Please enforce mastership first using openstack-reset-mastership -c !");
Jian Lic38e9032018-08-09 17:08:38 +090086 return;
87 }
88 }
89
90 if (isAll) {
91 printHeader();
92
93 // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE
94 instPortService.instancePorts().stream()
95 .filter(p -> p.state() == ACTIVE)
96 .filter(p -> instPortService.floatingIp(p.portId()) != null)
97 .forEach(port -> printReachability(tsService.probeNorthSouth(port)));
98 } else {
99
100 final Set<InstancePort> ports = Sets.newConcurrentHashSet();
101
102 for (String ip : vmIps) {
103 instPortService.instancePorts().stream()
104 .filter(p -> p.state().equals(InstancePort.State.ACTIVE))
105 .filter(p -> instPortService.floatingIp(p.portId()) != null)
106 .filter(p -> ip.equals(instPortService.floatingIp(p.portId()).toString()))
107 .forEach(ports::add);
108 }
109
110 printHeader();
111 ports.forEach(port -> printReachability(tsService.probeNorthSouth(port)));
112 }
113 }
114
115 private void printHeader() {
116 print(FORMAT, "Source IP", "", "Destination IP", "Reachability");
117 }
118
119 private void printReachability(Reachability r) {
120 if (r == null) {
121 return;
122 }
123 String result = r.isReachable() ? REACHABLE : UNREACHABLE;
124 print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result);
125 }
126}