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 static java.util.Collections.emptyList;
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;
24 * It holds all info about an InterVpnLink, combining both configurational
27 public class InterVpnLinkDataComposite {
29 private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkDataComposite.class);
31 private InterVpnLink interVpnLinkCfg;
32 private InterVpnLinkState interVpnLinkState;
34 public InterVpnLinkDataComposite(InterVpnLink interVpnLink) {
35 this.interVpnLinkCfg = interVpnLink;
38 public InterVpnLinkDataComposite(InterVpnLinkState interVpnLinkState) {
39 this.interVpnLinkState = interVpnLinkState;
42 public InterVpnLinkDataComposite(InterVpnLink interVpnLink, InterVpnLinkState interVpnLinkState) {
43 this.interVpnLinkCfg = interVpnLink;
44 this.interVpnLinkState = interVpnLinkState;
47 public InterVpnLink getInterVpnLinkConfig() {
48 return this.interVpnLinkCfg;
51 public void setInterVpnLinkConfig(InterVpnLink interVpnLink) {
52 this.interVpnLinkCfg = interVpnLink;
55 public InterVpnLinkState getInterVpnLinkState() {
56 return this.interVpnLinkState;
59 public void setInterVpnLinkState(InterVpnLinkState interVpnLinkState) {
60 this.interVpnLinkState = interVpnLinkState;
63 public boolean isComplete() {
64 return interVpnLinkCfg != null && interVpnLinkState != null
65 && interVpnLinkState.getFirstEndpointState() != null
66 && interVpnLinkState.getSecondEndpointState() != null;
69 public Optional<InterVpnLinkState.State> getState() {
70 return this.interVpnLinkState == null ? Optional.absent()
71 : Optional.fromNullable(this.interVpnLinkState.getState());
74 public boolean isActive() {
75 return isComplete() && getState().isPresent() && getState().get() == InterVpnLinkState.State.Active;
78 public boolean stepsOnDpn(BigInteger dpnId) {
79 return getFirstEndpointDpns().contains(dpnId) || getSecondEndpointDpns().contains(dpnId);
82 public boolean isBgpRoutesLeaking() {
83 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isBgpRoutesLeaking();
86 public boolean isStaticRoutesLeaking() {
87 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isStaticRoutesLeaking();
90 public boolean isConnectedRoutesLeaking() {
91 return this.interVpnLinkCfg != null && this.interVpnLinkCfg.isConnectedRoutesLeaking();
94 public boolean isFirstEndpointVpnName(String vpnName) {
95 return interVpnLinkCfg != null
96 && interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue().equals(vpnName);
99 public boolean isSecondEndpointVpnName(String vpnName) {
100 return interVpnLinkCfg != null
101 && interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue().equals(vpnName);
104 public boolean isVpnLinked(String vpnName) {
105 return isFirstEndpointVpnName(vpnName) || isSecondEndpointVpnName(vpnName);
108 public boolean isFirstEndpointIpAddr(String endpointIp) {
109 return interVpnLinkCfg != null
110 && interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
113 public boolean isSecondEndpointIpAddr(String endpointIp) {
114 return interVpnLinkCfg != null
115 && interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp);
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()));
126 public String getInterVpnLinkName() {
127 return (interVpnLinkCfg != null) ? interVpnLinkCfg.getName() : interVpnLinkState.getInterVpnLinkName();
130 public Optional<String> getFirstEndpointVpnUuid() {
131 if (this.interVpnLinkCfg == null) {
132 return Optional.absent();
134 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue());
137 public Optional<String> getFirstEndpointIpAddr() {
138 if (this.interVpnLinkCfg == null) {
139 return Optional.absent();
141 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue());
144 public Optional<Long> getFirstEndpointLportTag() {
145 return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getLportTag() == null)
147 : Optional.of(this.interVpnLinkState.getFirstEndpointState().getLportTag());
150 public List<BigInteger> getFirstEndpointDpns() {
151 return (!isComplete() || this.interVpnLinkState.getFirstEndpointState().getDpId() == null)
153 : this.interVpnLinkState.getFirstEndpointState().getDpId();
156 public Optional<String> getSecondEndpointVpnUuid() {
158 return Optional.absent();
160 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue());
163 public Optional<String> getSecondEndpointIpAddr() {
165 return Optional.absent();
167 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue());
170 public Optional<Long> getSecondEndpointLportTag() {
171 return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getLportTag() == null)
173 : Optional.of(this.interVpnLinkState.getSecondEndpointState().getLportTag());
176 public List<BigInteger> getSecondEndpointDpns() {
177 return (!isComplete() || this.interVpnLinkState.getSecondEndpointState().getDpId() == null)
179 : this.interVpnLinkState.getSecondEndpointState().getDpId();
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());
189 return isFirstEndpointIpAddr(endpointIpAddr) ? getFirstEndpointVpnUuid().orNull()
190 : getSecondEndpointVpnUuid().orNull();
194 public String getOtherEndpoint(String vpnUuid) {
195 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
196 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
200 return isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr().orNull()
201 : getFirstEndpointIpAddr().orNull();
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());
211 return isFirstEndpointIpAddr(endpointIpAddr) ? getSecondEndpointVpnUuid().orNull()
212 : getFirstEndpointVpnUuid().orNull();
215 public Optional<Long> getEndpointLportTagByVpnName(String vpnName) {
217 return Optional.absent();
220 return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag())
221 : Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag());
224 public Optional<Long> getEndpointLportTagByIpAddr(String endpointIp) {
226 return Optional.absent();
229 return isFirstEndpointIpAddr(endpointIp)
230 ? Optional.fromNullable(interVpnLinkState.getFirstEndpointState().getLportTag())
231 : Optional.fromNullable(interVpnLinkState.getSecondEndpointState().getLportTag());
234 public Optional<Long> getOtherEndpointLportTagByVpnName(String vpnName) {
236 return Optional.absent();
239 return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag())
240 : Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag());
244 public String getOtherVpnName(String vpnName) {
245 if (!isFirstEndpointVpnName(vpnName) && !isSecondEndpointVpnName(vpnName)) {
246 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnName, getInterVpnLinkName());
250 return isFirstEndpointVpnName(vpnName) ? getSecondEndpointVpnUuid().orNull()
251 : getFirstEndpointVpnUuid().orNull();
255 public String getOtherEndpointIpAddr(String vpnUuid) {
256 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
257 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
261 return isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr().orNull()
262 : getFirstEndpointIpAddr().orNull();
266 public String getEndpointIpAddr(String vpnUuid) {
267 if (!isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
268 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
272 return isFirstEndpointVpnName(vpnUuid) ? getFirstEndpointIpAddr().orNull()
273 : getSecondEndpointIpAddr().orNull();
276 public List<BigInteger> getEndpointDpnsByVpnName(String vpnUuid) {
281 List<BigInteger> dpns = isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getFirstEndpointState().getDpId()
282 : interVpnLinkState.getSecondEndpointState().getDpId();
283 return dpns == null ? emptyList() : dpns;
286 public List<BigInteger> getOtherEndpointDpnsByVpnName(String vpnUuid) {
291 List<BigInteger> dpns = isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getSecondEndpointState().getDpId()
292 : interVpnLinkState.getFirstEndpointState().getDpId();
293 return dpns == null ? emptyList() : dpns;
296 public List<BigInteger> getEndpointDpnsByIpAddr(String endpointIp) {
301 List<BigInteger> dpns =
302 isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getFirstEndpointState().getDpId()
303 : this.interVpnLinkState.getSecondEndpointState().getDpId();
304 return dpns == null ? emptyList() : dpns;
307 public List<BigInteger> getOtherEndpointDpnsByIpAddr(String endpointIp) {
312 List<BigInteger> dpns =
313 isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getSecondEndpointState().getDpId()
314 : this.interVpnLinkState.getFirstEndpointState().getDpId();
315 return dpns == null ? emptyList() : dpns;
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() + "]";
328 public boolean equals(Object obj) {
335 if (getClass() != obj.getClass()) {
338 InterVpnLinkDataComposite other = (InterVpnLinkDataComposite) obj;
340 return getInterVpnLinkName().equals(other.getInterVpnLinkName())
341 && getFirstEndpointVpnUuid().or(none).equals(other.getFirstEndpointVpnUuid().or(none))
342 && getFirstEndpointIpAddr().or(none).equals(other.getFirstEndpointIpAddr().or(none));
346 public int hashCode() {
347 return Objects.hash(this.interVpnLinkCfg, this.interVpnLinkState);