2ffef71fc89b34901f1cd7c8f9ac38d48683fbd0
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-api / src / main / java / org / opendaylight / netvirt / vpnmanager / api / intervpnlink / InterVpnLinkDataComposite.java
1 /*
2  * Copyright (c) 2016 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 java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
20
21 /**
22  * It holds all info about an InterVpnLink, combining both configurational
23  * and stateful
24  */
25 public class InterVpnLinkDataComposite {
26
27     private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkDataComposite.class);
28
29     private InterVpnLink interVpnLinkCfg;
30     private InterVpnLinkState interVpnLinkState;
31
32     public InterVpnLinkDataComposite(InterVpnLink iVpnLink) {
33         this.interVpnLinkCfg = iVpnLink;
34     }
35
36     public InterVpnLinkDataComposite(InterVpnLinkState iVpnLinkState) {
37         this.interVpnLinkState = iVpnLinkState;
38     }
39
40     public InterVpnLinkDataComposite(InterVpnLink iVpnLink, InterVpnLinkState iVpnLinkState) {
41         this.interVpnLinkCfg = iVpnLink;
42         this.interVpnLinkState = iVpnLinkState;
43     }
44
45     public InterVpnLink getInterVpnLinkConfig() {
46         return this.interVpnLinkCfg;
47     }
48
49     public void setInterVpnLinkConfig(InterVpnLink iVpnLink) {
50         this.interVpnLinkCfg = iVpnLink;
51     }
52
53     public InterVpnLinkState getInterVpnLinkState() {
54         return this.interVpnLinkState;
55     }
56
57     public void setInterVpnLinkState(InterVpnLinkState iVpnLinkState) {
58         this.interVpnLinkState = iVpnLinkState;
59     }
60
61     public boolean isComplete() {
62         return interVpnLinkCfg != null && interVpnLinkState != null;
63     }
64
65     public Optional<InterVpnLinkState.State> getState() {
66         return this.interVpnLinkState == null ? Optional.absent()
67                                               : Optional.of(this.interVpnLinkState.getState());
68     }
69
70     public boolean isActive() {
71         return isComplete() && getState().isPresent() && getState().get() == InterVpnLinkState.State.Active;
72     }
73
74     public boolean isFirstEndpointVpnName(String vpnName) {
75         return interVpnLinkCfg != null
76                && interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue().equals(vpnName);
77     }
78
79     public boolean isSecondEndpointVpnName(String vpnName) {
80         return interVpnLinkCfg != null
81                && interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue().equals(vpnName);
82     }
83
84     public boolean isFirstEndpointIpAddr(String endpointIp) {
85         return interVpnLinkCfg != null
86                && interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
87     }
88
89     public boolean isSecondEndpointIpAddr(String endpointIp) {
90         return interVpnLinkCfg != null
91                &&interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp);
92     }
93
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() ) );
99     }
100
101     public String getInterVpnLinkName() {
102         return (interVpnLinkCfg != null) ? interVpnLinkCfg.getName() : interVpnLinkState.getInterVpnLinkName();
103     }
104
105     public Optional<String> getFirstEndpointVpnUuid() {
106         if ( this.interVpnLinkCfg == null ) {
107             return Optional.absent();
108         }
109         return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getVpnUuid().getValue());
110     }
111
112     public Optional<String> getFirstEndpointIpAddr() {
113         if ( this.interVpnLinkCfg == null ) {
114             return Optional.absent();
115         }
116         return Optional.of(this.interVpnLinkCfg.getFirstEndpoint().getIpAddress().getValue());
117     }
118
119     public Optional<String> getSecondEndpointVpnUuid() {
120         if ( !isComplete() ) {
121             return Optional.absent();
122         }
123         return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getVpnUuid().getValue());
124     }
125
126     public Optional<String> getSecondEndpointIpAddr() {
127         if ( !isComplete() ) {
128             return Optional.absent();
129         }
130         return Optional.of(this.interVpnLinkCfg.getSecondEndpoint().getIpAddress().getValue());
131     }
132
133     public Optional<Long> getEndpointLportTagByIpAddr(String endpointIp) {
134         if ( !isComplete() ) {
135             return Optional.absent();
136         }
137
138         return isFirstEndpointIpAddr(endpointIp) ? Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag())
139                                                  : Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag());
140     }
141
142     public Optional<Long> getOtherEndpointLportTagByVpnName(String vpnName) {
143         if ( !isComplete() ) {
144             return Optional.absent();
145         }
146
147         return isFirstEndpointVpnName(vpnName) ? Optional.of(interVpnLinkState.getSecondEndpointState().getLportTag())
148                                                : Optional.of(interVpnLinkState.getFirstEndpointState().getLportTag());
149     }
150
151     public String getOtherEndpoint(String vpnUuid) {
152         if ( !isFirstEndpointVpnName(vpnUuid) && !isSecondEndpointVpnName(vpnUuid)) {
153             LOG.debug("VPN {} does not participate in InterVpnLink {}", vpnUuid, getInterVpnLinkName());
154             return null;
155         }
156
157         Optional<String> optEndpointIpAddr = isFirstEndpointVpnName(vpnUuid) ? getSecondEndpointIpAddr()
158                                                                              : getFirstEndpointIpAddr();
159         return optEndpointIpAddr.orNull();
160     }
161
162     public List<BigInteger> getEndpointDpnsByVpnName(String vpnUuid) {
163         List<BigInteger> result = new ArrayList<>();
164         if ( !isComplete()) {
165             return result;
166         }
167
168         return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getFirstEndpointState().getDpId()
169                                                : interVpnLinkState.getSecondEndpointState().getDpId();
170     }
171
172     public List<BigInteger> getOtherEndpointDpnsByVpnName(String vpnUuid) {
173         List<BigInteger> result = new ArrayList<>();
174         if ( !isComplete()) {
175             return result;
176         }
177
178         return isFirstEndpointVpnName(vpnUuid) ? interVpnLinkState.getSecondEndpointState().getDpId()
179                                                : interVpnLinkState.getFirstEndpointState().getDpId();
180     }
181
182     public List<BigInteger> getEndpointDpnsByIpAddr(String endpointIp) {
183         List<BigInteger> result = new ArrayList<>();
184         if ( !isComplete()) {
185             return result;
186         }
187
188         return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getFirstEndpointState().getDpId()
189                                                  : this.interVpnLinkState.getSecondEndpointState().getDpId();
190     }
191
192     public List<BigInteger> getOtherEndpointDpnsByIpAddr(String endpointIp) {
193         List<BigInteger> result = new ArrayList<>();
194         if ( !isComplete()) {
195             return result;
196         }
197
198         return isFirstEndpointIpAddr(endpointIp) ? this.interVpnLinkState.getSecondEndpointState().getDpId()
199                                                  : this.interVpnLinkState.getFirstEndpointState().getDpId();
200     }
201 }