2 * Copyright (c) 2016 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 java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import com.google.common.base.Optional;
22 * It holds all info about an InterVpnLink, combining both configurational
25 public class InterVpnLinkDataComposite {
27 private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkDataComposite.class);
29 private InterVpnLink interVpnLinkCfg;
30 private InterVpnLinkState interVpnLinkState;
32 public InterVpnLinkDataComposite(InterVpnLink iVpnLink) {
33 this.interVpnLinkCfg = iVpnLink;
36 public InterVpnLinkDataComposite(InterVpnLinkState iVpnLinkState) {
37 this.interVpnLinkState = iVpnLinkState;
40 public InterVpnLinkDataComposite(InterVpnLink iVpnLink, InterVpnLinkState iVpnLinkState) {
41 this.interVpnLinkCfg = iVpnLink;
42 this.interVpnLinkState = iVpnLinkState;
45 public InterVpnLink getInterVpnLinkConfig() {
46 return this.interVpnLinkCfg;
49 public void setInterVpnLinkConfig(InterVpnLink iVpnLink) {
50 this.interVpnLinkCfg = iVpnLink;
53 public InterVpnLinkState getInterVpnLinkState() {
54 return this.interVpnLinkState;
57 public void setInterVpnLinkState(InterVpnLinkState iVpnLinkState) {
58 this.interVpnLinkState = iVpnLinkState;
61 public boolean isComplete() {
62 return interVpnLinkCfg != null && interVpnLinkState != null;
65 public Optional<InterVpnLinkState.State> getState() {
66 return this.interVpnLinkState == null ? Optional.absent()
67 : Optional.of(this.interVpnLinkState.getState());
70 public boolean isActive() {
71 return isComplete() && getState().isPresent() && getState().get() == InterVpnLinkState.State.Active;
74 public boolean isFirstEndpointVpnName(String vpnName) {
75 return interVpnLinkCfg != null
76 && interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue().equals(vpnName);
79 public boolean isSecondEndpointVpnName(String vpnName) {
80 return interVpnLinkCfg != null
81 && interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue().equals(vpnName);
84 public boolean isFirstEndpointIpAddr(String endpointIp) {
85 return interVpnLinkCfg != null
86 && interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
89 public boolean isSecondEndpointIpAddr(String endpointIp) {
90 return interVpnLinkCfg != null
91 &&interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp);
94 public boolean isIpAddrTheOtherVpnEndpoint(String ipAddr, String vpnUuid) {
95 return (vpnUuid.equals(getFirstEndpointVpnUuid().orNull())
96 && ipAddr.equals(getSecondEndpointIpAddr().orNull()))
97 || ( vpnUuid.equals(getSecondEndpointVpnUuid().orNull())
98 && ipAddr.equals(getFirstEndpointIpAddr().orNull() ) );
101 public String getInterVpnLinkName() {
102 return (interVpnLinkCfg != null) ? interVpnLinkCfg.getName() : interVpnLinkState.getInterVpnLinkName();
105 public Optional<String> getFirstEndpointVpnUuid() {
106 if ( this.interVpnLinkCfg == null ) {
107 return Optional.absent();
109 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue());
112 public Optional<String> getFirstEndpointIpAddr() {
113 if ( this.interVpnLinkCfg == null ) {
114 return Optional.absent();
116 return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue());
119 public Optional<String> getSecondEndpointVpnUuid() {
120 if ( !isComplete() ) {
121 return Optional.absent();
123 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue());
126 public Optional<String> getSecondEndpointIpAddr() {
127 if ( !isComplete() ) {
128 return Optional.absent();
130 return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue());
133 public Optional<Long> getEndpointLportTagByIpAddr(String endpointIp) {
134 if ( !isComplete() ) {
135 return Optional.absent();
138 return isFirstEndpointIpAddr(endpointIp) ? Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag())
139 : Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag());
142 public Optional<Long> getOtherEndpointLportTagByVpnName(String vpnName) {
143 if ( !isComplete() ) {
144 return Optional.absent();
147 return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag())
148 : Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag());
151 public String getOtherEndpoint(String vpnUuid) {
152 if ( !isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
153 LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
157 Optional<String> optEndpointIpAddr = isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr()
158 : getFirstEndpointIpAddr();
159 return optEndpointIpAddr.orNull();
162 public List<BigInteger> getEndpointDpnsByVpnName(String vpnUuid) {
163 List<BigInteger> result = new ArrayList<>();
164 if ( !isComplete()) {
168 return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getFirstEndpointState().getDpId()
169 : interVpnLinkState.getSecondEndpointState().getDpId();
172 public List<BigInteger> getOtherEndpointDpnsByVpnName(String vpnUuid) {
173 List<BigInteger> result = new ArrayList<>();
174 if ( !isComplete()) {
178 return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getSecondEndpointState().getDpId()
179 : interVpnLinkState.getFirstEndpointState().getDpId();
182 public List<BigInteger> getEndpointDpnsByIpAddr(String endpointIp) {
183 List<BigInteger> result = new ArrayList<>();
184 if ( !isComplete()) {
188 return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getFirstEndpointState().getDpId()
189 : this.interVpnLinkState.getSecondEndpointState().getDpId();
192 public List<BigInteger> getOtherEndpointDpnsByIpAddr(String endpointIp) {
193 List<BigInteger> result = new ArrayList<>();
194 if ( !isComplete()) {
198 return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getSecondEndpointState().getDpId()
199 : this.interVpnLinkState.getFirstEndpointState().getDpId();