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.math.BigInteger;
11 import java.util.Collection;
12 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
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;
24 public class VirtualNetwork implements IVirtualNetwork {
25 private final Uuid networkUUID;
26 private final ConcurrentMap<BigInteger, DpnInterfaceInfo> dpnIfaceList = new ConcurrentHashMap<>();
27 private volatile Long elanTag;
29 public VirtualNetwork(Uuid networkUUID) {
30 this.networkUUID = networkUUID;
34 public Uuid getNetworkUuid() {
38 public void updateDpnPortInfo(BigInteger dpnId, Long ofPort, int addOrRemove) {
42 synchronized (networkUUID.getValue()) {
43 DpnInterfaceInfo dpnInterface = dpnIfaceList.computeIfAbsent(dpnId, key -> new DpnInterfaceInfo(dpnId));
44 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
45 dpnInterface.updateofPortList(ofPort);
47 dpnInterface.removeOfPortFromList(ofPort);
53 public Long getElanTag() {
57 public void setElanTag(Long etag) {
62 public List<BigInteger> getDpnsHostingNetwork() {
63 return dpnIfaceList.values().stream().flatMap(dpnInterfaceInfo -> Stream.of(dpnInterfaceInfo.getDpId()))
64 .collect(Collectors.toList());
67 public Collection<DpnInterfaceInfo> getDpnIfaceList() {
68 return dpnIfaceList.values();
71 public DpnInterfaceInfo getDpnIfaceInfo(BigInteger dpId) {
72 return dpId != null ? dpnIfaceList.get(dpId) : null;
75 public void setRSPuntFlowStatusOnDpnId(BigInteger dpnId, int action) {
76 DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
77 if (null != dpnInterface) {
78 dpnInterface.setRsFlowConfiguredStatus(action);
82 public int getRSPuntFlowStatusOnDpnId(BigInteger dpnId) {
83 DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
84 if (null != dpnInterface) {
85 return dpnInterface.getRsFlowConfiguredStatus();
87 return Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
90 public void clearDpnInterfaceList() {
95 public String toString() {
96 return "VirtualNetwork [networkUUID=" + networkUUID + " dpnIfaceList=" + dpnIfaceList + "]";
99 public void removeSelf() {
100 synchronized (networkUUID.getValue()) {
101 dpnIfaceList.values().forEach(dpnInterfaceInfo -> {
102 dpnInterfaceInfo.clearOfPortList();
103 dpnInterfaceInfo.clearNdTargetFlowInfo();
104 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
107 clearDpnInterfaceList();
111 public static class DpnInterfaceInfo {
113 int rsPuntFlowConfigured;
114 final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
115 final Set<Long> ofPortList = ConcurrentHashMap.newKeySet();
116 final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
118 DpnInterfaceInfo(BigInteger dpnId) {
120 rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
123 public void setDpId(BigInteger dpId) {
127 public BigInteger getDpId() {
131 public void setRsFlowConfiguredStatus(int status) {
132 this.rsPuntFlowConfigured = status;
135 public int getRsFlowConfiguredStatus() {
136 return rsPuntFlowConfigured;
139 public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
140 if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
141 this.subnetCidrPuntFlowList.add(subnetUUID);
143 this.subnetCidrPuntFlowList.remove(subnetUUID);
147 public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
148 return subnetCidrPuntFlowList.contains(subnetUUID);
151 public Set<Ipv6Address> getNDTargetFlows() {
152 return ndTargetFlowsPunted;
155 public void updateNDTargetAddress(Ipv6Address ipv6Address, int addOrRemove) {
156 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
157 if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
158 this.ndTargetFlowsPunted.add(ipv6);
160 this.ndTargetFlowsPunted.remove(ipv6);
164 public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
165 Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
166 return this.ndTargetFlowsPunted.contains(ipv6);
169 public void clearNdTargetFlowInfo() {
170 this.ndTargetFlowsPunted.clear();
173 public void updateofPortList(Long ofPort) {
174 this.ofPortList.add(ofPort);
177 public void removeOfPortFromList(Long ofPort) {
178 this.ofPortList.remove(ofPort);
181 public void clearOfPortList() {
182 this.ofPortList.clear();
185 public void clearsubnetCidrPuntFlowInfo() {
186 this.subnetCidrPuntFlowList.clear();
190 public String toString() {
191 return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
192 + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortList="