2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound;
10 import com.google.common.collect.Sets;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Iterator;
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;
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.
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.
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.
54 public class HwvtepDeviceInfo {
56 private static final Logger LOG = LoggerFactory.getLogger(HwvtepDeviceInfo.class);
58 private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, Boolean>> availableInOperDs =
59 new ConcurrentHashMap<>();
61 public void markAvailableInOperDs(Class<? extends Identifiable> cls, InstanceIdentifier key) {
62 availableInOperDs.putIfAbsent(cls, new ConcurrentHashMap<>());
63 availableInOperDs.get(cls).put(key, Boolean.TRUE);
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);
71 public Boolean clearOperDsAvailability(Class<? extends Identifiable> cls, InstanceIdentifier key) {
72 availableInOperDs.putIfAbsent(cls, new ConcurrentHashMap<>());
73 return availableInOperDs.get(cls).remove(key);
76 public enum DeviceDataStatus {
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;
89 DeviceData(InstanceIdentifier key, UUID uuid, Object data, DeviceDataStatus status) {
94 if (status == DeviceDataStatus.IN_TRANSIT) {
95 intransitTimeStamp = System.currentTimeMillis();
99 public Object getData() {
103 public DeviceDataStatus getStatus() {
107 public UUID getUuid() {
111 public InstanceIdentifier getKey() {
115 public boolean isIntransitTimeExpired() {
116 return System.currentTimeMillis()
117 > intransitTimeStamp + HwvtepSouthboundConstants.IN_TRANSIT_STATE_EXPIRY_TIME_MILLIS;
120 public boolean isInTransitState() {
121 return status == DeviceDataStatus.IN_TRANSIT;
124 public boolean isAvailableInOperDs() {
129 public String toString() {
130 return key + " uuid:" + uuid + " data:" + data + " status:" + status;
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<>();
143 private final HwvtepConnectionInstance connectionInstance;
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;
156 public HwvtepDeviceInfo(HwvtepConnectionInstance hwvtepConnectionInstance) {
157 this.connectionInstance = hwvtepConnectionInstance;
158 this.dependencyQueue = new DependencyQueue(this);
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();
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());
180 public void putPhysicalSwitch(UUID uuid, PhysicalSwitch physicalSwitch) {
181 physicalSwitches.put(uuid, physicalSwitch);
184 public PhysicalSwitch getPhysicalSwitch(UUID uuid) {
185 return physicalSwitches.get(uuid);
188 public PhysicalSwitch removePhysicalSwitch(UUID uuid) {
189 return physicalSwitches.remove(uuid);
192 public Map<UUID, PhysicalSwitch> getPhysicalSwitches() {
193 return physicalSwitches;
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();
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());
215 public void putPhysicalSwitchForTunnel(UUID uuid, UUID psUUID) {
216 mapTunnelToPhysicalSwitch.put(uuid, psUUID);
219 public PhysicalSwitch getPhysicalSwitchForTunnel(UUID uuid) {
220 return physicalSwitches.get(mapTunnelToPhysicalSwitch.get(uuid));
223 public void removePhysicalSwitchForTunnel(UUID uuid) {
224 mapTunnelToPhysicalSwitch.remove(uuid);
227 public Map<UUID, UUID> getPhysicalSwitchesForTunnels() {
228 return mapTunnelToPhysicalSwitch;
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;
236 public boolean isConfigDataAvailable(Class<? extends Identifiable> cls, InstanceIdentifier key) {
237 return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key) != null;
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));
245 public DeviceData getConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
246 return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key);
249 public Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> getConfigData() {
250 return Collections.unmodifiableMap(configKeyVsData);
253 public void clearConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
254 HwvtepSouthboundUtil.clearData(configKeyVsData, cls, key);
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);
262 if (deviceData != null) {
263 uuid = deviceData.getUuid();
264 data = deviceData.getData();
266 HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
267 new DeviceData(key, uuid, data, DeviceDataStatus.IN_TRANSIT));
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);
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);
283 HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
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) {
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());
305 HwvtepSouthboundUtil.clearData(uuidVsData, cls, uuid);
306 HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
309 public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, UUID uuid) {
310 return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
313 public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
314 return HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
317 public Map<InstanceIdentifier, DeviceData> getDeviceOperData(Class<? extends Identifiable> cls) {
318 return opKeyVsData.get(cls);
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();
329 public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
330 DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
337 public <T extends Identifiable> void addJobToQueue(DependentJob<T> job) {
338 dependencyQueue.addToQueue(job);
341 public void onConfigDataAvailable() {
342 dependencyQueue.processReadyJobsFromConfigQueue(connectionInstance);
345 public synchronized void onOperDataAvailable() {
346 dependencyQueue.processReadyJobsFromOpQueue(connectionInstance);
349 public void scheduleTransaction(final TransactCommand transactCommand) {
350 dependencyQueue.submit(() -> connectionInstance.transact(transactCommand));
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));
360 clearDeviceOperData(cls, key);
365 public void incRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
366 if (reference == null || tep == null) {
369 tepIdReferences.computeIfAbsent(tep, (tepId) -> Sets.newConcurrentHashSet());
370 tepIdReferences.get(tep).add(reference);
373 public int getRefCount(InstanceIdentifier tep) {
374 return tepIdReferences.containsKey(tep) ? tepIdReferences.get(tep).size() : 0;
377 public Set<InstanceIdentifier> getRefCounts(InstanceIdentifier tep) {
378 return tepIdReferences.get(tep);
381 public void decRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
382 if (reference == null || tep == null || !tepIdReferences.containsKey(tep)) {
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);
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()));
400 Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs> ucasts = logicalSwitchVsUcasts.get(logicalSwitchKey);
401 if (ucasts != null) {
402 ucasts.entrySet().forEach((entry) -> removeRemoteUcast(logicalSwitchKey, entry.getKey()));
404 markKeyAsInTransit(LogicalSwitches.class, logicalSwitchKey);
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()));
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());
425 public void removeRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid,
426 InstanceIdentifier<RemoteMcastMacs> mcastIid) {
427 if (!logicalSwitchVsMcasts.containsKey(lsIid)) {
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()));
434 markKeyAsInTransit(RemoteMcastMacs.class, mcastIid);
437 public void removeRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
438 InstanceIdentifier<RemoteUcastMacs> ucastIid) {
439 if (!logicalSwitchVsUcasts.containsKey(lsIid)) {
442 RemoteUcastMacs mac = logicalSwitchVsUcasts.get(lsIid).remove(ucastIid);
444 decRefCount(ucastIid, mac.getLocatorRef().getValue());
446 markKeyAsInTransit(RemoteUcastMacs.class, ucastIid);
449 public HwvtepConnectionInstance getConnectionInstance() {
450 return connectionInstance;
453 public void setConfigKeyVsData(Map<Class<? extends Identifiable>, Map<InstanceIdentifier,
454 DeviceData>> configKeyVsData) {
455 this.configKeyVsData = configKeyVsData;
458 public void setControllerTxHistory(TransactionHistory controllerTxHistory) {
459 this.controllerTxHistory = controllerTxHistory;
462 public void setDeviceUpdateHistory(TransactionHistory deviceUpdateHistory) {
463 this.deviceUpdateHistory = deviceUpdateHistory;
466 public void addToControllerTx(TransactionType transactionType, Object object) {
467 controllerTxHistory.addToHistory(transactionType, object);
470 public void addToDeviceUpdate(TransactionType transactionType, Object object) {
471 deviceUpdateHistory.addToHistory(transactionType, object);
474 public Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> getOperData() {
475 return Collections.unmodifiableMap(opKeyVsData);
478 public Map<Class<? extends Identifiable>, Map<UUID, DeviceData>> getUuidData() {
479 return Collections.unmodifiableMap(uuidVsData);
482 public void putKeyInDependencyQueue(InstanceIdentifier iid) {
483 iidInQueueCount.putIfAbsent(iid, new AtomicInteger(0));
484 iidInQueueCount.get(iid).incrementAndGet();
487 public void clearKeyFromDependencyQueue(InstanceIdentifier iid) {
488 iidInQueueCount.remove(iid);
491 public boolean isKeyInDependencyQueue(InstanceIdentifier iid) {
492 return iidInQueueCount.containsKey(iid);