blob: bf5c700ee7ff632355e0af0117c4c6073e1895a4 [file] [log] [blame]
Jonathan Hart313fdf02014-04-10 14:09:46 -07001package net.onrc.onos.core.packetservice;
Jonathan Hart7804bea2014-01-07 10:50:52 -08002
Jonathan Harte6e63732014-04-16 14:29:49 -07003import java.util.Map;
TeruU7feef8a2014-04-03 00:15:49 -07004
Jonathan Hartf5bd2582014-04-09 17:43:41 -07005import net.onrc.onos.core.topology.NetworkGraph;
6import net.onrc.onos.core.topology.Port;
7
Jonathan Harte6e63732014-04-16 14:29:49 -07008import com.google.common.collect.HashMultimap;
9import com.google.common.collect.Multimap;
10
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070011// TODO The generic broadcast packet shouldn't contain an IP address which is
12// only for ARP packets.
Ray Milkey269ffb92014-04-03 14:43:30 -070013
Jonathan Hart7804bea2014-01-07 10:50:52 -080014/**
15 * Notification to all ONOS instances to broadcast this packet out the edge of
16 * the network. The edge is defined as any port that doesn't have a link to
17 * another switch. The one exception is the port that the packet was received
18 * on.
Jonathan Hart7804bea2014-01-07 10:50:52 -080019 */
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070020public class BroadcastPacketOutNotification extends PacketOutNotification {
Jonathan Hart7804bea2014-01-07 10:50:52 -080021
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070022 private static final long serialVersionUID = 1L;
Jonathan Hart7804bea2014-01-07 10:50:52 -080023
TeruU7feef8a2014-04-03 00:15:49 -070024 private final int address;
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070025 private final long inSwitch;
26 private final short inPort;
Jonathan Hart7804bea2014-01-07 10:50:52 -080027
TeruU7feef8a2014-04-03 00:15:49 -070028 protected BroadcastPacketOutNotification() {
Ray Milkey269ffb92014-04-03 14:43:30 -070029 super();
TeruU7feef8a2014-04-03 00:15:49 -070030 this.address = -1;
31 this.inSwitch = -1;
32 this.inPort = -1;
33 }
Ray Milkey269ffb92014-04-03 14:43:30 -070034
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070035 /**
36 * Class constructor.
37 *
Ray Milkey269ffb92014-04-03 14:43:30 -070038 * @param packet packet data to send in the packet-out
39 * @param address target IP address if the packet is an ARP packet
40 * @param inSwitch dpid of the switch the packet was received on
41 * @param inPort port number of the receiving port
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070042 */
TeruU7feef8a2014-04-03 00:15:49 -070043 public BroadcastPacketOutNotification(byte[] packet, int address,
Ray Milkey269ffb92014-04-03 14:43:30 -070044 long inSwitch, short inPort) {
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070045 super(packet);
Jonathan Hart7804bea2014-01-07 10:50:52 -080046
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070047 this.address = address;
48 this.inSwitch = inSwitch;
49 this.inPort = inPort;
50 }
Ray Milkey269ffb92014-04-03 14:43:30 -070051
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070052 /**
53 * Get the dpid of the switch the packet was received on.
54 *
55 * @return receiving switch dpid
56 */
57 public long getInSwitch() {
58 return inSwitch;
59 }
60
61 /**
62 * Get the port number of the port the packet was received on.
63 *
64 * @return receiving port number
65 */
66 public short getInPort() {
67 return inPort;
68 }
69
70 /**
71 * Get the target IP address if the packet is an ARP packet.
72 *
73 * @return the target IP address for ARP packets, or null if the packet is
Ray Milkey269ffb92014-04-03 14:43:30 -070074 * not an ARP packet
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070075 */
TeruU7feef8a2014-04-03 00:15:49 -070076 public int getTargetAddress() {
Jonathan Hart7c9a2fb2014-03-27 09:51:41 -070077 return address;
78 }
Jonathan Harte6e63732014-04-16 14:29:49 -070079
80 @Override
Jonathan Hartf5bd2582014-04-09 17:43:41 -070081 public Multimap<Long, Short> calculateOutPorts(
82 Multimap<Long, Short> localPorts, NetworkGraph networkGraph) {
Jonathan Harte6e63732014-04-16 14:29:49 -070083 Multimap<Long, Short> outPorts = HashMultimap.create();
84
Jonathan Hartf5bd2582014-04-09 17:43:41 -070085 for (Map.Entry<Long, Short> entry : localPorts.entries()) {
86 Port globalPort;
87 networkGraph.acquireReadLock();
88 try {
89 globalPort = networkGraph.getPort(entry.getKey(),
90 entry.getValue().longValue());
91 } finally {
92 networkGraph.releaseReadLock();
93 }
94
95 if ((!entry.getKey().equals(inSwitch) ||
96 !entry.getValue().equals(inPort)) &&
97 globalPort != null &&
98 globalPort.getOutgoingLink() == null) {
99
Jonathan Harte6e63732014-04-16 14:29:49 -0700100 outPorts.put(entry.getKey(), entry.getValue());
101 }
102 }
103
104 return outPorts;
105 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800106}