ovsdb enable checkstyle on error
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbQosUpdateCommand.java
index 231c722278559dc3db2d85e2c7f6b287d17b4df2..5340cc5649fa78cf9bd48b82b9fffb45d8ac8d20 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.ovsdb.southbound.transactions.md;
 
+import com.google.common.base.Optional;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
-
+import java.util.Set;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
@@ -22,15 +22,20 @@ import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
+import org.opendaylight.ovsdb.schema.openvswitch.Queue;
 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQueueRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsKey;
@@ -46,19 +51,19 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
 public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosUpdateCommand.class);
 
     private Map<UUID, Qos> updatedQosRows;
     private Map<UUID, Qos> oldQosRows;
+    private Map<UUID, Queue> updatedQueueRows;
 
     public OvsdbQosUpdateCommand(OvsdbConnectionInstance key,
             TableUpdates updates, DatabaseSchema dbSchema) {
         super(key, updates, dbSchema);
         updatedQosRows = TyperUtils.extractRowsUpdated(Qos.class,getUpdates(), getDbSchema());
         oldQosRows = TyperUtils.extractRowsOld(Qos.class, getUpdates(), getDbSchema());
+        updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class, getUpdates(), getDbSchema());
     }
 
     @Override
@@ -90,15 +95,15 @@ public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
         if (ovsdbNode.isPresent()) {
             for (Entry<UUID, Qos> entry : updatedQosRows.entrySet()) {
                 Qos qos = entry.getValue();
-                Qos oldQos = oldQosRows.get(entry.getKey());
                 QosEntriesBuilder qosEntryBuilder = new QosEntriesBuilder();
                 qosEntryBuilder.setQosId(new Uri(getQosId(qos)));
                 qosEntryBuilder.setQosUuid(new Uuid(entry.getKey().toString()));
                 qosEntryBuilder.setQosType(
                         SouthboundMapper.createQosType(qos.getTypeColumn().getData().toString()));
+                Qos oldQos = oldQosRows.get(entry.getKey());
                 setOtherConfig(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
                 setExternalIds(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
-                setQueueList(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
+                setQueueList(transaction, qosEntryBuilder, oldQos, qos, nodeIId, ovsdbNode.get());
 
                 QosEntries qosEntry = qosEntryBuilder.build();
                 LOG.debug("Update Ovsdb Node {} with qos entries {}",ovsdbNode.get(), qosEntry);
@@ -111,13 +116,57 @@ public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
         }
     }
 
+    @SuppressWarnings("unchecked")
     private String getQosId(Qos qos) {
         if (qos.getExternalIdsColumn() != null
                 && qos.getExternalIdsColumn().getData() != null
-                && qos.getExternalIdsColumn().getData().containsKey(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY)) {
-            return qos.getExternalIdsColumn().getData().get(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY);
+                && qos.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
+            InstanceIdentifier<QosEntries> qosIid =
+                    (InstanceIdentifier<QosEntries>) SouthboundUtil.deserializeInstanceIdentifier(
+                            qos.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
+            if (qosIid != null) {
+                QosEntriesKey qosEntriesKey = qosIid.firstKeyOf(QosEntries.class);
+                if (qosEntriesKey != null) {
+                    return qosEntriesKey.getQosId().getValue();
+                }
+            }
+        }
+        return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
+    }
+
+    private Queue getQueue(UUID queueUuid) {
+        for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
+            if (entry.getKey().equals(queueUuid)) {
+                return entry.getValue();
+            }
+        }
+        return null;
+    }
+
+    @SuppressWarnings("unchecked")
+    private InstanceIdentifier<Queues> getQueueIid(UUID queueUuid, Node ovsdbNode) {
+        Queue queue = getQueue(queueUuid);
+        if (queue != null && queue.getExternalIdsColumn() != null
+                && queue.getExternalIdsColumn().getData() != null
+                && queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
+            return (InstanceIdentifier<Queues>) SouthboundUtil.deserializeInstanceIdentifier(
+                    queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
         } else {
-            return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
+            OvsdbNodeAugmentation node = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
+            if (node.getQueues() != null && !node.getQueues().isEmpty()) {
+                for (Queues q : node.getQueues()) {
+                    if (q.getQueueUuid().equals(new Uuid(queueUuid.toString()))) {
+                        return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
+                                .augmentation(OvsdbNodeAugmentation.class)
+                                .child(Queues.class, new QueuesKey(q.getQueueId()));
+                    }
+                }
+            }
+            LOG.debug("A Queue with UUID {} was not found in Ovsdb Node {}", queueUuid, node);
+            return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
+                    .augmentation(OvsdbNodeAugmentation.class)
+                    .child(Queues.class, new QueuesKey(
+                            new Uri(SouthboundConstants.QUEUE_URI_PREFIX + "://" + queueUuid.toString())));
         }
     }
 
@@ -223,7 +272,7 @@ public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
 
     private void setQueueList(ReadWriteTransaction transaction,
             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
-            InstanceIdentifier<Node> nodeIId) {
+            InstanceIdentifier<Node> nodeIId, Node ovsdbNode) {
         Map<Long,UUID> oldQueueList = null;
         Map<Long,UUID> queueList = null;
 
@@ -237,7 +286,7 @@ public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
             removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
         }
         if (queueList != null && !queueList.isEmpty()) {
-            setNewQueues(qosEntryBuilder, queueList);
+            setNewQueues(qosEntryBuilder, queueList, ovsdbNode);
         }
     }
 
@@ -257,13 +306,18 @@ public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
     }
 
     private void setNewQueues(QosEntriesBuilder qosEntryBuilder,
-            Map<Long, UUID> queueList) {
+            Map<Long, UUID> queueList, Node ovsdbNode) {
         Set<Entry<Long, UUID>> queueEntries = queueList.entrySet();
         List<QueueList> newQueueList = new ArrayList<>();
         for (Entry<Long, UUID> queueEntry : queueEntries) {
-            newQueueList.add(
-                    new QueueListBuilder().setQueueNumber(queueEntry.getKey())
+            InstanceIdentifier<Queues> queueIid = getQueueIid(queueEntry.getValue(), ovsdbNode);
+            if (queueIid != null) {
+                newQueueList.add(
+                    new QueueListBuilder()
+                    .setQueueNumber(queueEntry.getKey())
+                    .setQueueRef(new OvsdbQueueRef(queueIid))
                     .setQueueUuid(new Uuid(queueEntry.getValue().toString())).build());
+            }
         }
         qosEntryBuilder.setQueueList(newQueueList);
     }