Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.t3.impl; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.onlab.packet.EthType; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.net.host.HostService; |
| 23 | import org.onosproject.t3.api.StaticPacketTrace; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of the generator class that yields a set of Packet Traces. |
| 33 | */ |
| 34 | public class PingAllGenerator extends Generator<Set<StaticPacketTrace>> { |
| 35 | |
| 36 | private static final Logger log = getLogger(PingAllGenerator.class); |
| 37 | |
| 38 | private final EthType.EtherType etherType; |
| 39 | private final HostService hostService; |
| 40 | 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. |
| 46 | * @param service the host service |
| 47 | * @param manager the troubleshoot manager issuing the request. |
| 48 | */ |
| 49 | PingAllGenerator(EthType.EtherType etherType, HostService service, TroubleshootManager manager) { |
| 50 | this.etherType = etherType; |
| 51 | this.hostService = service; |
| 52 | this.manager = manager; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected void run() throws InterruptedException { |
| 57 | hostService.getHosts().forEach(host -> { |
| 58 | 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(); |
| 62 | hostService.getHosts().forEach(hostToPing -> { |
| 63 | 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 | } |