2 * Copyright (c) 2016 Red Hat, Inc. 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
8 package org.opendaylight.netvirt.ipv6service;
10 import java.util.Collection;
11 import java.util.List;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.genius.ipv6util.api.Ipv6Util;
19 import org.opendaylight.netvirt.ipv6service.api.IVirtualNetwork;
20 import org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceConstants;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yangtools.yang.common.Uint64;
25 public class VirtualNetwork implements IVirtualNetwork {
26 private final Uuid networkUUID;
27 private final ConcurrentMap<Uint64, DpnInterfaceInfo> dpnIfaceList = new ConcurrentHashMap<>();
28 private volatile Long elanTag;
29 private volatile int mtu = 0;
31 public VirtualNetwork(Uuid networkUUID) {
32 this.networkUUID = networkUUID;
36 public Uuid getNetworkUuid() {
40 public void updateDpnPortInfo(Uint64 dpnId, Long ofPort,Uuid portId, int addOrRemove) {
44 synchronized (networkUUID.getValue()) {
45 DpnInterfaceInfo dpnInterface = dpnIfaceList.computeIfAbsent(dpnId, key -> new DpnInterfaceInfo(dpnId));
46 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
47 dpnInterface.updateofPortMap(ofPort, portId);
49 dpnInterface.removeOfPortFromMap(ofPort);
55 public Long getElanTag() {
59 public void setElanTag(Long etag) {
64 public List<Uint64> getDpnsHostingNetwork() {
65 return dpnIfaceList.values().stream().flatMap(dpnInterfaceInfo -> Stream.of(dpnInterfaceInfo.getDpId()))
66 .collect(Collectors.toList());
69 public Collection<DpnInterfaceInfo> getDpnIfaceList() {
70 return dpnIfaceList.values();
74 public DpnInterfaceInfo getDpnIfaceInfo(Uint64 dpId) {
75 return dpId != null ? dpnIfaceList.get(dpId) : null;
78 public void setRSPuntFlowStatusOnDpnId(Uint64 dpnId, int action) {
79 DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
80 if (null != dpnInterface) {
81 dpnInterface.setRsFlowConfiguredStatus(action);
85 public int getRSPuntFlowStatusOnDpnId(Uint64 dpnId) {
86 DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
87 if (null != dpnInterface) {
88 return dpnInterface.getRsFlowConfiguredStatus();
90 return Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
93 public void clearDpnInterfaceList() {
98 public String toString() {
99 return "VirtualNetwork [networkUUID=" + networkUUID + " dpnIfaceList=" + dpnIfaceList + "]";
102 public void removeSelf() {
103 synchronized (networkUUID.getValue()) {
104 dpnIfaceList.values().forEach(dpnInterfaceInfo -> {
105 dpnInterfaceInfo.clearPortList();
106 dpnInterfaceInfo.clearNdTargetFlowInfo();
107 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
108 dpnInterfaceInfo.clearOvsNaResponderFlowConfigured();
111 clearDpnInterfaceList();
115 public void setMtu(int mtu) {
120 public int getMtu() {
124 public static class DpnInterfaceInfo {
126 int rsPuntFlowConfigured;
127 final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
128 final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
129 ConcurrentMap<Long, Uuid> ofPortMap;
130 ConcurrentMap<Uuid, Integer> ovsNaResponderFlowConfigured;
132 DpnInterfaceInfo(Uint64 dpnId) {
134 ofPortMap = new ConcurrentHashMap();
135 rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
136 ovsNaResponderFlowConfigured = new ConcurrentHashMap();
139 public void setDpId(Uint64 dpId) {
143 public Uint64 getDpId() {
147 public void setRsFlowConfiguredStatus(int status) {
148 this.rsPuntFlowConfigured = status;
151 public int getRsFlowConfiguredStatus() {
152 return rsPuntFlowConfigured;
155 public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
156 if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
157 this.subnetCidrPuntFlowList.add(subnetUUID);
159 this.subnetCidrPuntFlowList.remove(subnetUUID);
163 public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
164 return subnetCidrPuntFlowList.contains(subnetUUID);
167 public Set<Ipv6Address> getNDTargetFlows() {
168 return ndTargetFlowsPunted;
171 public void updateNDTargetAddress(Ipv6Address ipv6Address, int addOrRemove) {
172 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
173 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
174 this.ndTargetFlowsPunted.add(ipv6);
176 this.ndTargetFlowsPunted.remove(ipv6);
180 public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
181 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
182 return this.ndTargetFlowsPunted.contains(ipv6);
185 public void clearNdTargetFlowInfo() {
186 this.ndTargetFlowsPunted.clear();
189 public void setOvsNaResponderFlowConfiguredStatus(Uuid interfaceName, int lportTag, int addOrRemove) {
190 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
191 this.ovsNaResponderFlowConfigured.put(interfaceName, lportTag);
193 this.ovsNaResponderFlowConfigured.remove(interfaceName);
197 public void clearOvsNaResponderFlowConfigured() {
198 this.ovsNaResponderFlowConfigured.clear();
201 public void updateofPortMap(Long ofPort, Uuid portId) {
202 this.ofPortMap.put(ofPort, portId);
205 public void removeOfPortFromMap(Long ofPort) {
206 this.ofPortMap.remove(ofPort);
209 public void clearPortList() {
210 this.ofPortMap.clear();
213 public void clearsubnetCidrPuntFlowInfo() {
214 this.subnetCidrPuntFlowList.clear();
218 public String toString() {
219 return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
220 + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortMap ="