Fix safety warnings
[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.util.Collection;
11 import java.util.List;
12 import java.util.Set;
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;
24
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;
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(Uint64 dpnId, Long ofPort,Uuid portId, 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.updateofPortMap(ofPort, portId);
48             } else {
49                 dpnInterface.removeOfPortFromMap(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<Uint64> 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(Uint64 dpId) {
75         return dpId != null ? dpnIfaceList.get(dpId) : null;
76     }
77
78     public void setRSPuntFlowStatusOnDpnId(Uint64 dpnId, int action) {
79         DpnInterfaceInfo dpnInterface = getDpnIfaceInfo(dpnId);
80         if (null != dpnInterface) {
81             dpnInterface.setRsFlowConfiguredStatus(action);
82         }
83     }
84
85     public int getRSPuntFlowStatusOnDpnId(Uint64 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.clearPortList();
106                 dpnInterfaceInfo.clearNdTargetFlowInfo();
107                 dpnInterfaceInfo.clearsubnetCidrPuntFlowInfo();
108                 dpnInterfaceInfo.clearOvsNaResponderFlowConfigured();
109             });
110
111             clearDpnInterfaceList();
112         }
113     }
114
115     public void setMtu(int mtu) {
116         this.mtu = mtu;
117     }
118
119     @Override
120     public int getMtu() {
121         return this.mtu;
122     }
123
124     public static class DpnInterfaceInfo {
125         private Uint64 dpId;
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<>();
131
132         DpnInterfaceInfo(Uint64 dpnId) {
133             dpId = dpnId;
134         }
135
136         public void setDpId(Uint64 dpId) {
137             this.dpId = dpId;
138         }
139
140         public Uint64 getDpId() {
141             return dpId;
142         }
143
144         public void setRsFlowConfiguredStatus(int status) {
145             this.rsPuntFlowConfigured = status;
146         }
147
148         public int getRsFlowConfiguredStatus() {
149             return rsPuntFlowConfigured;
150         }
151
152         public void updateSubnetCidrFlowStatus(Uuid subnetUUID, int addOrRemove) {
153             if (addOrRemove == Ipv6ServiceConstants.ADD_FLOW) {
154                 this.subnetCidrPuntFlowList.add(subnetUUID);
155             } else {
156                 this.subnetCidrPuntFlowList.remove(subnetUUID);
157             }
158         }
159
160         public boolean isSubnetCidrFlowAlreadyConfigured(Uuid subnetUUID) {
161             return subnetCidrPuntFlowList.contains(subnetUUID);
162         }
163
164         public Set<Ipv6Address> getNDTargetFlows() {
165             return ndTargetFlowsPunted;
166         }
167
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);
172             } else {
173                 this.ndTargetFlowsPunted.remove(ipv6);
174             }
175         }
176
177         public boolean isNdTargetFlowAlreadyConfigured(Ipv6Address ipv6Address) {
178             Ipv6Address ipv6 = Ipv6Address.getDefaultInstance(Ipv6Util.getFormattedIpv6Address(ipv6Address));
179             return this.ndTargetFlowsPunted.contains(ipv6);
180         }
181
182         public void clearNdTargetFlowInfo() {
183             this.ndTargetFlowsPunted.clear();
184         }
185
186         public void setOvsNaResponderFlowConfiguredStatus(Uuid interfaceName, int lportTag, int addOrRemove) {
187             if (addOrRemove == Ipv6ServiceConstants.ADD_ENTRY) {
188                 this.ovsNaResponderFlowConfigured.put(interfaceName, lportTag);
189             } else {
190                 this.ovsNaResponderFlowConfigured.remove(interfaceName);
191             }
192         }
193
194         public void clearOvsNaResponderFlowConfigured() {
195             this.ovsNaResponderFlowConfigured.clear();
196         }
197
198         public void updateofPortMap(Long ofPort, Uuid portId) {
199             this.ofPortMap.put(ofPort, portId);
200         }
201
202         public void removeOfPortFromMap(Long ofPort) {
203             this.ofPortMap.remove(ofPort);
204         }
205
206         public void clearPortList() {
207             this.ofPortMap.clear();
208         }
209
210         public void clearsubnetCidrPuntFlowInfo() {
211             this.subnetCidrPuntFlowList.clear();
212         }
213
214         @Override
215         public String toString() {
216             return "DpnInterfaceInfo [dpId=" + dpId + " rsPuntFlowConfigured=" + rsPuntFlowConfigured
217                     + "subnetCidrPuntFlowList=" + subnetCidrPuntFlowList + " ofPortMap ="
218                     + ofPortMap  + "]";
219         }
220     }
221 }