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