Merge "Use ${project.version} for internal dependencies"
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / arp / ArpResolverMetadata.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services.arp;
9
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;
16
17 /**
18 *
19 * @author Anil Vishnoi (avishnoi@Brocade.com)
20 *
21 */
22
23 public final class ArpResolverMetadata {
24
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;
37
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;
53     }
54
55     public boolean isRefreshExternalNetworkBridgeDpidIfNeeded() {
56         return refreshExternalNetworkBridgeDpidIfNeeded;
57     }
58     public boolean isPeriodicRefresh() {
59         return periodicRefresh;
60     }
61     public RemoveFlowInput getFlowToRemove() {
62         return flowToRemove;
63     }
64     public void setFlowToRemove(RemoveFlowInput flowToRemove) {
65         this.flowToRemove = flowToRemove;
66     }
67     public Ipv4Address getGatewayIpAddress() {
68         return gatewayIpAddress;
69     }
70     public MacAddress getGatewayMacAddress() {
71         return gatewayMacAddress;
72     }
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);
79             }
80             gatewayMacAddressResolved = true;
81             numberOfOutstandingArpRequests = 0;
82         } else {
83             gatewayMacAddressResolved = false;
84         }
85         this.gatewayMacAddress = gatewayMacAddress;
86     }
87
88     public Long getExternalNetworkBridgeDpid() {
89         return externalNetworkBridgeDpid;
90     }
91     public void setExternalNetworkBridgeDpid(Long externalNetworkBridgeDpid) {
92         this.externalNetworkBridgeDpid = externalNetworkBridgeDpid;
93     }
94     public Ipv4Address getArpRequestSourceIp() {
95         return arpRequestSourceIp;
96     }
97     public MacAddress getArpRequestSourceMacAddress() {
98         return arpRequestSourceMacAddress;
99     }
100     public boolean isGatewayMacAddressResolved() {
101         return gatewayMacAddressResolved;
102     }
103
104     /**
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
107      * answered.
108      *
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
113      * than needed.
114      *
115      * @return Destination MAC address to be used in ARP request packet:  Either the unicast MAC or the broadcast MAC
116      * as described above.
117      */
118     public MacAddress getArpRequestDestMacAddress() {
119
120         numberOfOutstandingArpRequests++;
121
122         if (numberOfOutstandingArpRequests > MAX_OUTSTANDING_ARP_REQUESTS) {
123             gatewayMacAddressResolved = false;
124         }
125
126         if (gatewayMacAddressResolved) {
127             return gatewayMacAddress;
128         } else {
129             return ArpUtils.bytesToMac(NetUtils.getBroadcastMACAddr());
130         }
131     }
132
133     @Override
134     public int hashCode() {
135         final int prime = 31;
136         int result = 1;
137
138         result = prime
139                 * result
140                 + ((gatewayMacResolverListener == null) ? 0 : gatewayMacResolverListener.hashCode());
141         result = prime
142                 * result
143                 + ((arpRequestSourceMacAddress == null) ? 0 : arpRequestSourceMacAddress
144                         .hashCode());
145         result = prime
146                 * result
147                 + ((arpRequestSourceIp == null) ? 0 : arpRequestSourceIp
148                         .hashCode());
149         result = prime
150                 * result
151                 + ((externalNetworkBridgeDpid == null) ? 0
152                         : externalNetworkBridgeDpid.hashCode());
153         result = prime
154                 * result
155                 + ((gatewayIpAddress == null) ? 0 : gatewayIpAddress.hashCode());
156         result = prime * result + (periodicRefresh ? 1231 : 1237);
157         result = prime * result + (refreshExternalNetworkBridgeDpidIfNeeded ? 1231 : 1237);
158         return result;
159     }
160
161     @Override
162     public boolean equals(Object obj) {
163         if (this == obj)
164             return true;
165         if (obj == null)
166             return false;
167         if (getClass() != obj.getClass())
168             return false;
169         ArpResolverMetadata other = (ArpResolverMetadata) obj;
170         if (gatewayMacResolverListener == null) {
171             if (other.gatewayMacResolverListener != null)
172                 return false;
173         } else if (!gatewayMacResolverListener.equals(other.gatewayMacResolverListener))
174             return false;
175         if (arpRequestSourceMacAddress == null) {
176             if (other.arpRequestSourceMacAddress != null)
177                 return false;
178         } else if (!arpRequestSourceMacAddress.equals(other.arpRequestSourceMacAddress))
179             return false;
180         if (arpRequestSourceIp == null) {
181             if (other.arpRequestSourceIp != null)
182                 return false;
183         } else if (!arpRequestSourceIp.equals(other.arpRequestSourceIp))
184             return false;
185         if (externalNetworkBridgeDpid == null) {
186             if (other.externalNetworkBridgeDpid != null)
187                 return false;
188         } else if (!externalNetworkBridgeDpid
189                 .equals(other.externalNetworkBridgeDpid))
190             return false;
191         if (gatewayIpAddress == null) {
192             if (other.gatewayIpAddress != null)
193                 return false;
194         } else if (!gatewayIpAddress.equals(other.gatewayIpAddress))
195             return false;
196         if (periodicRefresh != other.periodicRefresh)
197             return false;
198         if (refreshExternalNetworkBridgeDpidIfNeeded != other.refreshExternalNetworkBridgeDpidIfNeeded)
199             return false;
200         return true;
201     }
202
203 }