Update MRI projects for Aluminium
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepDeviceInfo.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.ovsdb.hwvtepsouthbound;
9
10 import com.google.common.collect.Sets;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.atomic.AtomicInteger;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependentJob;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
25 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
26 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
31 import org.opendaylight.yangtools.yang.binding.Identifiable;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /*
37  * HwvtepDeviceInfo is used to store some of the table entries received
38  * in updates from a Hwvtep device. There will be one instance of this per
39  * Hwvtep device connected. Table entries are stored in a map keyed by
40  * uuids of respective rows.
41  *
42  * Purpose of this class is to provide data present in tables which
43  * were updated in a previous transaction and are not available in
44  * current updatedRows. This allows us to handle updates for Tables
45  * which reference other tables and need information in those tables
46  * to add data to Operational data store.
47  *
48  * e.g. Mac-entries in data store use logical-switch-ref as one of the
49  * keys. Mac-entry updates from switch rarely contain Logical_Switch
50  * table entries. To add mac-entries we need table entries from
51  * Logical_Switch table which were created in an earlier update.
52  *
53  */
54 public class HwvtepDeviceInfo {
55
56     private static final Logger LOG = LoggerFactory.getLogger(HwvtepDeviceInfo.class);
57
58     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, Boolean>> availableInOperDs =
59             new ConcurrentHashMap<>();
60
61     public void markAvailableInOperDs(Class<? extends Identifiable> cls, InstanceIdentifier key) {
62         availableInOperDs.putIfAbsent(cls, new ConcurrentHashMap<>());
63         availableInOperDs.get(cls).put(key, Boolean.TRUE);
64     }
65
66     public Boolean isAvailableInOperDs(Class<? extends Identifiable> cls, InstanceIdentifier key) {
67         availableInOperDs.putIfAbsent(cls, new ConcurrentHashMap<>());
68         return availableInOperDs.get(cls).getOrDefault(key, Boolean.FALSE);
69     }
70
71     public Boolean clearOperDsAvailability(Class<? extends Identifiable> cls, InstanceIdentifier key) {
72         availableInOperDs.putIfAbsent(cls, new ConcurrentHashMap<>());
73         return availableInOperDs.get(cls).remove(key);
74     }
75
76     public enum DeviceDataStatus {
77         IN_TRANSIT,
78         UNAVAILABLE,
79         AVAILABLE
80     }
81
82     public static class DeviceData {
83         private final InstanceIdentifier key;
84         private final UUID uuid;
85         private final Object data;
86         private final DeviceDataStatus status;
87         private long intransitTimeStamp;
88
89         DeviceData(InstanceIdentifier key, UUID uuid, Object data, DeviceDataStatus status) {
90             this.data = data;
91             this.key = key;
92             this.status = status;
93             this.uuid = uuid;
94             if (status == DeviceDataStatus.IN_TRANSIT) {
95                 intransitTimeStamp = System.currentTimeMillis();
96             }
97         }
98
99         public Object getData() {
100             return data;
101         }
102
103         public DeviceDataStatus getStatus() {
104             return status;
105         }
106
107         public UUID getUuid() {
108             return uuid;
109         }
110
111         public InstanceIdentifier getKey() {
112             return key;
113         }
114
115         public boolean isIntransitTimeExpired() {
116             return System.currentTimeMillis()
117                     > intransitTimeStamp + HwvtepSouthboundConstants.IN_TRANSIT_STATE_EXPIRY_TIME_MILLIS;
118         }
119
120         public boolean isInTransitState() {
121             return status == DeviceDataStatus.IN_TRANSIT;
122         }
123
124         public boolean isAvailableInOperDs() {
125             return false;
126         }
127
128         @Override
129         public String toString() {
130             return key + " uuid:" + uuid + " data:" + data + " status:" + status;
131         }
132     }
133
134     private static AtomicInteger ZERO = new AtomicInteger(0);
135     private final Map<InstanceIdentifier<?>, Set<InstanceIdentifier>> tepIdReferences = new ConcurrentHashMap<>();
136     private final Map<InstanceIdentifier<LogicalSwitches>, Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs>>
137             logicalSwitchVsUcasts = new ConcurrentHashMap<>();
138     private final Map<InstanceIdentifier<LogicalSwitches>, Map<InstanceIdentifier<RemoteMcastMacs>, RemoteMcastMacs>>
139             logicalSwitchVsMcasts = new ConcurrentHashMap<>();
140     private final Map<UUID, PhysicalSwitch> physicalSwitches = new ConcurrentHashMap<>();
141     private final Map<UUID, UUID> mapTunnelToPhysicalSwitch = new ConcurrentHashMap<>();
142
143     private final HwvtepConnectionInstance connectionInstance;
144
145     private Map<InstanceIdentifier, AtomicInteger> iidInQueueCount = new ConcurrentHashMap<>();
146     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> configKeyVsData =
147             new ConcurrentHashMap<>();
148     private final Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> opKeyVsData =
149             new ConcurrentHashMap<>();
150     private final Map<Class<? extends Identifiable>, Map<UUID, DeviceData>> uuidVsData = new ConcurrentHashMap<>();
151     private final DependencyQueue dependencyQueue;
152     private TransactionHistory controllerTxHistory;
153     private TransactionHistory deviceUpdateHistory;
154
155
156     public HwvtepDeviceInfo(HwvtepConnectionInstance hwvtepConnectionInstance) {
157         this.connectionInstance = hwvtepConnectionInstance;
158         this.dependencyQueue = new DependencyQueue(this);
159     }
160
161     public LogicalSwitch getLogicalSwitch(UUID uuid) {
162         DeviceData deviceData = getDeviceOperData(LogicalSwitches.class, uuid);
163         if (deviceData != null && deviceData.getData() != null) {
164             return (LogicalSwitch) deviceData.getData();
165         }
166         return null;
167     }
168
169     public Map<UUID, LogicalSwitch> getLogicalSwitches() {
170         Map<UUID, DeviceData> switches = uuidVsData.get(LogicalSwitches.class);
171         Map<UUID, LogicalSwitch> result = new HashMap<>();
172         if (switches != null) {
173             for (Map.Entry<UUID, DeviceData> entry : switches.entrySet()) {
174                 result.put(entry.getKey(), (LogicalSwitch) entry.getValue().getData());
175             }
176         }
177         return result;
178     }
179
180     public void putPhysicalSwitch(UUID uuid, PhysicalSwitch physicalSwitch) {
181         physicalSwitches.put(uuid, physicalSwitch);
182     }
183
184     public PhysicalSwitch getPhysicalSwitch(UUID uuid) {
185         return physicalSwitches.get(uuid);
186     }
187
188     public PhysicalSwitch removePhysicalSwitch(UUID uuid) {
189         return physicalSwitches.remove(uuid);
190     }
191
192     public Map<UUID, PhysicalSwitch> getPhysicalSwitches() {
193         return physicalSwitches;
194     }
195
196     public PhysicalLocator getPhysicalLocator(UUID uuid) {
197         DeviceData deviceData = getDeviceOperData(TerminationPoint.class, uuid);
198         if (deviceData != null && deviceData.getData() != null) {
199             return (PhysicalLocator) deviceData.getData();
200         }
201         return null;
202     }
203
204     public Map<UUID, PhysicalLocator> getPhysicalLocators() {
205         Map<UUID, DeviceData> locators = uuidVsData.get(TerminationPoint.class);
206         Map<UUID, PhysicalLocator> result = new HashMap<>();
207         if (locators != null) {
208             for (Map.Entry<UUID, DeviceData> entry : locators.entrySet()) {
209                 result.put(entry.getKey(), (PhysicalLocator) entry.getValue().getData());
210             }
211         }
212         return result;
213     }
214
215     public void putPhysicalSwitchForTunnel(UUID uuid, UUID psUUID) {
216         mapTunnelToPhysicalSwitch.put(uuid, psUUID);
217     }
218
219     public PhysicalSwitch getPhysicalSwitchForTunnel(UUID uuid) {
220         return physicalSwitches.get(mapTunnelToPhysicalSwitch.get(uuid));
221     }
222
223     public void removePhysicalSwitchForTunnel(UUID uuid) {
224         mapTunnelToPhysicalSwitch.remove(uuid);
225     }
226
227     public Map<UUID, UUID> getPhysicalSwitchesForTunnels() {
228         return mapTunnelToPhysicalSwitch;
229     }
230
231     public boolean isKeyInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
232         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
233         return deviceData != null && DeviceDataStatus.IN_TRANSIT == deviceData.status;
234     }
235
236     public boolean isConfigDataAvailable(Class<? extends Identifiable> cls, InstanceIdentifier key) {
237         return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key) != null;
238     }
239
240     public void updateConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key, Object data) {
241         HwvtepSouthboundUtil.updateData(configKeyVsData, cls, key,
242                 new DeviceData(key, null, data, DeviceDataStatus.AVAILABLE));
243     }
244
245     public DeviceData getConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
246         return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key);
247     }
248
249     public Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> getConfigData() {
250         return Collections.unmodifiableMap(configKeyVsData);
251     }
252
253     public void clearConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
254         HwvtepSouthboundUtil.clearData(configKeyVsData, cls, key);
255     }
256
257     public void markKeyAsInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
258         LOG.debug("Marking device data as intransit {}", key);
259         DeviceData deviceData = getDeviceOperData(cls, key);
260         UUID uuid = null;
261         Object data = null;
262         if (deviceData != null) {
263             uuid = deviceData.getUuid();
264             data = deviceData.getData();
265         }
266         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
267                 new DeviceData(key, uuid, data, DeviceDataStatus.IN_TRANSIT));
268     }
269
270     public void updateDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key,
271             UUID uuid, Object data) {
272         LOG.debug("Updating device data {}", key);
273         DeviceData deviceData = new DeviceData(key, uuid, data, DeviceDataStatus.AVAILABLE);
274         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key, deviceData);
275         HwvtepSouthboundUtil.updateData(uuidVsData, cls, uuid, deviceData);
276     }
277
278     public void clearDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
279         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
280         if (deviceData != null && deviceData.uuid != null) {
281             HwvtepSouthboundUtil.clearData(uuidVsData, cls, deviceData.uuid);
282         }
283         HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
284     }
285
286     public void clearDeviceOperData(Class<? extends Identifiable> cls) {
287         Map<InstanceIdentifier, DeviceData> iids = opKeyVsData.get(cls);
288         if (iids != null && !iids.isEmpty()) {
289             Iterator<Map.Entry<InstanceIdentifier, DeviceData>> it = iids.entrySet().iterator();
290             while (it.hasNext()) {
291                 Map.Entry<InstanceIdentifier, DeviceData> entry = it.next();
292                 DeviceData deviceData = entry.getValue();
293                 if (deviceData != null && deviceData.getStatus() != DeviceDataStatus.IN_TRANSIT) {
294                     it.remove();
295                 }
296             }
297         }
298     }
299
300     public void clearDeviceOperUUID(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid) {
301         LOG.debug("Clearing device data {}", key);
302         if (uuidVsData.containsKey(cls) && uuidVsData.get(cls).containsKey(uuid)) {
303             LOG.debug("Remove {} {} from device data.", connectionInstance.getNodeId().getValue(), cls.getSimpleName());
304         }
305         HwvtepSouthboundUtil.clearData(uuidVsData, cls, uuid);
306         HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
307     }
308
309     public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, UUID uuid) {
310         return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
311     }
312
313     public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
314         return HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
315     }
316
317     public Map<InstanceIdentifier, DeviceData> getDeviceOperData(Class<? extends Identifiable> cls) {
318         return opKeyVsData.get(cls);
319     }
320
321     public InstanceIdentifier getDeviceOperKey(final Class<? extends Identifiable> cls, final UUID uuid) {
322         DeviceData deviceData = HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
323         if (deviceData != null) {
324             return deviceData.getKey();
325         }
326         return null;
327     }
328
329     public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
330         DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
331         if (data != null) {
332             return data.uuid;
333         }
334         return null;
335     }
336
337     public <T extends Identifiable> void addJobToQueue(DependentJob<T> job) {
338         dependencyQueue.addToQueue(job);
339     }
340
341     public void onConfigDataAvailable() {
342         dependencyQueue.processReadyJobsFromConfigQueue(connectionInstance);
343     }
344
345     public synchronized void onOperDataAvailable() {
346         dependencyQueue.processReadyJobsFromOpQueue(connectionInstance);
347     }
348
349     public void scheduleTransaction(final TransactCommand transactCommand) {
350         dependencyQueue.submit(() -> connectionInstance.transact(transactCommand));
351     }
352
353     public void clearInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
354         DeviceData deviceData = getDeviceOperData(cls, key);
355         if (deviceData != null && deviceData.isInTransitState()) {
356             if (deviceData.getData() != null) {
357                 HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
358                         new DeviceData(key, deviceData.getUuid(), deviceData.getData(), DeviceDataStatus.AVAILABLE));
359             } else {
360                 clearDeviceOperData(cls, key);
361             }
362         }
363     }
364
365     public void incRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
366         if (reference == null || tep == null) {
367             return;
368         }
369         tepIdReferences.computeIfAbsent(tep, (tepId) -> Sets.newConcurrentHashSet());
370         tepIdReferences.get(tep).add(reference);
371     }
372
373     public int getRefCount(InstanceIdentifier tep) {
374         return tepIdReferences.containsKey(tep) ? tepIdReferences.get(tep).size() : 0;
375     }
376
377     public Set<InstanceIdentifier> getRefCounts(InstanceIdentifier tep) {
378         return tepIdReferences.get(tep);
379     }
380
381     public void decRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
382         if (reference == null || tep == null || !tepIdReferences.containsKey(tep)) {
383             return;
384         }
385         //synchronize to make sure that no two parallel deletes puts the key in transit state twice
386         synchronized (this) {
387             boolean removed = tepIdReferences.get(tep).remove(reference);
388             if (removed && tepIdReferences.get(tep).isEmpty()) {
389                 LOG.debug("Marking the termination point as in transit ref count zero {} ", tep);
390                 markKeyAsInTransit(TerminationPoint.class, tep);
391             }
392         }
393     }
394
395     public void clearLogicalSwitchRefs(InstanceIdentifier<LogicalSwitches> logicalSwitchKey) {
396         Map<InstanceIdentifier<RemoteMcastMacs>, RemoteMcastMacs> mcasts = logicalSwitchVsMcasts.get(logicalSwitchKey);
397         if (mcasts != null) {
398             mcasts.entrySet().forEach((entry) -> removeRemoteMcast(logicalSwitchKey, entry.getKey()));
399         }
400         Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs> ucasts = logicalSwitchVsUcasts.get(logicalSwitchKey);
401         if (ucasts != null) {
402             ucasts.entrySet().forEach((entry) -> removeRemoteUcast(logicalSwitchKey, entry.getKey()));
403         }
404         markKeyAsInTransit(LogicalSwitches.class, logicalSwitchKey);
405     }
406
407     public  void updateRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid,
408                                    InstanceIdentifier<RemoteMcastMacs> mcastIid,
409                                    RemoteMcastMacs mac) {
410         logicalSwitchVsMcasts.computeIfAbsent(lsIid, (lsKey) -> new ConcurrentHashMap<>());
411         logicalSwitchVsMcasts.get(lsIid).put(mcastIid, mac);
412         if (mac.getLocatorSet() != null) {
413             mac.getLocatorSet().forEach((iid) -> incRefCount(mcastIid, iid.getLocatorRef().getValue()));
414         }
415     }
416
417     public  void updateRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
418                                    InstanceIdentifier<RemoteUcastMacs> ucastIid,
419                                    RemoteUcastMacs mac) {
420         logicalSwitchVsUcasts.computeIfAbsent(lsIid, (lsKey) -> new ConcurrentHashMap<>());
421         logicalSwitchVsUcasts.get(lsIid).put(ucastIid, mac);
422         incRefCount(ucastIid, mac.getLocatorRef().getValue());
423     }
424
425     public void removeRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid,
426             InstanceIdentifier<RemoteMcastMacs> mcastIid) {
427         if (!logicalSwitchVsMcasts.containsKey(lsIid)) {
428             return;
429         }
430         RemoteMcastMacs mac = logicalSwitchVsMcasts.get(lsIid).remove(mcastIid);
431         if (mac != null && mac.getLocatorSet() != null) {
432             mac.getLocatorSet().forEach((iid) -> decRefCount(mcastIid, iid.getLocatorRef().getValue()));
433         }
434         markKeyAsInTransit(RemoteMcastMacs.class, mcastIid);
435     }
436
437     public void removeRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
438                                    InstanceIdentifier<RemoteUcastMacs> ucastIid) {
439         if (!logicalSwitchVsUcasts.containsKey(lsIid)) {
440             return;
441         }
442         RemoteUcastMacs mac = logicalSwitchVsUcasts.get(lsIid).remove(ucastIid);
443         if (mac != null) {
444             decRefCount(ucastIid, mac.getLocatorRef().getValue());
445         }
446         markKeyAsInTransit(RemoteUcastMacs.class, ucastIid);
447     }
448
449     public HwvtepConnectionInstance getConnectionInstance() {
450         return connectionInstance;
451     }
452
453     public void setConfigKeyVsData(Map<Class<? extends Identifiable>, Map<InstanceIdentifier,
454             DeviceData>> configKeyVsData) {
455         this.configKeyVsData = configKeyVsData;
456     }
457
458     public void setControllerTxHistory(TransactionHistory controllerTxHistory) {
459         this.controllerTxHistory = controllerTxHistory;
460     }
461
462     public void setDeviceUpdateHistory(TransactionHistory deviceUpdateHistory) {
463         this.deviceUpdateHistory = deviceUpdateHistory;
464     }
465
466     public void addToControllerTx(TransactionType transactionType, Object object) {
467         controllerTxHistory.addToHistory(transactionType, object);
468     }
469
470     public void addToDeviceUpdate(TransactionType transactionType, Object object) {
471         deviceUpdateHistory.addToHistory(transactionType, object);
472     }
473
474     public Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> getOperData() {
475         return Collections.unmodifiableMap(opKeyVsData);
476     }
477
478     public Map<Class<? extends Identifiable>, Map<UUID, DeviceData>> getUuidData() {
479         return Collections.unmodifiableMap(uuidVsData);
480     }
481
482     public void putKeyInDependencyQueue(InstanceIdentifier iid) {
483         iidInQueueCount.putIfAbsent(iid, new AtomicInteger(0));
484         iidInQueueCount.get(iid).incrementAndGet();
485     }
486
487     public void clearKeyFromDependencyQueue(InstanceIdentifier iid) {
488         iidInQueueCount.remove(iid);
489     }
490
491     public boolean isKeyInDependencyQueue(InstanceIdentifier iid) {
492         return iidInQueueCount.containsKey(iid);
493     }
494
495 }