ec637a98abb77b83fb76330c3dd6140b2eba2cfd
[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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
11 import com.google.common.collect.Sets;
12 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
13 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependentJob;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
16 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
17 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
18 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Identifiable;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.concurrent.ConcurrentHashMap;
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     public enum DeviceDataStatus {
59         IN_TRANSIT,
60         UNAVAILABLE,
61         AVAILABLE
62     }
63
64     public static class DeviceData {
65         private final InstanceIdentifier key;
66         private final UUID uuid;
67         private final Object data;
68         private final DeviceDataStatus status;
69         private long intransitTimeStamp;
70
71         DeviceData(InstanceIdentifier key, UUID uuid, Object data, DeviceDataStatus status) {
72             this.data = data;
73             this.key = key;
74             this.status = status;
75             this.uuid = uuid;
76             if (status == DeviceDataStatus.IN_TRANSIT) {
77                 intransitTimeStamp = System.currentTimeMillis();
78             }
79         }
80
81         public Object getData() {
82             return data;
83         }
84
85         public DeviceDataStatus getStatus() {
86             return status;
87         }
88
89         public UUID getUuid() {
90             return uuid;
91         }
92
93         public InstanceIdentifier getKey() {
94             return key;
95         }
96
97         public boolean isIntransitTimeExpired() {
98             return System.currentTimeMillis()
99                     > intransitTimeStamp + HwvtepSouthboundConstants.IN_TRANSIT_STATE_EXPIRY_TIME_MILLIS;
100         }
101
102         public boolean isInTransitState() {
103             return status == DeviceDataStatus.IN_TRANSIT;
104         }
105     }
106
107     private Map<InstanceIdentifier, Set<InstanceIdentifier>> tepIdReferences;
108     private Map<InstanceIdentifier<LogicalSwitches>, Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs>> logicalSwitchVsUcasts;
109     private Map<InstanceIdentifier<LogicalSwitches>, Map<InstanceIdentifier<RemoteMcastMacs>, RemoteMcastMacs>> logicalSwitchVsMcasts;
110     private Map<UUID, LogicalSwitch> logicalSwitches = null;
111     private Map<UUID, PhysicalSwitch> physicalSwitches = null;
112     private Map<UUID, PhysicalLocator> physicalLocators = null;
113     private Map<UUID, UUID> mapTunnelToPhysicalSwitch = null;
114
115     private HwvtepConnectionInstance connectionInstance;
116
117     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> configKeyVsData = new ConcurrentHashMap<>();
118     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> opKeyVsData = new ConcurrentHashMap<>();
119     private Map<Class<? extends Identifiable>, Map<UUID, Object>> uuidVsData = new ConcurrentHashMap<>();
120     private DependencyQueue dependencyQueue;
121
122     public HwvtepDeviceInfo(HwvtepConnectionInstance hwvtepConnectionInstance) {
123         this.connectionInstance = hwvtepConnectionInstance;
124         this.logicalSwitches = new ConcurrentHashMap<>();
125         this.physicalSwitches = new ConcurrentHashMap<>();
126         this.physicalLocators = new ConcurrentHashMap<>();
127         this.mapTunnelToPhysicalSwitch = new ConcurrentHashMap<>();
128         this.tepIdReferences = new ConcurrentHashMap<>();
129         this.logicalSwitchVsUcasts = new ConcurrentHashMap<>();
130         this.logicalSwitchVsMcasts = new ConcurrentHashMap<>();
131         this.dependencyQueue = new DependencyQueue(this);
132     }
133
134     public LogicalSwitch getLogicalSwitch(UUID uuid) {
135         return (LogicalSwitch) getDeviceOperData(LogicalSwitches.class, uuid);
136     }
137
138     public Map<UUID, LogicalSwitch> getLogicalSwitches() {
139         Map<UUID, Object> switches = uuidVsData.get(LogicalSwitches.class);
140         Map<UUID, LogicalSwitch> result = new HashMap<>();
141         if (switches != null) {
142             for (Map.Entry<UUID, Object> entry : switches.entrySet()) {
143                 result.put(entry.getKey(), (LogicalSwitch) entry.getValue());
144             }
145         }
146         return result;
147     }
148
149     public void putPhysicalSwitch(UUID uuid, PhysicalSwitch pSwitch) {
150         physicalSwitches.put(uuid, pSwitch);
151     }
152
153     public PhysicalSwitch getPhysicalSwitch(UUID uuid) {
154         return physicalSwitches.get(uuid);
155     }
156
157     public PhysicalSwitch removePhysicalSwitch(UUID uuid) {
158         return physicalSwitches.remove(uuid);
159     }
160
161     public Map<UUID, PhysicalSwitch> getPhysicalSwitches() {
162         return physicalSwitches;
163     }
164
165     public PhysicalLocator getPhysicalLocator(UUID uuid) {
166         return (PhysicalLocator) getDeviceOperData(TerminationPoint.class, uuid);
167     }
168
169     public Map<UUID, PhysicalLocator> getPhysicalLocators() {
170         Map<UUID, Object> locators = uuidVsData.get(TerminationPoint.class);
171         Map<UUID, PhysicalLocator> result = new HashMap<>();
172         if (locators != null) {
173             for (Map.Entry<UUID, Object> entry : locators.entrySet()) {
174                 result.put(entry.getKey(), (PhysicalLocator) entry.getValue());
175             }
176         }
177         return result;
178     }
179
180     public void putPhysicalSwitchForTunnel(UUID uuid, UUID psUUID) {
181         mapTunnelToPhysicalSwitch.put(uuid, psUUID);
182     }
183
184     public PhysicalSwitch getPhysicalSwitchForTunnel(UUID uuid) {
185         return physicalSwitches.get(mapTunnelToPhysicalSwitch.get(uuid));
186     }
187
188     public void removePhysicalSwitchForTunnel(UUID uuid) {
189         mapTunnelToPhysicalSwitch.remove(uuid);
190     }
191
192     public Map<UUID, UUID> getPhysicalSwitchesForTunnels() {
193         return mapTunnelToPhysicalSwitch;
194     }
195
196     public boolean isKeyInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
197         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
198         return deviceData != null && DeviceDataStatus.IN_TRANSIT == deviceData.status;
199     }
200
201     public boolean isConfigDataAvailable(Class<? extends Identifiable> cls, InstanceIdentifier key) {
202         return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key) != null;
203     }
204
205     public void updateConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key, Object data) {
206         HwvtepSouthboundUtil.updateData(configKeyVsData, cls, key,
207                 new DeviceData(key, null, data, DeviceDataStatus.AVAILABLE));
208     }
209
210     public Object getConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
211         DeviceData deviceData = HwvtepSouthboundUtil.getData(configKeyVsData, cls, key);
212         if (deviceData != null) {
213             return deviceData.getData();
214         }
215         return null;
216     }
217
218     public void clearConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
219         HwvtepSouthboundUtil.clearData(configKeyVsData, cls, key);
220     }
221
222     public void markKeyAsInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
223         LOG.debug("Marking device data as intransit {}", key);
224         DeviceData deviceData = getDeviceOperData(cls, key);
225         UUID uuid = null;
226         Object data = null;
227         if (deviceData != null) {
228             uuid = deviceData.getUuid();
229             data = deviceData.getData();
230         }
231         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
232                 new DeviceData(key, uuid, data, DeviceDataStatus.IN_TRANSIT));
233     }
234
235     public void updateDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid, Object data) {
236         LOG.debug("Updating device data {}", key);
237         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
238                 new DeviceData(key, uuid, data, DeviceDataStatus.AVAILABLE));
239         HwvtepSouthboundUtil.updateData(uuidVsData, cls, uuid, data);
240     }
241
242     public void clearDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
243         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
244         if (deviceData != null && deviceData.uuid != null) {
245             HwvtepSouthboundUtil.clearData(uuidVsData, cls, deviceData.uuid);
246         }
247         HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
248     }
249
250     public Object getDeviceOperData(Class<? extends Identifiable> cls, UUID uuid) {
251         return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
252     }
253
254     public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
255         return HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
256     }
257
258     public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
259         DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
260         if (data != null) {
261             return data.uuid;
262         }
263         return null;
264     }
265
266     public <T extends Identifiable> void addJobToQueue(DependentJob<T> job) {
267         dependencyQueue.addToQueue(job);
268     }
269
270     public void onConfigDataAvailable() {
271         dependencyQueue.processReadyJobsFromConfigQueue(connectionInstance);
272     }
273
274     public synchronized void onOperDataAvailable() {
275         dependencyQueue.processReadyJobsFromOpQueue(connectionInstance);
276     }
277
278     public void scheduleTransaction(final TransactCommand transactCommand) {
279         dependencyQueue.submit(() -> connectionInstance.transact(transactCommand));
280     }
281
282     public void clearDeviceOperData(Class<? extends Identifiable> cls) {
283         Map<InstanceIdentifier, DeviceData> iids = opKeyVsData.get(cls);
284         if (iids != null && !iids.isEmpty()) {
285             Iterator<Map.Entry<InstanceIdentifier, DeviceData>> it = iids.entrySet().iterator();
286             while (it.hasNext()) {
287                 Map.Entry<InstanceIdentifier, DeviceData> entry = it.next();
288                 DeviceData deviceData = entry.getValue();
289                 if (deviceData != null && deviceData.getStatus() != DeviceDataStatus.IN_TRANSIT) {
290                     it.remove();
291                 }
292             }
293         }
294     }
295
296     public void clearInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
297         DeviceData deviceData = getDeviceOperData(cls, key);
298         if (deviceData != null && deviceData.isInTransitState()) {
299             if (deviceData.getData() != null) {
300                 HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
301                         new DeviceData(key, deviceData.getUuid(), deviceData.getData(), DeviceDataStatus.AVAILABLE));
302             } else {
303                 clearDeviceOperData(cls, key);
304             }
305         }
306     }
307
308     public Map<InstanceIdentifier, DeviceData> getDeviceOperData(Class<? extends Identifiable> cls) {
309         return opKeyVsData.get(cls);
310     }
311
312     public void incRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
313         if (reference == null || tep == null) {
314             return;
315         }
316         tepIdReferences.computeIfAbsent(tep, (tepId) -> Sets.newConcurrentHashSet());
317         tepIdReferences.get(tep).add(reference);
318     }
319
320     public int getRefCount(InstanceIdentifier tep) {
321         return tepIdReferences.containsKey(tep) ? tepIdReferences.get(tep).size() : 0;
322     }
323
324     public Set<InstanceIdentifier> getRefCounts(InstanceIdentifier tep) {
325         return tepIdReferences.get(tep);
326     }
327
328     public void decRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
329         if (reference == null || tep == null || !tepIdReferences.containsKey(tep)) {
330             return;
331         }
332         //synchronize to make sure that no two parallel deletes puts the key in transit state twice
333         synchronized (this) {
334             boolean removed = tepIdReferences.get(tep).remove(reference);
335             if (removed && tepIdReferences.get(tep).isEmpty()) {
336                 LOG.debug("Marking the termination point as in transit ref count zero {} ", tep);
337                 markKeyAsInTransit(TerminationPoint.class, tep);
338             }
339         }
340     }
341
342     public void clearLogicalSwitchRefs(InstanceIdentifier<LogicalSwitches> logicalSwitchKey) {
343         Map<InstanceIdentifier<RemoteMcastMacs>, RemoteMcastMacs> mcasts = logicalSwitchVsMcasts.get(logicalSwitchKey);
344         if (mcasts != null ) {
345             mcasts.entrySet().forEach( (entry) -> removeRemoteMcast(logicalSwitchKey, entry.getKey()));
346         }
347         Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs> ucasts = logicalSwitchVsUcasts.get(logicalSwitchKey);
348         if (ucasts != null ) {
349             ucasts.entrySet().forEach( (entry) -> removeRemoteUcast(logicalSwitchKey, entry.getKey()));
350         }
351         markKeyAsInTransit(LogicalSwitches.class, logicalSwitchKey);
352     }
353
354     public  void updateRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid,
355                                    InstanceIdentifier<RemoteMcastMacs> mcastIid,
356                                    RemoteMcastMacs mac) {
357         logicalSwitchVsMcasts.computeIfAbsent(lsIid, (lsKey) -> new ConcurrentHashMap<>());
358         logicalSwitchVsMcasts.get(lsIid).put(mcastIid, mac);
359         if (mac.getLocatorSet() != null) {
360             mac.getLocatorSet().forEach( (iid) -> incRefCount(mcastIid, iid.getLocatorRef().getValue()));
361         }
362     }
363
364     public  void updateRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
365                                    InstanceIdentifier<RemoteUcastMacs> ucastIid,
366                                    RemoteUcastMacs mac) {
367         logicalSwitchVsUcasts.computeIfAbsent(lsIid, (lsKey) -> new ConcurrentHashMap<>());
368         logicalSwitchVsUcasts.get(lsIid).put(ucastIid, mac);
369         incRefCount(ucastIid, mac.getLocatorRef().getValue());
370     }
371
372     public  void removeRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid, InstanceIdentifier<RemoteMcastMacs> mcastIid) {
373         if (!logicalSwitchVsMcasts.containsKey(lsIid)) {
374             return;
375         }
376         RemoteMcastMacs mac = logicalSwitchVsMcasts.get(lsIid).remove(mcastIid);
377         if (mac != null && mac.getLocatorSet() != null) {
378             mac.getLocatorSet().forEach((iid) -> decRefCount(mcastIid, iid.getLocatorRef().getValue()));
379         }
380         markKeyAsInTransit(RemoteMcastMacs.class, mcastIid);
381     }
382
383     public void removeRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
384                                    InstanceIdentifier<RemoteUcastMacs> ucastIid) {
385         if (!logicalSwitchVsUcasts.containsKey(lsIid)) {
386             return;
387         }
388         RemoteUcastMacs mac = logicalSwitchVsUcasts.get(lsIid).remove(ucastIid);
389         if (mac != null) {
390             decRefCount(ucastIid, mac.getLocatorRef().getValue());
391         }
392         markKeyAsInTransit(RemoteUcastMacs.class, ucastIid);
393     }
394
395     public HwvtepConnectionInstance getConnectionInstance() {
396         return connectionInstance;
397     }
398 }