Switch to JDT annotations for Nullable and NonNull
[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.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.genius.ipv6util.api.Ipv6Util;
20 import org.opendaylight.netvirt.ipv6service.api.IVirtualNetwork;
21 import org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceConstants;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24
25 public class VirtualNetwork implements IVirtualNetwork {
26     private final Uuid networkUUID;
27     private final ConcurrentMap<BigInteger, DpnInterfaceInfo> dpnIfaceList = new ConcurrentHashMap<>();
28     private volatile Long elanTag;
29     private volatile int mtu = 0;
30
31     public VirtualNetwork(Uuid networkUUID) {
32         this.networkUUID = networkUUID;
33     }
34
35     @Override
36     public Uuid getNetworkUuid() {
37         return networkUUID;
38     }
39
40     public void updateDpnPortInfo(BigInteger dpnId, Long ofPort, int addOrRemove) {
41         if (dpnId == null) {
42             return;
43         }
44         synchronized (networkUUID.getValue()) {
45             DpnInterfaceInfo dpnInterface = dpnIfaceList.computeIfAbsent(dpnId, key -> new DpnInterfaceInfo(dpnId));
46             if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
47                 dpnInterface.updateofPortList(ofPort);
48             } else {
49                 dpnInterface.removeOfPortFromList(ofPort);
50             }
51         }
52     }
53
54     @Override
55     public Long getElanTag() {
56         return elanTag;
57     }
58
59     public void setElanTag(Long etag) {
60         elanTag = etag;
61     }
62
63     @Override
64     public List<BigInteger> getDpnsHostingNetwork() {
65         return dpnIfaceList.values().stream().flatMap(dpnInterfaceInfo -> Stream.of(dpnInterfaceInfo.getDpId()))
66                 .collect(Collectors.toList());
67     }
68
69     public Collection<DpnInterfaceInfo> getDpnIfaceList() {
70         return dpnIfaceList.values();
71     }
72
73     @Nullable
74     public DpnInterfaceInfo getDpnIfaceInfo(BigInteger dpId) {
75         return dpId != null ? dpnIfaceList.get(dpId) : null;
76     }
77
78     public void setRSPuntFlowStatusOnDpnId(BigInteger dpnId, int action) {
79         DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
80         if (null != dpnInterface) {
81             dpnInterface.setRsFlowConfiguredStatus(action);
82         }
83     }
84
85     public int getRSPuntFlowStatusOnDpnId(BigInteger dpnId) {
86         DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
87         if (null != dpnInterface) {
88             return dpnInterface.getRsFlowConfiguredStatus();
89         }
90         return Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
91     }
92
93     public void clearDpnInterfaceList() {
94         dpnIfaceList.clear();
95     }
96
97     @Override
98     public String toString() {
99         return "VirtualNetwork [networkUUID=" + networkUUID + " dpnIfaceList=" + dpnIfaceList + "]";
100     }
101
102     public void removeSelf() {
103         synchronized (networkUUID.getValue()) {
104             dpnIfaceList.values().forEach(dpnInterfaceInfo -> {
105                 dpnInterfaceInfo.clearOfPortList();
106                 dpnInterfaceInfo.clearNdTargetFlowInfo();
107                 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
108             });
109
110             clearDpnInterfaceList();
111         }
112     }
113
114     public void setMtu(int mtu) {
115         this.mtu = mtu;
116     }
117
118     @Override
119     public int getMtu() {
120         return this.mtu;
121     }
122
123     public static class DpnInterfaceInfo {
124         BigInteger dpId;
125         int rsPuntFlowConfigured;
126         final Set<Uuid> subnetCidrPuntFlowList = ConcurrentHashMap.newKeySet();
127         final Set<Long> ofPortList = ConcurrentHashMap.newKeySet();
128         final Set<Ipv6Address> ndTargetFlowsPunted = ConcurrentHashMap.newKeySet();
129
130         DpnInterfaceInfo(BigInteger dpnId) {
131             dpId = dpnId;
132             rsPuntFlowConfigured = Ipv6ServiceConstants.FLOWS_NOT_CONFIGURED;
133         }
134
135         public void setDpId(BigInteger dpId) {
136             this.dpId = dpId;
137         }
138
139         public BigInteger getDpId() {
140             return dpId;
141         }
142
143         public void setRsFlowConfiguredStatus(int status) {
144             this.rsPuntFlowConfigured = status;
145         }
146
147         public int getRsFlowConfiguredStatus() {
148             return rsPuntFlowConfigured;
149         }
150
151         public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
152             if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
153                 this.subnetCidrPuntFlowList.add(subnetUUID);
154             } else {
155                 this.subnetCidrPuntFlowList.remove(subnetUUID);
156             }
157         }
158
159         public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
160             return subnetCidrPuntFlowList.contains(subnetUUID);
161         }
162
163         public Set<Ipv6Address> getNDTargetFlows() {
164             return ndTargetFlowsPunted;
165         }
166
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);
171             } else {
172                 this.ndTargetFlowsPunted.remove(ipv6);
173             }
174         }
175
176         public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
177             Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
178             return this.ndTargetFlowsPunted.contains(ipv6);
179         }
180
181         public void clearNdTargetFlowInfo() {
182             this.ndTargetFlowsPunted.clear();
183         }
184
185         public void updateofPortList(Long ofPort) {
186             this.ofPortList.add(ofPort);
187         }
188
189         public void removeOfPortFromList(Long ofPort) {
190             this.ofPortList.remove(ofPort);
191         }
192
193         public void clearOfPortList() {
194             this.ofPortList.clear();
195         }
196
197         public void clearsubnetCidrPuntFlowInfo() {
198             this.subnetCidrPuntFlowList.clear();
199         }
200
201         @Override
202         public String toString() {
203             return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
204                     + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortList="
205                     + ofPortList + "]";
206         }
207     }
208 }