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, int addOrRemove) {
44 synchronized (networkUUID.getValue()) {
45 DpnInterfaceInfo dpnInterface = dpnIfaceList.computeIfAbsent(dpnId, key -> new DpnInterfaceInfo(dpnId));
46 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
47 dpnInterface.updateofPortList(ofPort);
49 dpnInterface.removeOfPortFromList(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.clearOfPortList();
106 dpnInterfaceInfo.clearNdTargetFlowInfo();
107 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
110 clearDpnInterfaceList();
114 public void setMtu(int mtu) {
119 public int getMtu() {
123 public static class DpnInterfaceInfo {
125 int rsPuntFlowConfigured;
126 final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
127 final Set<Long> ofPortList = ConcurrentHashMap.newKeySet();
128 final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
130 DpnInterfaceInfo(Uint64 dpnId) {
132 rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
135 public void setDpId(Uint64 dpId) {
139 public Uint64 getDpId() {
143 public void setRsFlowConfiguredStatus(int status) {
144 this.rsPuntFlowConfigured = status;
147 public int getRsFlowConfiguredStatus() {
148 return rsPuntFlowConfigured;
151 public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
152 if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
153 this.subnetCidrPuntFlowList.add(subnetUUID);
155 this.subnetCidrPuntFlowList.remove(subnetUUID);
159 public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
160 return subnetCidrPuntFlowList.contains(subnetUUID);
163 public Set<Ipv6Address> getNDTargetFlows() {
164 return ndTargetFlowsPunted;
167 public void updateNDTargetAddress(Ipv6Address ipv6Address, int addOrRemove) {
168 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
169 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
170 this.ndTargetFlowsPunted.add(ipv6);
172 this.ndTargetFlowsPunted.remove(ipv6);
176 public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
177 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
178 return this.ndTargetFlowsPunted.contains(ipv6);
181 public void clearNdTargetFlowInfo() {
182 this.ndTargetFlowsPunted.clear();
185 public void updateofPortList(Long ofPort) {
186 this.ofPortList.add(ofPort);
189 public void removeOfPortFromList(Long ofPort) {
190 this.ofPortList.remove(ofPort);
193 public void clearOfPortList() {
194 this.ofPortList.clear();
197 public void clearsubnetCidrPuntFlowInfo() {
198 this.subnetCidrPuntFlowList.clear();
202 public String toString() {
203 return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
204 + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortList="