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