blob: e935fad08af23661fd5579098738565203bd3e6b [file] [log] [blame]
Andrea Campanella41de8062018-02-28 16:43:16 +01001/*
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 */
16
17package org.onosproject.t3.impl;
18
19import com.google.common.collect.Sets;
20import org.onlab.packet.EthType;
21import org.onlab.packet.IpAddress;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080022import org.onosproject.t3.api.HostNib;
Andrea Campanella41de8062018-02-28 16:43:16 +010023import org.onosproject.t3.api.StaticPacketTrace;
24import org.slf4j.Logger;
25
26import java.util.List;
27import java.util.Set;
28
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Implementation of the generator class that yields a set of Packet Traces.
33 */
34public class PingAllGenerator extends Generator<Set<StaticPacketTrace>> {
35
36 private static final Logger log = getLogger(PingAllGenerator.class);
37
38 private final EthType.EtherType etherType;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080039 private final HostNib hostNib;
Andrea Campanella41de8062018-02-28 16:43:16 +010040 private final TroubleshootManager manager;
41
42 /**
43 * Creates a generator for obtaining traces of pings between all the hosts in the network.
44 *
45 * @param etherType the type of traffic we are tracing.
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080046 * @param hostNib the host NIB
Andrea Campanella41de8062018-02-28 16:43:16 +010047 * @param manager the troubleshoot manager issuing the request.
48 */
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080049 PingAllGenerator(EthType.EtherType etherType, HostNib hostNib, TroubleshootManager manager) {
Andrea Campanella41de8062018-02-28 16:43:16 +010050 this.etherType = etherType;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080051 this.hostNib = hostNib;
Andrea Campanella41de8062018-02-28 16:43:16 +010052 this.manager = manager;
53 }
54
55 @Override
56 protected void run() throws InterruptedException {
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080057 hostNib.getHosts().forEach(host -> {
Andrea Campanella41de8062018-02-28 16:43:16 +010058 List<IpAddress> ipAddresses = manager.getIpAddresses(host, etherType, false);
59 if (ipAddresses.size() > 0) {
60 //check if the host has only local IPs of that ETH type
61 boolean onlyLocalSrc = ipAddresses.size() == 1 && ipAddresses.get(0).isLinkLocal();
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080062 hostNib.getHosts().forEach(hostToPing -> {
Andrea Campanella41de8062018-02-28 16:43:16 +010063 List<IpAddress> ipAddressesToPing = manager.getIpAddresses(hostToPing, etherType, false);
64 //check if the other host has only local IPs of that ETH type
65 boolean onlyLocalDst = ipAddressesToPing.size() == 1 && ipAddressesToPing.get(0).isLinkLocal();
66 boolean sameLocation = Sets.intersection(host.locations(), hostToPing.locations()).size() > 0;
67 //Trace is done only if they are both local and under the same location
68 // or not local and if they are not the same host.
69 if (((sameLocation && onlyLocalDst && onlyLocalSrc) ||
70 (!onlyLocalSrc && !onlyLocalDst && ipAddressesToPing.size() > 0))
71 && !host.equals(hostToPing)) {
72 try {
73 yield(manager.trace(host.id(), hostToPing.id(), etherType));
74 } catch (InterruptedException e) {
75 log.warn("Interrupted generator", e.getMessage());
76 log.debug("exception", e);
77 }
78 }
79 });
80 }
81 });
82
83 }
84}