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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
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;
30 import java.util.HashMap;
31 import java.util.Iterator;
34 import java.util.concurrent.ConcurrentHashMap;
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 public enum DeviceDataStatus {
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;
71 DeviceData(InstanceIdentifier key, UUID uuid, Object data, DeviceDataStatus status) {
76 if (status == DeviceDataStatus.IN_TRANSIT) {
77 intransitTimeStamp = System.currentTimeMillis();
81 public Object getData() {
85 public DeviceDataStatus getStatus() {
89 public UUID getUuid() {
93 public InstanceIdentifier getKey() {
97 public boolean isIntransitTimeExpired() {
98 return System.currentTimeMillis()
99 > intransitTimeStamp + HwvtepSouthboundConstants.IN_TRANSIT_STATE_EXPIRY_TIME_MILLIS;
102 public boolean isInTransitState() {
103 return status == DeviceDataStatus.IN_TRANSIT;
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;
115 private HwvtepConnectionInstance connectionInstance;
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;
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);
134 public LogicalSwitch getLogicalSwitch(UUID uuid) {
135 return (LogicalSwitch) getDeviceOperData(LogicalSwitches.class, uuid);
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());
149 public void putPhysicalSwitch(UUID uuid, PhysicalSwitch pSwitch) {
150 physicalSwitches.put(uuid, pSwitch);
153 public PhysicalSwitch getPhysicalSwitch(UUID uuid) {
154 return physicalSwitches.get(uuid);
157 public PhysicalSwitch removePhysicalSwitch(UUID uuid) {
158 return physicalSwitches.remove(uuid);
161 public Map<UUID, PhysicalSwitch> getPhysicalSwitches() {
162 return physicalSwitches;
165 public PhysicalLocator getPhysicalLocator(UUID uuid) {
166 return (PhysicalLocator) getDeviceOperData(TerminationPoint.class, uuid);
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());
180 public void putPhysicalSwitchForTunnel(UUID uuid, UUID psUUID) {
181 mapTunnelToPhysicalSwitch.put(uuid, psUUID);
184 public PhysicalSwitch getPhysicalSwitchForTunnel(UUID uuid) {
185 return physicalSwitches.get(mapTunnelToPhysicalSwitch.get(uuid));
188 public void removePhysicalSwitchForTunnel(UUID uuid) {
189 mapTunnelToPhysicalSwitch.remove(uuid);
192 public Map<UUID, UUID> getPhysicalSwitchesForTunnels() {
193 return mapTunnelToPhysicalSwitch;
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;
201 public boolean isConfigDataAvailable(Class<? extends Identifiable> cls, InstanceIdentifier key) {
202 return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key) != null;
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));
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();
218 public void clearConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
219 HwvtepSouthboundUtil.clearData(configKeyVsData, cls, key);
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);
227 if (deviceData != null) {
228 uuid = deviceData.getUuid();
229 data = deviceData.getData();
231 HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
232 new DeviceData(key, uuid, data, DeviceDataStatus.IN_TRANSIT));
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);
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);
247 HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
250 public Object getDeviceOperData(Class<? extends Identifiable> cls, UUID uuid) {
251 return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
254 public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
255 return HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
258 public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
259 DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
266 public <T extends Identifiable> void addJobToQueue(DependentJob<T> job) {
267 dependencyQueue.addToQueue(job);
270 public void onConfigDataAvailable() {
271 dependencyQueue.processReadyJobsFromConfigQueue(connectionInstance);
274 public synchronized void onOperDataAvailable() {
275 dependencyQueue.processReadyJobsFromOpQueue(connectionInstance);
278 public void scheduleTransaction(final TransactCommand transactCommand) {
279 dependencyQueue.submit(() -> connectionInstance.transact(transactCommand));
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) {
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));
303 clearDeviceOperData(cls, key);
308 public Map<InstanceIdentifier, DeviceData> getDeviceOperData(Class<? extends Identifiable> cls) {
309 return opKeyVsData.get(cls);
312 public void incRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
313 if (reference == null || tep == null) {
316 tepIdReferences.computeIfAbsent(tep, (tepId) -> Sets.newConcurrentHashSet());
317 tepIdReferences.get(tep).add(reference);
320 public int getRefCount(InstanceIdentifier tep) {
321 return tepIdReferences.containsKey(tep) ? tepIdReferences.get(tep).size() : 0;
324 public Set<InstanceIdentifier> getRefCounts(InstanceIdentifier tep) {
325 return tepIdReferences.get(tep);
328 public void decRefCount(InstanceIdentifier reference, InstanceIdentifier tep) {
329 if (reference == null || tep == null || !tepIdReferences.containsKey(tep)) {
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);
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()));
347 Map<InstanceIdentifier<RemoteUcastMacs>, RemoteUcastMacs> ucasts = logicalSwitchVsUcasts.get(logicalSwitchKey);
348 if (ucasts != null ) {
349 ucasts.entrySet().forEach( (entry) -> removeRemoteUcast(logicalSwitchKey, entry.getKey()));
351 markKeyAsInTransit(LogicalSwitches.class, logicalSwitchKey);
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()));
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());
372 public void removeRemoteMcast(InstanceIdentifier<LogicalSwitches> lsIid, InstanceIdentifier<RemoteMcastMacs> mcastIid) {
373 if (!logicalSwitchVsMcasts.containsKey(lsIid)) {
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()));
380 markKeyAsInTransit(RemoteMcastMacs.class, mcastIid);
383 public void removeRemoteUcast(InstanceIdentifier<LogicalSwitches> lsIid,
384 InstanceIdentifier<RemoteUcastMacs> ucastIid) {
385 if (!logicalSwitchVsUcasts.containsKey(lsIid)) {
388 RemoteUcastMacs mac = logicalSwitchVsUcasts.get(lsIid).remove(ucastIid);
390 decRefCount(ucastIid, mac.getLocatorRef().getValue());
392 markKeyAsInTransit(RemoteUcastMacs.class, ucastIid);
395 public HwvtepConnectionInstance getConnectionInstance() {
396 return connectionInstance;