00c1f4115c35b53389beb0df4d0be9a34c465e4a
[netvirt.git] / neutronvpn / api / src / main / java / org / opendaylight / netvirt / neutronvpn / api / l2gw / L2GatewayDevice.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.neutronvpn.api.l2gw;
10
11 import com.google.common.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.atomic.AtomicBoolean;
20 import java.util.stream.Collectors;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
27
28 /**
29  * The Class L2GatewayDevice.
30  */
31 public class L2GatewayDevice {
32
33     private final String deviceName;
34     private final Set<IpAddress> tunnelIps = ConcurrentHashMap.newKeySet();
35     private final Set<Uuid> l2GatewayIds = ConcurrentHashMap.newKeySet();
36     private final Set<LocalUcastMacs> ucastLocalMacs = ConcurrentHashMap.newKeySet();
37     private final AtomicBoolean connected = new AtomicBoolean(false);
38     private final Map<Uuid,Set<Devices>> l2gwConnectionIdToDevices = new ConcurrentHashMap<>();
39     private volatile String hwvtepNodeId;
40
41     public L2GatewayDevice(String deviceName) {
42         this.deviceName = deviceName;
43     }
44
45     /**
46      * VTEP device name mentioned with L2 Gateway.
47      *
48      * @return the device name
49      */
50     public String getDeviceName() {
51         return deviceName;
52     }
53
54     /**
55      * VTEP Node id for the device mentioned with L2 Gateway.
56      *
57      * @return the hwvtep node id
58      */
59     public String getHwvtepNodeId() {
60         return hwvtepNodeId;
61     }
62
63     /**
64      * Sets the hwvtep node id.
65      *
66      * @param nodeId
67      *            the new hwvtep node id
68      */
69     public void setHwvtepNodeId(String nodeId) {
70         this.hwvtepNodeId = nodeId;
71     }
72
73     /**
74      * Tunnel IP created with in the device mentioned with L2 Gateway.
75      *
76      * @return the tunnel ips
77      */
78     @Nonnull
79     public Set<IpAddress> getTunnelIps() {
80         return tunnelIps;
81     }
82
83     public void addL2gwConnectionIdToDevice(Uuid connectionId, Devices device) {
84         l2gwConnectionIdToDevices.computeIfAbsent(connectionId, key -> Sets.newConcurrentHashSet()).add(device);
85     }
86
87     @Nonnull
88     public Collection<Devices> getDevicesForL2gwConnectionId(Uuid connectionId) {
89         final Set<Devices> devices = l2gwConnectionIdToDevices.get(connectionId);
90         return devices != null ? devices : Collections.emptyList();
91     }
92
93     /**
94      * Gets the tunnel ip.
95      *
96      * @return the tunnel ip
97      */
98     @Nullable
99     public IpAddress getTunnelIp() {
100         if (!tunnelIps.isEmpty()) {
101             return tunnelIps.iterator().next();
102         }
103         return null;
104     }
105
106     /**
107      * Adds the tunnel ip.
108      *
109      * @param tunnelIp
110      *            the tunnel ip
111      */
112     public void addTunnelIp(IpAddress tunnelIp) {
113         tunnelIps.add(tunnelIp);
114     }
115
116     /**
117      * UUID representing L2Gateway.
118      *
119      * @return the l2 gateway ids
120      */
121     @Nonnull
122     public Set<Uuid> getL2GatewayIds() {
123         return l2GatewayIds;
124     }
125
126     /**
127      * Adds the l2 gateway id.
128      *
129      * @param l2GatewayId
130      *            the l2 gateway id
131      */
132     public void addL2GatewayId(Uuid l2GatewayId) {
133         l2GatewayIds.add(l2GatewayId);
134     }
135
136     /**
137      * Removes the l2 gateway id.
138      *
139      * @param l2GatewayId
140      *            the l2 gateway id
141      */
142     public void removeL2GatewayId(Uuid l2GatewayId) {
143         l2GatewayIds.remove(l2GatewayId);
144     }
145
146     /**
147      * Clear hwvtep node data.
148      */
149     public void clearHwvtepNodeData() {
150         tunnelIps.clear();
151         hwvtepNodeId = null;
152     }
153
154     /**
155      * Sets the tunnel ips.
156      *
157      * @param tunnelIps
158      *            the new tunnel ips
159      */
160     public void setTunnelIps(Set<IpAddress> tunnelIps) {
161         this.tunnelIps.clear();
162         this.tunnelIps.addAll(tunnelIps);
163     }
164
165     /**
166      * Gets the ucast local macs.
167      *
168      * @return the ucast local macs
169      */
170     @Nonnull
171     public Collection<LocalUcastMacs> getUcastLocalMacs() {
172         return new ArrayList<>(ucastLocalMacs);
173     }
174
175     /**
176      * Adds the ucast local mac.
177      *
178      * @param localUcastMacs
179      *            the local ucast macs
180      */
181     public void addUcastLocalMac(LocalUcastMacs localUcastMacs) {
182         ucastLocalMacs.add(localUcastMacs);
183     }
184
185     /**
186      * Removes the ucast local mac.
187      *
188      * @param localUcastMacs
189      *            the local ucast macs
190      */
191     public void removeUcastLocalMac(LocalUcastMacs localUcastMacs) {
192         ucastLocalMacs.remove(localUcastMacs);
193     }
194
195     public boolean isConnected() {
196         return connected.get();
197     }
198
199     public void setConnected(boolean connected) {
200         this.connected.set(connected);
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see java.lang.Object#hashCode()
207      */
208     @Override
209     public int hashCode() {
210         final int prime = 31;
211         int result = 1;
212         result = prime * result + (deviceName == null ? 0 : deviceName.hashCode());
213         result = prime * result + (hwvtepNodeId == null ? 0 : hwvtepNodeId.hashCode());
214         result = prime * result + l2GatewayIds.hashCode();
215         result = prime * result + tunnelIps.hashCode();
216         result = prime * result + ucastLocalMacs.hashCode();
217         return result;
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see java.lang.Object#equals(java.lang.Object)
224      */
225     @Override
226     public boolean equals(Object obj) {
227         if (this == obj) {
228             return true;
229         }
230         if (obj == null) {
231             return false;
232         }
233         if (getClass() != obj.getClass()) {
234             return false;
235         }
236         L2GatewayDevice other = (L2GatewayDevice) obj;
237         if (deviceName == null) {
238             if (other.deviceName != null) {
239                 return false;
240             }
241         } else if (!deviceName.equals(other.deviceName)) {
242             return false;
243         }
244         if (hwvtepNodeId == null) {
245             if (other.hwvtepNodeId != null) {
246                 return false;
247             }
248         } else if (!hwvtepNodeId.equals(other.hwvtepNodeId)) {
249             return false;
250         }
251         if (!l2GatewayIds.equals(other.l2GatewayIds)) {
252             return false;
253         }
254         if (!tunnelIps.equals(other.tunnelIps)) {
255             return false;
256         }
257         if (!ucastLocalMacs.equals(other.ucastLocalMacs)) {
258             return false;
259         }
260         return true;
261     }
262
263     public boolean containsUcastMac(LocalUcastMacs mac) {
264         return ucastLocalMacs.contains(mac);
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see java.lang.Object#toString()
271      */
272     @Override
273     public String toString() {
274         List<String> lstTunnelIps = new ArrayList<>();
275         for (IpAddress ip : this.tunnelIps) {
276             lstTunnelIps.add(ip.stringValue());
277         }
278
279         List<String> lstMacs =
280                 this.ucastLocalMacs.stream().map(localUcastMac -> localUcastMac.getMacEntryKey().getValue()).collect(
281                         Collectors.toList());
282
283         return "L2GatewayDevice [deviceName=" + deviceName + ", hwvtepNodeId=" + hwvtepNodeId + ", tunnelIps="
284                 + lstTunnelIps + ", l2GatewayIds=" + l2GatewayIds + ", ucastLocalMacs=" + lstMacs + "]";
285     }
286
287 }