2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services.arp;
10 import org.opendaylight.controller.liblldp.NetUtils;
11 import org.opendaylight.ovsdb.openstack.netvirt.api.GatewayMacResolverListener;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
19 * @author Anil Vishnoi (avishnoi@Brocade.com)
23 public final class ArpResolverMetadata {
25 private final GatewayMacResolverListener gatewayMacResolverListener;
26 private final Ipv4Address gatewayIpAddress;
27 private Long externalNetworkBridgeDpid;
28 private final boolean refreshExternalNetworkBridgeDpidIfNeeded;
29 private final Ipv4Address arpRequestSourceIp;
30 private final MacAddress arpRequestSourceMacAddress;
31 private final boolean periodicRefresh;
32 private RemoveFlowInput flowToRemove;
33 private MacAddress gatewayMacAddress;
34 private boolean gatewayMacAddressResolved;
35 private int numberOfOutstandingArpRequests;
36 private static final int MAX_OUTSTANDING_ARP_REQUESTS = 2;
38 public ArpResolverMetadata(final GatewayMacResolverListener gatewayMacResolverListener,
39 final Long externalNetworkBridgeDpid,
40 final boolean refreshExternalNetworkBridgeDpidIfNeeded,
41 final Ipv4Address gatewayIpAddress, final Ipv4Address arpRequestSourceIp,
42 final MacAddress arpRequestMacAddress, final boolean periodicRefresh){
43 this.gatewayMacResolverListener = gatewayMacResolverListener;
44 this.externalNetworkBridgeDpid = externalNetworkBridgeDpid;
45 this.refreshExternalNetworkBridgeDpidIfNeeded = refreshExternalNetworkBridgeDpidIfNeeded;
46 this.gatewayIpAddress = gatewayIpAddress;
47 this.arpRequestSourceIp = arpRequestSourceIp;
48 this.arpRequestSourceMacAddress = arpRequestMacAddress;
49 this.periodicRefresh = periodicRefresh;
50 this.gatewayMacAddress = null;
51 this.gatewayMacAddressResolved = false;
52 this.numberOfOutstandingArpRequests = 0;
55 public boolean isRefreshExternalNetworkBridgeDpidIfNeeded() {
56 return refreshExternalNetworkBridgeDpidIfNeeded;
58 public boolean isPeriodicRefresh() {
59 return periodicRefresh;
61 public RemoveFlowInput getFlowToRemove() {
64 public void setFlowToRemove(RemoveFlowInput flowToRemove) {
65 this.flowToRemove = flowToRemove;
67 public Ipv4Address getGatewayIpAddress() {
68 return gatewayIpAddress;
70 public MacAddress getGatewayMacAddress() {
71 return gatewayMacAddress;
73 public void setGatewayMacAddress(MacAddress gatewayMacAddress) {
74 if (gatewayMacAddress != null) {
75 if (gatewayMacResolverListener != null &&
76 !gatewayMacAddress.equals(this.gatewayMacAddress)) {
77 gatewayMacResolverListener.gatewayMacResolved(externalNetworkBridgeDpid,
78 new IpAddress(gatewayIpAddress), gatewayMacAddress);
80 gatewayMacAddressResolved = true;
81 numberOfOutstandingArpRequests = 0;
83 gatewayMacAddressResolved = false;
85 this.gatewayMacAddress = gatewayMacAddress;
88 public Long getExternalNetworkBridgeDpid() {
89 return externalNetworkBridgeDpid;
91 public void setExternalNetworkBridgeDpid(Long externalNetworkBridgeDpid) {
92 this.externalNetworkBridgeDpid = externalNetworkBridgeDpid;
94 public Ipv4Address getArpRequestSourceIp() {
95 return arpRequestSourceIp;
97 public MacAddress getArpRequestSourceMacAddress() {
98 return arpRequestSourceMacAddress;
100 public boolean isGatewayMacAddressResolved() {
101 return gatewayMacAddressResolved;
105 * This method is used to determine whether to use the broadcast MAC or the unicast MAC as the destination address
106 * for an ARP request packet based on whether one of the last MAX_OUTSTANDING_ARP_REQUESTS requests has been
109 * A counter (numberOfOutstandingArpRequests) is maintained to track outstanding ARP requests. This counter is
110 * incremented in this method and reset when setGatewayMacAddress() is called with an updated MAC address after an
111 * ARP reply is received. It is therefore expected that this method be called exactly once for each ARP request
112 * event, and not be called for other reasons, or it may result in more broadcast ARP request packets being sent
115 * @return Destination MAC address to be used in ARP request packet: Either the unicast MAC or the broadcast MAC
116 * as described above.
118 public MacAddress getArpRequestDestMacAddress() {
120 numberOfOutstandingArpRequests++;
122 if (numberOfOutstandingArpRequests > MAX_OUTSTANDING_ARP_REQUESTS) {
123 gatewayMacAddressResolved = false;
126 if (gatewayMacAddressResolved) {
127 return gatewayMacAddress;
129 return ArpUtils.bytesToMac(NetUtils.getBroadcastMACAddr());
134 public int hashCode() {
135 final int prime = 31;
140 + ((gatewayMacResolverListener == null) ? 0 : gatewayMacResolverListener.hashCode());
143 + ((arpRequestSourceMacAddress == null) ? 0 : arpRequestSourceMacAddress
147 + ((arpRequestSourceIp == null) ? 0 : arpRequestSourceIp
151 + ((externalNetworkBridgeDpid == null) ? 0
152 : externalNetworkBridgeDpid.hashCode());
155 + ((gatewayIpAddress == null) ? 0 : gatewayIpAddress.hashCode());
156 result = prime * result + (periodicRefresh ? 1231 : 1237);
157 result = prime * result + (refreshExternalNetworkBridgeDpidIfNeeded ? 1231 : 1237);
162 public boolean equals(Object obj) {
167 if (getClass() != obj.getClass())
169 ArpResolverMetadata other = (ArpResolverMetadata) obj;
170 if (gatewayMacResolverListener == null) {
171 if (other.gatewayMacResolverListener != null)
173 } else if (!gatewayMacResolverListener.equals(other.gatewayMacResolverListener))
175 if (arpRequestSourceMacAddress == null) {
176 if (other.arpRequestSourceMacAddress != null)
178 } else if (!arpRequestSourceMacAddress.equals(other.arpRequestSourceMacAddress))
180 if (arpRequestSourceIp == null) {
181 if (other.arpRequestSourceIp != null)
183 } else if (!arpRequestSourceIp.equals(other.arpRequestSourceIp))
185 if (externalNetworkBridgeDpid == null) {
186 if (other.externalNetworkBridgeDpid != null)
188 } else if (!externalNetworkBridgeDpid
189 .equals(other.externalNetworkBridgeDpid))
191 if (gatewayIpAddress == null) {
192 if (other.gatewayIpAddress != null)
194 } else if (!gatewayIpAddress.equals(other.gatewayIpAddress))
196 if (periodicRefresh != other.periodicRefresh)
198 if (refreshExternalNetworkBridgeDpidIfNeeded != other.refreshExternalNetworkBridgeDpidIfNeeded)