2 * Copyright © 2016, 2017 Intel Communications Systems, Inc. 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.southbound.transactions.md;
11 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.lib.message.TableUpdates;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
25 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
26 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
27 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
28 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
29 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
30 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQueueRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
56 private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosUpdateCommand.class);
58 private final InstanceIdentifierCodec instanceIdentifierCodec;
60 private Map<UUID, Qos> updatedQosRows;
61 private Map<UUID, Qos> oldQosRows;
62 private Map<UUID, Queue> updatedQueueRows;
64 public OvsdbQosUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
65 TableUpdates updates, DatabaseSchema dbSchema) {
66 super(key, updates, dbSchema);
67 this.instanceIdentifierCodec = instanceIdentifierCodec;
68 updatedQosRows = TyperUtils.extractRowsUpdated(Qos.class,getUpdates(), getDbSchema());
69 oldQosRows = TyperUtils.extractRowsOld(Qos.class, getUpdates(), getDbSchema());
70 updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class, getUpdates(), getDbSchema());
74 public void execute(ReadWriteTransaction transaction) {
75 if (updatedQosRows != null && !updatedQosRows.isEmpty()) {
76 updateQos(transaction, updatedQosRows);
81 * Update the QosEntries values after finding the related
82 * {@link org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch} list.
84 * Qos and OpenVSwitch are independent tables in the Open_vSwitch schema
85 * but the OVSDB yang model includes the Qos fields in the
86 * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Qos
87 * updates together and in other cases independently. This method here
91 * @param transaction the {@link ReadWriteTransaction}
92 * @param newUpdatedQosRows updated {@link Qos} rows
95 private void updateQos(ReadWriteTransaction transaction,
96 Map<UUID, Qos> newUpdatedQosRows) {
98 final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
99 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
100 if (ovsdbNode.isPresent()) {
101 for (Entry<UUID, Qos> entry : newUpdatedQosRows.entrySet()) {
102 Qos qos = entry.getValue();
103 QosEntriesBuilder qosEntryBuilder = new QosEntriesBuilder();
104 qosEntryBuilder.setQosId(new Uri(getQosId(qos)));
105 qosEntryBuilder.setQosUuid(new Uuid(entry.getKey().toString()));
106 qosEntryBuilder.setQosType(
107 SouthboundMapper.createQosType(qos.getTypeColumn().getData()));
108 Qos oldQos = oldQosRows.get(entry.getKey());
109 setOtherConfig(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
110 setExternalIds(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
111 setQueueList(transaction, qosEntryBuilder, oldQos, qos, nodeIId, ovsdbNode.get());
113 QosEntries qosEntry = qosEntryBuilder.build();
114 LOG.debug("Update Ovsdb Node {} with qos entries {}",ovsdbNode.get(), qosEntry);
115 InstanceIdentifier<QosEntries> iid = nodeIId
116 .augmentation(OvsdbNodeAugmentation.class)
117 .child(QosEntries.class, qosEntry.getKey());
118 transaction.merge(LogicalDatastoreType.OPERATIONAL,
124 @SuppressWarnings("unchecked")
125 private String getQosId(Qos qos) {
126 if (qos.getExternalIdsColumn() != null
127 && qos.getExternalIdsColumn().getData() != null) {
128 if (qos.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
129 InstanceIdentifier<QosEntries> qosIid =
130 (InstanceIdentifier<QosEntries>) instanceIdentifierCodec.bindingDeserializerOrNull(
131 qos.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
132 if (qosIid != null) {
133 QosEntriesKey qosEntriesKey = qosIid.firstKeyOf(QosEntries.class);
134 if (qosEntriesKey != null) {
135 return qosEntriesKey.getQosId().getValue();
138 } else if (qos.getExternalIdsColumn().getData().containsKey(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY)) {
139 return qos.getExternalIdsColumn().getData().get(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY);
142 return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
145 private Queue getQueue(UUID queueUuid) {
146 for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
147 if (entry.getKey().equals(queueUuid)) {
148 return entry.getValue();
154 @SuppressWarnings("unchecked")
155 private InstanceIdentifier<Queues> getQueueIid(UUID queueUuid, Node ovsdbNode) {
156 Queue queue = getQueue(queueUuid);
157 if (queue != null && queue.getExternalIdsColumn() != null
158 && queue.getExternalIdsColumn().getData() != null
159 && queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
160 return (InstanceIdentifier<Queues>) instanceIdentifierCodec.bindingDeserializerOrNull(
161 queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
163 OvsdbNodeAugmentation node = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
164 if (node.getQueues() != null && !node.getQueues().isEmpty()) {
165 for (Queues q : node.getQueues()) {
166 if (q.getQueueUuid().equals(new Uuid(queueUuid.toString()))) {
167 return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
168 .augmentation(OvsdbNodeAugmentation.class)
169 .child(Queues.class, new QueuesKey(q.getQueueId()));
173 LOG.debug("A Queue with UUID {} was not found in Ovsdb Node {}", queueUuid, node);
174 return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
175 .augmentation(OvsdbNodeAugmentation.class)
176 .child(Queues.class, new QueuesKey(
177 new Uri(SouthboundConstants.QUEUE_URI_PREFIX + "://" + queueUuid.toString())));
181 private void setOtherConfig(ReadWriteTransaction transaction,
182 QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
183 InstanceIdentifier<Node> nodeIId) {
184 Map<String, String> oldOtherConfigs = null;
185 Map<String, String> otherConfigs = null;
187 if (qos.getOtherConfigColumn() != null) {
188 otherConfigs = qos.getOtherConfigColumn().getData();
190 if (oldQos != null && oldQos.getOtherConfigColumn() != null) {
191 oldOtherConfigs = oldQos.getOtherConfigColumn().getData();
193 if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
194 removeOldConfigs(transaction, qosEntryBuilder, oldOtherConfigs, qos, nodeIId);
196 if (otherConfigs != null && !otherConfigs.isEmpty()) {
197 setNewOtherConfigs(qosEntryBuilder, otherConfigs);
201 private void removeOldConfigs(ReadWriteTransaction transaction,
202 QosEntriesBuilder qosEntryBuilder, Map<String, String> oldOtherConfigs,
203 Qos qos, InstanceIdentifier<Node> nodeIId) {
204 InstanceIdentifier<QosEntries> qosIId = nodeIId
205 .augmentation(OvsdbNodeAugmentation.class)
206 .child(QosEntries.class, qosEntryBuilder.build().getKey());
207 Set<String> otherConfigKeys = oldOtherConfigs.keySet();
208 for (String otherConfigKey : otherConfigKeys) {
209 KeyedInstanceIdentifier<QosOtherConfig, QosOtherConfigKey> otherIId =
211 .child(QosOtherConfig.class, new QosOtherConfigKey(otherConfigKey));
212 transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
216 private void setNewOtherConfigs(QosEntriesBuilder qosEntryBuilder,
217 Map<String, String> otherConfig) {
218 Set<String> otherConfigKeys = otherConfig.keySet();
219 List<QosOtherConfig> otherConfigList = new ArrayList<>();
220 String otherConfigValue;
221 for (String otherConfigKey : otherConfigKeys) {
222 otherConfigValue = otherConfig.get(otherConfigKey);
223 if (otherConfigKey != null && otherConfigValue != null) {
224 otherConfigList.add(new QosOtherConfigBuilder().setOtherConfigKey(otherConfigKey)
225 .setOtherConfigValue(otherConfigValue).build());
228 qosEntryBuilder.setQosOtherConfig(otherConfigList);
231 private void setExternalIds(ReadWriteTransaction transaction,
232 QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
233 InstanceIdentifier<Node> nodeIId) {
234 Map<String, String> oldExternalIds = null;
235 Map<String, String> externalIds = null;
237 if (qos.getExternalIdsColumn() != null) {
238 externalIds = qos.getExternalIdsColumn().getData();
240 if (oldQos != null && oldQos.getExternalIdsColumn() != null) {
241 oldExternalIds = oldQos.getExternalIdsColumn().getData();
243 if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
244 removeOldExternalIds(transaction, qosEntryBuilder, oldExternalIds, qos, nodeIId);
246 if (externalIds != null && !externalIds.isEmpty()) {
247 setNewExternalIds(qosEntryBuilder, externalIds);
251 private void removeOldExternalIds(ReadWriteTransaction transaction,
252 QosEntriesBuilder qosEntryBuilder, Map<String, String> oldExternalIds,
253 Qos qos, InstanceIdentifier<Node> nodeIId) {
254 InstanceIdentifier<QosEntries> qosIId = nodeIId
255 .augmentation(OvsdbNodeAugmentation.class)
256 .child(QosEntries.class, qosEntryBuilder.build().getKey());
257 Set<String> externalIdsKeys = oldExternalIds.keySet();
258 for (String extIdKey : externalIdsKeys) {
259 KeyedInstanceIdentifier<QosExternalIds, QosExternalIdsKey> externalIId =
261 .child(QosExternalIds.class, new QosExternalIdsKey(extIdKey));
262 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
266 private void setNewExternalIds(QosEntriesBuilder qosEntryBuilder,
267 Map<String, String> externalIds) {
268 Set<String> externalIdsKeys = externalIds.keySet();
269 List<QosExternalIds> externalIdsList = new ArrayList<>();
271 for (String extIdKey : externalIdsKeys) {
272 extIdValue = externalIds.get(extIdKey);
273 if (extIdKey != null && extIdValue != null) {
274 externalIdsList.add(new QosExternalIdsBuilder().setQosExternalIdKey(extIdKey)
275 .setQosExternalIdValue(extIdValue).build());
278 qosEntryBuilder.setQosExternalIds(externalIdsList);
281 private void setQueueList(ReadWriteTransaction transaction,
282 QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
283 InstanceIdentifier<Node> nodeIId, Node ovsdbNode) {
284 Map<Long,UUID> oldQueueList = null;
285 Map<Long,UUID> queueList = null;
287 if (qos.getQueuesColumn() != null) {
288 queueList = qos.getQueuesColumn().getData();
290 if (oldQos != null && oldQos.getQueuesColumn() != null) {
291 oldQueueList = oldQos.getQueuesColumn().getData();
293 if ((oldQueueList != null) && !oldQueueList.isEmpty()) {
294 removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
296 if (queueList != null && !queueList.isEmpty()) {
297 setNewQueues(qosEntryBuilder, queueList, ovsdbNode);
301 private void removeOldQueues(ReadWriteTransaction transaction,
302 QosEntriesBuilder qosEntryBuilder, Map<Long, UUID> oldQueueList,
303 Qos qos, InstanceIdentifier<Node> nodeIId) {
304 InstanceIdentifier<QosEntries> qosIId = nodeIId
305 .augmentation(OvsdbNodeAugmentation.class)
306 .child(QosEntries.class, qosEntryBuilder.build().getKey());
307 Collection<Long> queueListKeys = oldQueueList.keySet();
308 for (Long queueListKey : queueListKeys) {
309 KeyedInstanceIdentifier<QueueList, QueueListKey> otherIId =
311 .child(QueueList.class, new QueueListKey(new Long(queueListKey.toString())));
312 transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
316 private void setNewQueues(QosEntriesBuilder qosEntryBuilder,
317 Map<Long, UUID> queueList, Node ovsdbNode) {
318 Set<Entry<Long, UUID>> queueEntries = queueList.entrySet();
319 List<QueueList> newQueueList = new ArrayList<>();
320 for (Entry<Long, UUID> queueEntry : queueEntries) {
321 InstanceIdentifier<Queues> queueIid = getQueueIid(queueEntry.getValue(), ovsdbNode);
322 if (queueIid != null) {
324 new QueueListBuilder()
325 .setQueueNumber(queueEntry.getKey())
326 .setQueueRef(new OvsdbQueueRef(queueIid))
327 .setQueueUuid(new Uuid(queueEntry.getValue().toString())).build());
330 qosEntryBuilder.setQueueList(newQueueList);