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.neutronvpn.api.l2gw;
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;
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;
29 * The Class L2GatewayDevice.
31 public class L2GatewayDevice {
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;
41 public L2GatewayDevice(String deviceName) {
42 this.deviceName = deviceName;
46 * VTEP device name mentioned with L2 Gateway.
48 * @return the device name
50 public String getDeviceName() {
55 * VTEP Node id for the device mentioned with L2 Gateway.
57 * @return the hwvtep node id
59 public String getHwvtepNodeId() {
64 * Sets the hwvtep node id.
67 * the new hwvtep node id
69 public void setHwvtepNodeId(String nodeId) {
70 this.hwvtepNodeId = nodeId;
74 * Tunnel IP created with in the device mentioned with L2 Gateway.
76 * @return the tunnel ips
79 public Set<IpAddress> getTunnelIps() {
83 public void addL2gwConnectionIdToDevice(Uuid connectionId, Devices device) {
84 l2gwConnectionIdToDevices.computeIfAbsent(connectionId, key -> Sets.newConcurrentHashSet()).add(device);
88 public Collection<Devices> getDevicesForL2gwConnectionId(Uuid connectionId) {
89 final Set<Devices> devices = l2gwConnectionIdToDevices.get(connectionId);
90 return devices != null ? devices : Collections.emptyList();
96 * @return the tunnel ip
99 public IpAddress getTunnelIp() {
100 if (!tunnelIps.isEmpty()) {
101 return tunnelIps.iterator().next();
107 * Adds the tunnel ip.
112 public void addTunnelIp(IpAddress tunnelIp) {
113 tunnelIps.add(tunnelIp);
117 * UUID representing L2Gateway.
119 * @return the l2 gateway ids
122 public Set<Uuid> getL2GatewayIds() {
127 * Adds the l2 gateway id.
132 public void addL2GatewayId(Uuid l2GatewayId) {
133 l2GatewayIds.add(l2GatewayId);
137 * Removes the l2 gateway id.
142 public void removeL2GatewayId(Uuid l2GatewayId) {
143 l2GatewayIds.remove(l2GatewayId);
147 * Clear hwvtep node data.
149 public void clearHwvtepNodeData() {
155 * Sets the tunnel ips.
160 public void setTunnelIps(Set<IpAddress> tunnelIps) {
161 this.tunnelIps.clear();
162 this.tunnelIps.addAll(tunnelIps);
166 * Gets the ucast local macs.
168 * @return the ucast local macs
171 public Collection<LocalUcastMacs> getUcastLocalMacs() {
172 return new ArrayList<>(ucastLocalMacs);
176 * Adds the ucast local mac.
178 * @param localUcastMacs
179 * the local ucast macs
181 public void addUcastLocalMac(LocalUcastMacs localUcastMacs) {
182 ucastLocalMacs.add(localUcastMacs);
186 * Removes the ucast local mac.
188 * @param localUcastMacs
189 * the local ucast macs
191 public void removeUcastLocalMac(LocalUcastMacs localUcastMacs) {
192 ucastLocalMacs.remove(localUcastMacs);
195 public boolean isConnected() {
196 return connected.get();
199 public void setConnected(boolean connected) {
200 this.connected.set(connected);
206 * @see java.lang.Object#hashCode()
209 public int hashCode() {
210 final int prime = 31;
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();
223 * @see java.lang.Object#equals(java.lang.Object)
226 public boolean equals(Object obj) {
233 if (getClass() != obj.getClass()) {
236 L2GatewayDevice other = (L2GatewayDevice) obj;
237 if (deviceName == null) {
238 if (other.deviceName != null) {
241 } else if (!deviceName.equals(other.deviceName)) {
244 if (hwvtepNodeId == null) {
245 if (other.hwvtepNodeId != null) {
248 } else if (!hwvtepNodeId.equals(other.hwvtepNodeId)) {
251 if (!l2GatewayIds.equals(other.l2GatewayIds)) {
254 if (!tunnelIps.equals(other.tunnelIps)) {
257 if (!ucastLocalMacs.equals(other.ucastLocalMacs)) {
263 public boolean containsUcastMac(LocalUcastMacs mac) {
264 return ucastLocalMacs.contains(mac);
270 * @see java.lang.Object#toString()
273 public String toString() {
274 List<String> lstTunnelIps = new ArrayList<>();
275 for (IpAddress ip : this.tunnelIps) {
276 lstTunnelIps.add(ip.stringValue());
279 List<String> lstMacs =
280 this.ucastLocalMacs.stream().map(localUcastMac -> localUcastMac.getMacEntryKey().getValue()).collect(
281 Collectors.toList());
283 return "L2GatewayDevice [deviceName=" + deviceName + ", hwvtepNodeId=" + hwvtepNodeId + ", tunnelIps="
284 + lstTunnelIps + ", l2GatewayIds=" + l2GatewayIds + ", ucastLocalMacs=" + lstMacs + "]";