2 * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.vpnmanager.api.intervpnlink;
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;
23 * It holds all info about an InterVpnLink, combining both configurational
26 public class InterVpnLinkDataComposite {
28 private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkDataComposite.class);
30 private InterVpnLink interVpnLinkCfg;
31 private InterVpnLinkState interVpnLinkState;
33 public InterVpnLinkDataComposite(InterVpnLink interVpnLink) {
34 this.interVpnLinkCfg = interVpnLink;
37 public InterVpnLinkDataComposite(InterVpnLinkState interVpnLinkState) {
38 this.interVpnLinkState = interVpnLinkState;
41 public InterVpnLinkDataComposite(InterVpnLink interVpnLink, InterVpnLinkState interVpnLinkState) {
42 this.interVpnLinkCfg = interVpnLink;
43 this.interVpnLinkState = interVpnLinkState;
46 public InterVpnLink getInterVpnLinkConfig() {
47 return this.interVpnLinkCfg;
50 public void setInterVpnLinkConfig(InterVpnLink interVpnLink) {
51 this.interVpnLinkCfg = interVpnLink;
54 public InterVpnLinkState getInterVpnLinkState() {
55 return this.interVpnLinkState;
58 public void setInterVpnLinkState(InterVpnLinkState interVpnLinkState) {
59 this.interVpnLinkState = interVpnLinkState;
62 public boolean isComplete() {
63 return interVpnLinkCfg != null && interVpnLinkState != null
64 && interVpnLinkState.getFirstEndpointState() != null
65 && interVpnLinkState.getSecondEndpointState() != null;
68 public Optional<InterVpnLinkState.State> getState() {
69 return this.interVpnLinkState == null ? Optional.absent()
70 : Optional.fromNullable(this.interVpnLinkState.getState());
73 public boolean isActive() {
74 return isComplete() && getState().isPresent() && getState().get() == InterVpnLinkState.State.Active;
77 public boolean stepsOnDpn(BigInteger dpnId) {
78 return getFirstEndpointDpns().contains(dpnId) || getSecondEndpointDpns().contains(dpnId);
81 public boolean isBgpRoutesLeaking() {
82 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isBgpRoutesLeaking();
85 public boolean isStaticRoutesLeaking() {
86 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isStaticRoutesLeaking();
89 public boolean isConnectedRoutesLeaking() {
90 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isConnectedRoutesLeaking();
93 public boolean isFirstEndpointVpnName(String vpnName) {
94 return interVpnLinkCfg != null
95 && interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue().equals(vpnName);
98 public boolean isSecondEndpointVpnName(String vpnName) {
99 return interVpnLinkCfg != null
100 && interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue().equals(vpnName);
103 public boolean isVpnLinked(String vpnName) {
104 return isFirstEndpointVpnName(vpnName) || isSecondEndpointVpnName(vpnName);
107 public boolean isFirstEndpointIpAddr(String endpointIp) {
108 return interVpnLinkCfg != null
109 && interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
112 public boolean isSecondEndpointIpAddr(String endpointIp) {
113 return interVpnLinkCfg != null
114 && interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp);
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()));
124 public String getInterVpnLinkName() {
125 return (interVpnLinkCfg != null) ? interVpnLinkCfg.getName() : interVpnLinkState.getInterVpnLinkName();
128 public Optional<String> getFirstEndpointVpnUuid() {
129 if (this.interVpnLinkCfg == null) {
130 return Optional.absent();
132 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue());
135 public Optional<String> getFirstEndpointIpAddr() {
136 if (this.interVpnLinkCfg == null) {
137 return Optional.absent();
139 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue());
142 public Optional<Long> getFirstEndpointLportTag() {
143 return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getLportTag() == null)
145 : Optional.of(this.interVpnLinkState.getFirstEndpointState().getLportTag());
148 public List<BigInteger> getFirstEndpointDpns() {
149 return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getDpId() == null)
150 ? Collections.emptyList()
151 : this.interVpnLinkState.getFirstEndpointState().getDpId();
154 public Optional<String> getSecondEndpointVpnUuid() {
156 return Optional.absent();
158 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue());
161 public Optional<String> getSecondEndpointIpAddr() {
163 return Optional.absent();
165 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue());
168 public Optional<Long> getSecondEndpointLportTag() {
169 return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getLportTag() == null)
171 : Optional.of(this.interVpnLinkState.getSecondEndpointState().getLportTag());
174 public List<BigInteger> getSecondEndpointDpns() {
175 return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getDpId() == null)
176 ? Collections.emptyList()
177 : this.interVpnLinkState.getSecondEndpointState().getDpId();
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());
186 return isFirstEndpointIpAddr(endpointIpAddr) ? getFirstEndpointVpnUuid().get()
187 : getSecondEndpointVpnUuid().get();
190 public String getOtherEndpoint(String vpnUuid) {
191 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
192 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
196 Optional<String> optEndpointIpAddr = isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr()
197 : getFirstEndpointIpAddr();
198 return optEndpointIpAddr.orNull();
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());
207 return isFirstEndpointIpAddr(endpointIpAddr) ? getSecondEndpointVpnUuid().get()
208 : getFirstEndpointVpnUuid().get();
211 public Optional<Long> getEndpointLportTagByVpnName(String vpnName) {
213 return Optional.absent();
216 return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag())
217 : Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag());
220 public Optional<Long> getEndpointLportTagByIpAddr(String endpointIp) {
222 return Optional.absent();
225 return isFirstEndpointIpAddr(endpointIp)
226 ? Optional.fromNullable(interVpnLinkState.getFirstEndpointState().getLportTag())
227 : Optional.fromNullable(interVpnLinkState.getSecondEndpointState().getLportTag());
230 public Optional<Long> getOtherEndpointLportTagByVpnName(String vpnName) {
232 return Optional.absent();
235 return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag())
236 : Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag());
239 public String getOtherVpnName(String vpnName) {
240 if (!isFirstEndpointVpnName(vpnName) && !isSecondEndpointVpnName(vpnName)) {
241 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnName, getInterVpnLinkName());
245 Optional<String> optOtherVpnName = isFirstEndpointVpnName(vpnName) ? getSecondEndpointVpnUuid()
246 : getFirstEndpointVpnUuid();
247 return optOtherVpnName.orNull();
250 public String getOtherEndpointIpAddr(String vpnUuid) {
251 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
252 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
256 Optional<String> optEndpointIpAddr = isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr()
257 : getFirstEndpointIpAddr();
258 return optEndpointIpAddr.orNull();
261 public String getEndpointIpAddr(String vpnUuid) {
262 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
263 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
267 Optional<String> optEndpointIpAddr = isFirstEndpointVpnName(vpnUuid) ? getFirstEndpointIpAddr()
268 : getSecondEndpointIpAddr();
269 return optEndpointIpAddr.orNull();
272 public List<BigInteger> getEndpointDpnsByVpnName(String vpnUuid) {
274 return new ArrayList<>();
277 return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getFirstEndpointState().getDpId()
278 : interVpnLinkState.getSecondEndpointState().getDpId();
281 public List<BigInteger> getOtherEndpointDpnsByVpnName(String vpnUuid) {
282 List<BigInteger> result = new ArrayList<>();
287 return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getSecondEndpointState().getDpId()
288 : interVpnLinkState.getFirstEndpointState().getDpId();
291 public List<BigInteger> getEndpointDpnsByIpAddr(String endpointIp) {
292 List<BigInteger> result = new ArrayList<>();
297 return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getFirstEndpointState().getDpId()
298 : this.interVpnLinkState.getSecondEndpointState().getDpId();
301 public List<BigInteger> getOtherEndpointDpnsByIpAddr(String endpointIp) {
302 List<BigInteger> result = new ArrayList<>();
307 return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getSecondEndpointState().getDpId()
308 : this.interVpnLinkState.getFirstEndpointState().getDpId();
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() + "]";
321 public boolean equals(Object obj) {
328 if (getClass() != obj.getClass()) {
331 InterVpnLinkDataComposite other = (InterVpnLinkDataComposite) obj;
333 return getInterVpnLinkName().equals(other.getInterVpnLinkName())
334 && getFirstEndpointVpnUuid().or(none).equals(other.getFirstEndpointVpnUuid().or(none))
335 && getFirstEndpointIpAddr().or(none).equals(other.getFirstEndpointIpAddr().or(none));
339 public int hashCode() {
340 return Objects.hash(this.interVpnLinkCfg, this.interVpnLinkState);