Bug 8055: use Java collection constructors
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / HwvtepOperationalState.java
1 /*
2  * Copyright (c) 2015, 2017 China Telecom Beijing Research Institute 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
43 import org.opendaylight.yangtools.yang.binding.Identifiable;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Map.Entry;
54 import java.util.Set;
55 import java.util.concurrent.ConcurrentHashMap;
56
57 //TODO: need to be optimized, get entry by iid not name
58 public class HwvtepOperationalState {
59
60     private static final Logger LOG = LoggerFactory.getLogger(HwvtepOperationalState.class);
61
62     private Map<InstanceIdentifier<Node>, Node> operationalNodes = new HashMap<>();
63     private ReadWriteTransaction transaction;
64     HashMap<InstanceIdentifier<TerminationPoint>, UUID> inflightLocators = new HashMap<>();
65     private HwvtepDeviceInfo deviceInfo;
66     private HwvtepConnectionInstance connectionInstance;
67     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, UUID>> currentTxUUIDs = new ConcurrentHashMap<>();
68     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, Boolean>> currentTxDeletedKeys = new ConcurrentHashMap<>();
69
70     /* stores the modified and deleted data for each child type of each node id
71        Map<nodeid , Pair < updated, deleted >
72        each updated/ deleted contains Map < child type, List<ChildData>>
73        child type is the child of hwvtep Global augmentation
74      */
75     private Map<InstanceIdentifier<Node>,
76             Pair<Map<Class<? extends Identifiable>, List<Identifiable>>,
77                     Map<Class<? extends Identifiable>, List<Identifiable>>>> modifiedData = new HashMap<>();
78     private boolean inReconciliation = false;
79     private final DataBroker db;
80     private final Collection<DataTreeModification<Node>> changes;
81
82     public HwvtepOperationalState(DataBroker db, HwvtepConnectionInstance connectionInstance,
83                                   Collection<DataTreeModification<Node>> changes) {
84         this.connectionInstance = connectionInstance;
85         this.deviceInfo = connectionInstance.getDeviceInfo();
86         this.db = db;
87         this.changes = changes;
88         this.transaction = db.newReadWriteTransaction();
89     }
90
91     public HwvtepOperationalState(HwvtepConnectionInstance connectionInstance) {
92         this.connectionInstance = connectionInstance;
93         this.deviceInfo = connectionInstance.getDeviceInfo();
94         this.db = connectionInstance.getDataBroker();
95         this.changes = null;
96         transaction = connectionInstance.getDataBroker().newReadWriteTransaction();
97         Optional<Node> readNode = HwvtepSouthboundUtil.readNode(transaction,
98                 connectionInstance.getInstanceIdentifier());
99         if (readNode.isPresent()) {
100             operationalNodes.put(connectionInstance.getInstanceIdentifier(), readNode.get());
101         }
102     }
103
104     public void readOperationalNodes() {
105         if (inReconciliation) {
106             return;
107         }
108         if (changes == null) {
109             LOG.warn("Could not read operational nodes for {} as changes is",
110                     connectionInstance.getNodeId().getValue());
111             return;
112         }
113         Map<InstanceIdentifier<Node>, Node> nodeCreateOrUpdate =
114                 TransactUtils.extractCreatedOrUpdatedOrRemoved(changes, Node.class);
115         if (nodeCreateOrUpdate != null) {
116             transaction = db.newReadWriteTransaction();
117             for (Entry<InstanceIdentifier<Node>, Node> entry: nodeCreateOrUpdate.entrySet()) {
118                 Optional<Node> readNode = HwvtepSouthboundUtil.readNode(transaction, entry.getKey());
119                 //add related globalNode or physicalSwitchNode to operationalNodes map
120                 //for example, when creating physical port, logical switch is needed
121                 //but logical switch is in HwvtepGlobalAugmentation rather than PhysicalSwitchAugmentation
122                 if (readNode.isPresent()) {
123                     operationalNodes.put(entry.getKey(), readNode.get());
124                     HwvtepGlobalAugmentation hgAugmentation = readNode.get().getAugmentation(HwvtepGlobalAugmentation.class);
125                     PhysicalSwitchAugmentation psAugmentation = readNode.get().getAugmentation(PhysicalSwitchAugmentation.class);
126                     if (hgAugmentation != null && hgAugmentation.getSwitches() != null) {
127                         for (Switches pswitch : hgAugmentation.getSwitches()) {
128                             @SuppressWarnings("unchecked")
129                             InstanceIdentifier<Node> psNodeIid = (InstanceIdentifier<Node>) pswitch.getSwitchRef().getValue();
130                             Optional<Node> psNode = HwvtepSouthboundUtil.readNode(transaction, psNodeIid);
131                             if (psNode.isPresent()) {
132                                 operationalNodes.put(psNodeIid, psNode.get());
133                             }
134                         }
135                     }
136                     if (psAugmentation != null) {
137                         @SuppressWarnings("unchecked")
138                         InstanceIdentifier<Node> hgNodeIid = (InstanceIdentifier<Node>) psAugmentation.getManagedBy().getValue();
139                         Optional<Node> hgNode = HwvtepSouthboundUtil.readNode(transaction, hgNodeIid);
140                         if (hgNode.isPresent()) {
141                             operationalNodes.put(hgNodeIid, hgNode.get());
142                         }
143                     }
144                 }
145             }
146         }
147     }
148
149     public Optional<Node> getGlobalNode(InstanceIdentifier<?> iid) {
150         InstanceIdentifier<Node> nodeIid = iid.firstIdentifierOf(Node.class);
151         return Optional.fromNullable(operationalNodes.get(nodeIid));
152     }
153
154     public Optional<HwvtepGlobalAugmentation> getHwvtepGlobalAugmentation(InstanceIdentifier<?> iid) {
155         Preconditions.checkNotNull(iid);
156         Optional<Node> nodeOptional = getGlobalNode(iid);
157         if (nodeOptional.isPresent()) {
158             return Optional.fromNullable(nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class));
159         }
160         return Optional.absent();
161     }
162
163     public Optional<PhysicalSwitchAugmentation> getPhysicalSwitchAugmentation(InstanceIdentifier<?> iid) {
164         Preconditions.checkNotNull(iid);
165         Optional<Node> nodeOptional = getGlobalNode(iid);
166         if (nodeOptional.isPresent()) {
167             return Optional.fromNullable(nodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class));
168         }
169         return Optional.absent();
170     }
171
172     public Optional<List<TerminationPoint>> getTerminationPointList(InstanceIdentifier<?> iid) {
173         Preconditions.checkNotNull(iid);
174         Optional<Node> nodeOptional = getGlobalNode(iid);
175         if (nodeOptional.isPresent() && nodeOptional.get().getTerminationPoint() != null) {
176             return Optional.fromNullable(nodeOptional.get().getTerminationPoint());
177         }
178         return Optional.absent();
179     }
180
181     public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<?> iid, LogicalSwitchesKey logicalSwitchesKey) {
182         Preconditions.checkNotNull(iid);
183         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
184         if (nodeOptional.isPresent()) {
185             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
186             List<LogicalSwitches> lswitchList = null;
187             if (hgAugmentation != null) {
188                 lswitchList = hgAugmentation.getLogicalSwitches();
189             }
190             if (lswitchList != null) {
191                 for (LogicalSwitches lswitch: lswitchList) {
192                     if (lswitch.getKey().equals(logicalSwitchesKey)) {
193                         return Optional.fromNullable(lswitch);
194                     }
195                 }
196             }
197         }
198         return Optional.absent();
199     }
200
201     public Optional<Tunnels> getTunnels(InstanceIdentifier<?> iid, TunnelsKey tunnelsKey) {
202         Preconditions.checkNotNull(iid);
203         Optional<PhysicalSwitchAugmentation> psOptional = getPhysicalSwitchAugmentation(iid);
204         if (psOptional.isPresent()) {
205             PhysicalSwitchAugmentation psAugmentation = psOptional.get();
206             List<Tunnels> tunnelList = null;
207             if (psAugmentation != null) {
208                 tunnelList = psAugmentation.getTunnels();
209             }
210             if (tunnelList != null) {
211                 for (Tunnels tunnel: tunnelList) {
212                     if (tunnel.getKey().equals(tunnelsKey)) {
213                         return Optional.fromNullable(tunnel);
214                     }
215                 }
216             }
217         }
218         return Optional.absent();
219     }
220
221     public Optional<HwvtepPhysicalPortAugmentation> getPhysicalPortAugmentation(InstanceIdentifier<?> iid,
222             HwvtepNodeName hwvtepNodeName) {
223         Preconditions.checkNotNull(iid);
224         Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
225         if (nodeOptional.isPresent()) {
226             List<TerminationPoint> tpList = nodeOptional.get();
227             for (TerminationPoint tp : tpList) {
228                 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
229                 if (hppAugmentation != null && hppAugmentation.getHwvtepNodeName().equals(hwvtepNodeName)) {
230                     return Optional.fromNullable(hppAugmentation);
231                 }
232             }
233         }
234         return Optional.absent();
235     }
236
237     public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<?> iid,
238             IpAddress dstIp, Class<? extends EncapsulationTypeBase> encapType) {
239         Preconditions.checkNotNull(iid);
240         Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
241         if (nodeOptional.isPresent()) {
242             List<TerminationPoint> tpList = nodeOptional.get();
243             for (TerminationPoint tp : tpList) {
244                 HwvtepPhysicalLocatorAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
245                 if (hppAugmentation != null && hppAugmentation.getDstIp().equals(dstIp)
246                         && hppAugmentation.getEncapsulationType().equals(encapType)) {
247                     return Optional.fromNullable(hppAugmentation);
248                 }
249             }
250         }
251         return Optional.absent();
252     }
253
254     public Optional<LocalMcastMacs> getLocalMcastMacs(InstanceIdentifier<?> iid, LocalMcastMacsKey key) {
255         Preconditions.checkNotNull(iid);
256         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
257         if (nodeOptional.isPresent()) {
258             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
259             List<LocalMcastMacs> macList = null;
260             if (hgAugmentation != null) {
261                 macList = hgAugmentation.getLocalMcastMacs();
262             }
263             if (macList != null) {
264                 for (LocalMcastMacs mac: macList) {
265                     if (mac.getKey().equals(key)) {
266                         return Optional.fromNullable(mac);
267                     }
268                 }
269             }
270         }
271         return Optional.absent();
272     }
273
274     public Optional<RemoteMcastMacs> getRemoteMcastMacs(InstanceIdentifier<?> iid, RemoteMcastMacsKey key) {
275         Preconditions.checkNotNull(iid);
276         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
277         if (nodeOptional.isPresent()) {
278             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
279             List<RemoteMcastMacs> macList = null;
280             if (hgAugmentation != null) {
281                 macList = hgAugmentation.getRemoteMcastMacs();
282             }
283             if (macList != null) {
284                 for (RemoteMcastMacs mac: macList) {
285                     if (mac.getKey().equals(key)) {
286                         return Optional.fromNullable(mac);
287                     }
288                 }
289             }
290         }
291         return Optional.absent();
292     }
293
294     public Optional<LocalUcastMacs> getLocalUcastMacs(InstanceIdentifier<?> iid, LocalUcastMacsKey key) {
295         Preconditions.checkNotNull(iid);
296         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
297         if (nodeOptional.isPresent()) {
298             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
299             List<LocalUcastMacs> macList = null;
300             if (hgAugmentation != null) {
301                 macList = hgAugmentation.getLocalUcastMacs();
302             }
303             if (macList != null) {
304                 for (LocalUcastMacs mac: macList) {
305                     if (mac.getKey().equals(key)) {
306                         return Optional.fromNullable(mac);
307                     }
308                 }
309             }
310         }
311         return Optional.absent();
312     }
313
314     public Optional<RemoteUcastMacs> getRemoteUcastMacs(InstanceIdentifier<?> iid, RemoteUcastMacsKey key) {
315         Preconditions.checkNotNull(iid);
316         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
317         if (nodeOptional.isPresent()) {
318             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
319             List<RemoteUcastMacs> macList = null;
320             if (hgAugmentation != null) {
321                 macList = hgAugmentation.getRemoteUcastMacs();
322             }
323             if (macList != null) {
324                 for (RemoteUcastMacs mac: macList) {
325                     if (mac.getKey().equals(key)) {
326                         return Optional.fromNullable(mac);
327                     }
328                 }
329             }
330         }
331         return Optional.absent();
332     }
333
334     public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<TerminationPoint> iid) {
335         Optional<TerminationPoint> tp = HwvtepSouthboundUtil.readNode(transaction, iid);
336         if (tp.isPresent()) {
337             return Optional.fromNullable(tp.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class));
338         }
339         return Optional.absent();
340     }
341
342     public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<LogicalSwitches> iid) {
343         Optional<LogicalSwitches> lswitch = HwvtepSouthboundUtil.readNode(transaction, iid);
344         return lswitch;
345     }
346
347     public Optional<Tunnels> getTunnels(InstanceIdentifier<Tunnels> iid) {
348         Optional<Tunnels> tunnels = HwvtepSouthboundUtil.readNode(transaction, iid);
349         return tunnels;
350     }
351
352     public ReadWriteTransaction getReadWriteTransaction() {
353         return transaction;
354     }
355
356     public void setPhysicalLocatorInFlight(InstanceIdentifier<TerminationPoint> iid,
357                                            UUID uuid) {
358         inflightLocators.put(iid, uuid);
359     }
360
361     public UUID getPhysicalLocatorInFlight(InstanceIdentifier<TerminationPoint> iid) {
362         return inflightLocators.get(iid);
363     }
364
365     public HwvtepConnectionInstance getConnectionInstance() {
366         return connectionInstance;
367     }
368
369     public HwvtepDeviceInfo getDeviceInfo() {
370         return deviceInfo;
371     }
372
373     public void updateCurrentTxData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid) {
374         HwvtepSouthboundUtil.updateData(currentTxUUIDs, cls, key, uuid);
375     }
376
377     public void updateCurrentTxDeleteData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
378         HwvtepSouthboundUtil.updateData(currentTxDeletedKeys, cls, key, Boolean.TRUE);
379     }
380
381     public UUID getUUIDFromCurrentTx(Class<? extends Identifiable> cls, InstanceIdentifier key) {
382         return HwvtepSouthboundUtil.getData(currentTxUUIDs, cls, key);
383     }
384
385     public boolean isKeyPartOfCurrentTx(Class<? extends Identifiable> cls, InstanceIdentifier key) {
386         return HwvtepSouthboundUtil.containsKey(currentTxUUIDs, cls, key);
387     }
388
389     public Set<InstanceIdentifier> getDeletedKeysInCurrentTx(Class<? extends Identifiable> cls) {
390         if (currentTxDeletedKeys.containsKey(cls)) {
391             return currentTxDeletedKeys.get(cls).keySet();
392         }
393         return Collections.EMPTY_SET;
394     }
395
396     public List<? extends Identifiable> getUpdatedData(final InstanceIdentifier<Node> key,
397                                                        final Class<? extends Identifiable> cls) {
398         List<Identifiable> result = null;
399         if (modifiedData.get(key) != null && modifiedData.get(key).getLeft() != null) {
400             result = modifiedData.get(key).getLeft().get(cls);
401         }
402         if (result == null) {
403             result = Collections.EMPTY_LIST;
404         }
405         return result;
406     }
407
408     public List<? extends Identifiable> getDeletedData(final InstanceIdentifier<Node> key,
409                                                        final Class<? extends Identifiable> cls) {
410         List<Identifiable> result = null;
411         if (modifiedData.get(key) != null && modifiedData.get(key).getRight() != null) {
412             result = modifiedData.get(key).getRight().get(cls);
413         }
414         if (result == null) {
415             result = Collections.EMPTY_LIST;
416         }
417         return result;
418     }
419
420     public void setModifiedData(final Map<InstanceIdentifier<Node>,
421             Pair<Map<Class<? extends Identifiable>, List<Identifiable>>,
422                     Map<Class<? extends Identifiable>, List<Identifiable>>>> modifiedData) {
423         this.modifiedData = modifiedData;
424     }
425
426     public boolean isInReconciliation() {
427         return inReconciliation;
428     }
429
430     public void setInReconciliation(boolean inReconciliation) {
431         this.inReconciliation = inReconciliation;
432     }
433 }