Bump odlparent->6.0.0,mdsal->5.0.3
[netvirt.git] / vpnmanager / api / src / main / java / org / opendaylight / netvirt / vpnmanager / api / intervpnlink / InterVpnLinkDataComposite.java
1 /*
2  * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.vpnmanager.api.intervpnlink;
10
11 import static java.util.Collections.emptyList;
12
13 import com.google.common.base.Optional;
14 import java.math.BigInteger;
15 import java.util.List;
16 import java.util.Objects;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.common.Uint64;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * It holds all info about an InterVpnLink, combining both configurational
27  * and stateful.
28  */
29 public class InterVpnLinkDataComposite {
30
31     private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkDataComposite.class);
32
33     private InterVpnLink interVpnLinkCfg;
34     private InterVpnLinkState interVpnLinkState;
35
36     public InterVpnLinkDataComposite(InterVpnLink interVpnLink) {
37         this.interVpnLinkCfg = interVpnLink;
38     }
39
40     public InterVpnLinkDataComposite(InterVpnLinkState interVpnLinkState) {
41         this.interVpnLinkState = interVpnLinkState;
42     }
43
44     public InterVpnLinkDataComposite(InterVpnLink interVpnLink, InterVpnLinkState interVpnLinkState) {
45         this.interVpnLinkCfg = interVpnLink;
46         this.interVpnLinkState = interVpnLinkState;
47     }
48
49     public InterVpnLink getInterVpnLinkConfig() {
50         return this.interVpnLinkCfg;
51     }
52
53     public void setInterVpnLinkConfig(InterVpnLink interVpnLink) {
54         this.interVpnLinkCfg = interVpnLink;
55     }
56
57     public InterVpnLinkState getInterVpnLinkState() {
58         return this.interVpnLinkState;
59     }
60
61     public void setInterVpnLinkState(InterVpnLinkState interVpnLinkState) {
62         this.interVpnLinkState = interVpnLinkState;
63     }
64
65     public boolean isComplete() {
66         return interVpnLinkCfg != null && interVpnLinkState != null
67                  && interVpnLinkState.getFirstEndpointState() != null
68                  && interVpnLinkState.getSecondEndpointState() != null;
69     }
70
71     public Optional<InterVpnLinkState.State> getState() {
72         return this.interVpnLinkState == null ? Optional.absent()
73                                               : Optional.fromNullable(this.interVpnLinkState.getState());
74     }
75
76     public boolean isActive() {
77         return isComplete() && getState().isPresent() && getState().get() == InterVpnLinkState.State.Active;
78     }
79
80     public boolean stepsOnDpn(BigInteger dpnId) {
81         return getFirstEndpointDpns().contains(Uint64.valueOf(dpnId))
82                     || getSecondEndpointDpns().contains(Uint64.valueOf(dpnId));
83     }
84
85     public boolean isBgpRoutesLeaking() {
86         return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isBgpRoutesLeaking();
87     }
88
89     public boolean isStaticRoutesLeaking() {
90         return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isStaticRoutesLeaking();
91     }
92
93     public boolean isConnectedRoutesLeaking() {
94         return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isConnectedRoutesLeaking();
95     }
96
97     public boolean isFirstEndpointVpnName(String vpnName) {
98         return interVpnLinkCfg != null
99                && interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue().equals(vpnName);
100     }
101
102     public boolean isSecondEndpointVpnName(String vpnName) {
103         return interVpnLinkCfg != null
104                && interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue().equals(vpnName);
105     }
106
107     public boolean isVpnLinked(String vpnName) {
108         return isFirstEndpointVpnName(vpnName) || isSecondEndpointVpnName(vpnName);
109     }
110
111     public boolean isFirstEndpointIpAddr(String endpointIp) {
112         return interVpnLinkCfg != null
113                && interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
114     }
115
116     public boolean isSecondEndpointIpAddr(String endpointIp) {
117         return interVpnLinkCfg != null
118                && interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp);
119     }
120
121     public boolean isIpAddrTheOtherVpnEndpoint(String ipAddr, String vpnUuid) {
122         return (vpnUuid.equals(getFirstEndpointVpnUuid().orNull())
123                     && ipAddr.equals(getSecondEndpointIpAddr().orNull()))
124                || (vpnUuid.equals(getSecondEndpointVpnUuid().orNull())
125                      && ipAddr.equals(getFirstEndpointIpAddr().orNull()));
126     }
127
128     @Nullable
129     public String getInterVpnLinkName() {
130         return (interVpnLinkCfg != null) ? interVpnLinkCfg.getName() : interVpnLinkState.getInterVpnLinkName();
131     }
132
133     public Optional<String> getFirstEndpointVpnUuid() {
134         if (this.interVpnLinkCfg == null) {
135             return Optional.absent();
136         }
137         return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue());
138     }
139
140     public Optional<String> getFirstEndpointIpAddr() {
141         if (this.interVpnLinkCfg == null) {
142             return Optional.absent();
143         }
144         return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue());
145     }
146
147     public Optional<Uint32> getFirstEndpointLportTag() {
148         return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getLportTag() == null)
149                    ? Optional.absent()
150                    : Optional.of(this.interVpnLinkState.getFirstEndpointState().getLportTag());
151     }
152
153     public List<Uint64> getFirstEndpointDpns() {
154         return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getDpId() == null)
155                    ? emptyList()
156                    : this.interVpnLinkState.getFirstEndpointState().getDpId();
157     }
158
159     public Optional<String> getSecondEndpointVpnUuid() {
160         if (!isComplete()) {
161             return Optional.absent();
162         }
163         return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue());
164     }
165
166     public Optional<String> getSecondEndpointIpAddr() {
167         if (!isComplete()) {
168             return Optional.absent();
169         }
170         return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue());
171     }
172
173     public Optional<Uint32> getSecondEndpointLportTag() {
174         return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getLportTag() == null)
175             ? Optional.absent()
176             : Optional.of(this.interVpnLinkState.getSecondEndpointState().getLportTag());
177     }
178
179     public List<Uint64> getSecondEndpointDpns() {
180         return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getDpId() == null)
181                     ? emptyList()
182                     : this.interVpnLinkState.getSecondEndpointState().getDpId();
183     }
184
185     @Nullable
186     public String getVpnNameByIpAddress(String endpointIpAddr) {
187         if (!isFirstEndpointIpAddr(endpointIpAddr) && !isSecondEndpointIpAddr(endpointIpAddr)) {
188             LOG.debug("Endpoint IpAddress {} does not participate in InterVpnLink {}",
189                       endpointIpAddr, getInterVpnLinkName());
190             return null;
191         }
192         return isFirstEndpointIpAddr(endpointIpAddr) ? getFirstEndpointVpnUuid().orNull()
193                                                      : getSecondEndpointVpnUuid().orNull();
194     }
195
196     @Nullable
197     public String getOtherEndpoint(String vpnUuid) {
198         if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
199             LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
200             return null;
201         }
202
203         return isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr().orNull()
204                                                : getFirstEndpointIpAddr().orNull();
205     }
206
207     @Nullable
208     public String getOtherVpnNameByIpAddress(String endpointIpAddr) {
209         if (!isFirstEndpointIpAddr(endpointIpAddr) && !isSecondEndpointIpAddr(endpointIpAddr)) {
210             LOG.debug("Endpoint IpAddress {} does not participate in InterVpnLink {}",
211                       endpointIpAddr, getInterVpnLinkName());
212             return null;
213         }
214         return isFirstEndpointIpAddr(endpointIpAddr) ? getSecondEndpointVpnUuid().orNull()
215                                                      : getFirstEndpointVpnUuid().orNull();
216     }
217
218     public Optional<Uint32> getEndpointLportTagByVpnName(String vpnName) {
219         if (!isComplete()) {
220             return Optional.absent();
221         }
222
223         return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag())
224                                                : Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag());
225     }
226
227     public Optional<Uint32> getEndpointLportTagByIpAddr(String endpointIp) {
228         if (!isComplete()) {
229             return Optional.absent();
230         }
231
232         return isFirstEndpointIpAddr(endpointIp)
233                     ? Optional.fromNullable(interVpnLinkState.getFirstEndpointState().getLportTag())
234                     : Optional.fromNullable(interVpnLinkState.getSecondEndpointState().getLportTag());
235     }
236
237     public Optional<Uint32> getOtherEndpointLportTagByVpnName(String vpnName) {
238         if (!isComplete()) {
239             return Optional.absent();
240         }
241
242         return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag())
243                                                : Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag());
244     }
245
246     @Nullable
247     public String getOtherVpnName(String vpnName) {
248         if (!isFirstEndpointVpnName(vpnName) && !isSecondEndpointVpnName(vpnName)) {
249             LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnName, getInterVpnLinkName());
250             return null;
251         }
252
253         return isFirstEndpointVpnName(vpnName) ? getSecondEndpointVpnUuid().orNull()
254                                                : getFirstEndpointVpnUuid().orNull();
255     }
256
257     @Nullable
258     public String getOtherEndpointIpAddr(String vpnUuid) {
259         if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
260             LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
261             return null;
262         }
263
264         return isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr().orNull()
265                                                : getFirstEndpointIpAddr().orNull();
266     }
267
268     @Nullable
269     public String getEndpointIpAddr(String vpnUuid) {
270         if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
271             LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
272             return null;
273         }
274
275         return isFirstEndpointVpnName(vpnUuid) ? getFirstEndpointIpAddr().orNull()
276                                                : getSecondEndpointIpAddr().orNull();
277     }
278
279     public List<Uint64> getEndpointDpnsByVpnName(String vpnUuid) {
280         if (!isComplete()) {
281             return emptyList();
282         }
283
284         List<Uint64> dpns = isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getFirstEndpointState().getDpId()
285                                                                 : interVpnLinkState.getSecondEndpointState().getDpId();
286         return dpns == null ? emptyList() : dpns;
287     }
288
289     public List<Uint64> getOtherEndpointDpnsByVpnName(String vpnUuid) {
290         if (!isComplete()) {
291             return emptyList();
292         }
293
294         List<Uint64> dpns = isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getSecondEndpointState().getDpId()
295                                                                 : interVpnLinkState.getFirstEndpointState().getDpId();
296         return dpns == null ? emptyList() : dpns;
297     }
298
299     public List<Uint64> getEndpointDpnsByIpAddr(String endpointIp) {
300         if (!isComplete()) {
301             return emptyList();
302         }
303
304         List<Uint64> dpns =
305             isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getFirstEndpointState().getDpId()
306                                               : this.interVpnLinkState.getSecondEndpointState().getDpId();
307         return dpns == null ? emptyList() : dpns;
308     }
309
310     public List<Uint64> getOtherEndpointDpnsByIpAddr(String endpointIp) {
311         if (!isComplete()) {
312             return emptyList();
313         }
314
315         List<Uint64> dpns =
316             isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getSecondEndpointState().getDpId()
317                                               : this.interVpnLinkState.getFirstEndpointState().getDpId();
318         return dpns == null ? emptyList() : dpns;
319     }
320
321     @Override
322     public String toString() {
323         final String ns = "Not specified";
324         return "InterVpnLink " + getInterVpnLinkName() + " 1stEndpoint=[vpn=" + getFirstEndpointVpnUuid().or(ns)
325             + " ipAddr=" + getFirstEndpointIpAddr().or(ns) + " dpn=" + getFirstEndpointDpns() + "]  2ndEndpoint=[vpn="
326             + getSecondEndpointVpnUuid().or(ns) + " ipAddr=" + getSecondEndpointIpAddr().or(ns) + " dpn="
327             + getSecondEndpointDpns() + "]";
328     }
329
330     @Override
331     public boolean equals(Object obj) {
332         if (this == obj) {
333             return true;
334         }
335         if (obj == null) {
336             return false;
337         }
338         if (getClass() != obj.getClass()) {
339             return false;
340         }
341         InterVpnLinkDataComposite other = (InterVpnLinkDataComposite) obj;
342         String none = "";
343         return getInterVpnLinkName().equals(other.getInterVpnLinkName())
344             && getFirstEndpointVpnUuid().or(none).equals(other.getFirstEndpointVpnUuid().or(none))
345             && getFirstEndpointIpAddr().or(none).equals(other.getFirstEndpointIpAddr().or(none));
346     }
347
348     @Override
349     public int hashCode() {
350         return Objects.hash(this.interVpnLinkCfg, this.interVpnLinkState);
351     }
352 }