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