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 private int rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
127 private final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
128 final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
129 final ConcurrentMap<Long, Uuid> ofPortMap = new ConcurrentHashMap<>();
130 private final ConcurrentMap<Uuid, Integer> ovsNaResponderFlowConfigured = new ConcurrentHashMap<>();
132 DpnInterfaceInfo(Uint64 dpnId) {
136 public void setDpId(Uint64 dpId) {
140 public Uint64 getDpId() {
144 public void setRsFlowConfiguredStatus(int status) {
145 this.rsPuntFlowConfigured = status;
148 public int getRsFlowConfiguredStatus() {
149 return rsPuntFlowConfigured;
152 public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
153 if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
154 this.subnetCidrPuntFlowList.add(subnetUUID);
156 this.subnetCidrPuntFlowList.remove(subnetUUID);
160 public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
161 return subnetCidrPuntFlowList.contains(subnetUUID);
164 public Set<Ipv6Address> getNDTargetFlows() {
165 return ndTargetFlowsPunted;
168 public void updateNDTargetAddress(Ipv6Address ipv6Address, int addOrRemove) {
169 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
170 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
171 this.ndTargetFlowsPunted.add(ipv6);
173 this.ndTargetFlowsPunted.remove(ipv6);
177 public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
178 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
179 return this.ndTargetFlowsPunted.contains(ipv6);
182 public void clearNdTargetFlowInfo() {
183 this.ndTargetFlowsPunted.clear();
186 public void setOvsNaResponderFlowConfiguredStatus(Uuid interfaceName, int lportTag, int addOrRemove) {
187 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
188 this.ovsNaResponderFlowConfigured.put(interfaceName, lportTag);
190 this.ovsNaResponderFlowConfigured.remove(interfaceName);
194 public void clearOvsNaResponderFlowConfigured() {
195 this.ovsNaResponderFlowConfigured.clear();
198 public void updateofPortMap(Long ofPort, Uuid portId) {
199 this.ofPortMap.put(ofPort, portId);
202 public void removeOfPortFromMap(Long ofPort) {
203 this.ofPortMap.remove(ofPort);
206 public void clearPortList() {
207 this.ofPortMap.clear();
210 public void clearsubnetCidrPuntFlowInfo() {
211 this.subnetCidrPuntFlowList.clear();
215 public String toString() {
216 return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
217 + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortMap ="