VPN changes for IPv6 ND based aliveness monitor
[netvirt.git] / ipv6service / impl / src / main / java / org / opendaylight / netvirt / ipv6service / VirtualNetwork.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.ipv6service;
9
10 import java.math.BigInteger;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Set;
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;
23
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;
28
29     public VirtualNetwork(Uuid networkUUID) {
30         this.networkUUID = networkUUID;
31     }
32
33     @Override
34     public Uuid getNetworkUuid() {
35         return networkUUID;
36     }
37
38     public void updateDpnPortInfo(BigInteger dpnId, Long ofPort, int addOrRemove) {
39         if (dpnId == null) {
40             return;
41         }
42         synchronized (networkUUID.getValue()) {
43             DpnInterfaceInfo dpnInterface = dpnIfaceList.computeIfAbsent(dpnId, key -> new DpnInterfaceInfo(dpnId));
44             if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
45                 dpnInterface.updateofPortList(ofPort);
46             } else {
47                 dpnInterface.removeOfPortFromList(ofPort);
48             }
49         }
50     }
51
52     @Override
53     public Long getElanTag() {
54         return elanTag;
55     }
56
57     public void setElanTag(Long etag) {
58         elanTag = etag;
59     }
60
61     @Override
62     public List<BigInteger> getDpnsHostingNetwork() {
63         return dpnIfaceList.values().stream().flatMap(dpnInterfaceInfo -> Stream.of(dpnInterfaceInfo.getDpId()))
64                 .collect(Collectors.toList());
65     }
66
67     public Collection<DpnInterfaceInfo> getDpnIfaceList() {
68         return dpnIfaceList.values();
69     }
70
71     public DpnInterfaceInfo getDpnIfaceInfo(BigInteger dpId) {
72         return dpId != null ? dpnIfaceList.get(dpId) : null;
73     }
74
75     public void setRSPuntFlowStatusOnDpnId(BigInteger dpnId, int action) {
76         DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
77         if (null != dpnInterface) {
78             dpnInterface.setRsFlowConfiguredStatus(action);
79         }
80     }
81
82     public int getRSPuntFlowStatusOnDpnId(BigInteger dpnId) {
83         DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
84         if (null != dpnInterface) {
85             return dpnInterface.getRsFlowConfiguredStatus();
86         }
87         return Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
88     }
89
90     public void clearDpnInterfaceList() {
91         dpnIfaceList.clear();
92     }
93
94     @Override
95     public String toString() {
96         return "VirtualNetwork [networkUUID=" + networkUUID + " dpnIfaceList=" + dpnIfaceList + "]";
97     }
98
99     public void removeSelf() {
100         synchronized (networkUUID.getValue()) {
101             dpnIfaceList.values().forEach(dpnInterfaceInfo -> {
102                 dpnInterfaceInfo.clearOfPortList();
103                 dpnInterfaceInfo.clearNdTargetFlowInfo();
104                 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
105             });
106
107             clearDpnInterfaceList();
108         }
109     }
110
111     public static class DpnInterfaceInfo {
112         BigInteger dpId;
113         int rsPuntFlowConfigured;
114         final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
115         final Set<Long> ofPortList = ConcurrentHashMap.newKeySet();
116         final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
117
118         DpnInterfaceInfo(BigInteger dpnId) {
119             dpId = dpnId;
120             rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
121         }
122
123         public void setDpId(BigInteger dpId) {
124             this.dpId = dpId;
125         }
126
127         public BigInteger getDpId() {
128             return dpId;
129         }
130
131         public void setRsFlowConfiguredStatus(int status) {
132             this.rsPuntFlowConfigured = status;
133         }
134
135         public int getRsFlowConfiguredStatus() {
136             return rsPuntFlowConfigured;
137         }
138
139         public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
140             if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
141                 this.subnetCidrPuntFlowList.add(subnetUUID);
142             } else {
143                 this.subnetCidrPuntFlowList.remove(subnetUUID);
144             }
145         }
146
147         public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
148             return subnetCidrPuntFlowList.contains(subnetUUID);
149         }
150
151         public Set<Ipv6Address> getNDTargetFlows() {
152             return ndTargetFlowsPunted;
153         }
154
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);
159             } else {
160                 this.ndTargetFlowsPunted.remove(ipv6);
161             }
162         }
163
164         public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
165             Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
166             return this.ndTargetFlowsPunted.contains(ipv6);
167         }
168
169         public void clearNdTargetFlowInfo() {
170             this.ndTargetFlowsPunted.clear();
171         }
172
173         public void updateofPortList(Long ofPort) {
174             this.ofPortList.add(ofPort);
175         }
176
177         public void removeOfPortFromList(Long ofPort) {
178             this.ofPortList.remove(ofPort);
179         }
180
181         public void clearOfPortList() {
182             this.ofPortList.clear();
183         }
184
185         public void clearsubnetCidrPuntFlowInfo() {
186             this.subnetCidrPuntFlowList.clear();
187         }
188
189         @Override
190         public String toString() {
191             return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
192                     + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortList="
193                     + ofPortList + "]";
194         }
195     }
196 }