BUG 5746 - Ovsdb QoS and Queue model enhancements
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbQosUpdateCommand.java
1 /*
2  * Copyright (c) 2016 Intel Communications Systems, Inc. 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.southbound.transactions.md;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.Map.Entry;
17
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.OvsdbConnectionInstance;
27 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
28 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
29 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQueueRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIdsKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 import com.google.common.base.Optional;
55
56 public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
57     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosUpdateCommand.class);
58
59     private Map<UUID, Qos> updatedQosRows;
60     private Map<UUID, Qos> oldQosRows;
61     private Map<UUID, Queue> updatedQueueRows;
62
63     public OvsdbQosUpdateCommand(OvsdbConnectionInstance key,
64             TableUpdates updates, DatabaseSchema dbSchema) {
65         super(key, updates, dbSchema);
66         updatedQosRows = TyperUtils.extractRowsUpdated(Qos.class,getUpdates(), getDbSchema());
67         oldQosRows = TyperUtils.extractRowsOld(Qos.class, getUpdates(), getDbSchema());
68         updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class, getUpdates(), getDbSchema());
69     }
70
71     @Override
72     public void execute(ReadWriteTransaction transaction) {
73         if (updatedQosRows != null && !updatedQosRows.isEmpty()) {
74             updateQos(transaction, updatedQosRows);
75         }
76     }
77
78     /**
79      * Update the QosEntries values after finding the related {@link OpenVSwitch} list.
80      * <p>
81      * Qos and OpenVSwitch are independent tables in the Open_vSwitch schema
82      * but the OVSDB yang model includes the Qos fields in the
83      * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Qos
84      * updates together and in other cases independently. This method here
85      * assumes the latter.
86      * </p>
87      *
88      * @param transaction the {@link ReadWriteTransaction}
89      * @param updatedQosRows updated {@link Qos} rows
90
91      */
92     private void updateQos(ReadWriteTransaction transaction,
93                                   Map<UUID, Qos> updatedQosRows) {
94
95         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
96         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
97         if (ovsdbNode.isPresent()) {
98             for (Entry<UUID, Qos> entry : updatedQosRows.entrySet()) {
99                 Qos qos = entry.getValue();
100                 Qos oldQos = oldQosRows.get(entry.getKey());
101                 QosEntriesBuilder qosEntryBuilder = new QosEntriesBuilder();
102                 qosEntryBuilder.setQosId(new Uri(getQosId(qos)));
103                 qosEntryBuilder.setQosUuid(new Uuid(entry.getKey().toString()));
104                 qosEntryBuilder.setQosType(
105                         SouthboundMapper.createQosType(qos.getTypeColumn().getData().toString()));
106                 setOtherConfig(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
107                 setExternalIds(transaction, qosEntryBuilder, oldQos, qos, nodeIId);
108                 setQueueList(transaction, qosEntryBuilder, oldQos, qos, nodeIId, ovsdbNode.get());
109
110                 QosEntries qosEntry = qosEntryBuilder.build();
111                 LOG.debug("Update Ovsdb Node {} with qos entries {}",ovsdbNode.get(), qosEntry);
112                 InstanceIdentifier<QosEntries> iid = nodeIId
113                         .augmentation(OvsdbNodeAugmentation.class)
114                         .child(QosEntries.class, qosEntry.getKey());
115                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
116                         iid, qosEntry);
117             }
118         }
119     }
120
121     private String getQosId(Qos qos) {
122         if (qos.getExternalIdsColumn() != null
123                 && qos.getExternalIdsColumn().getData() != null
124                 && qos.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
125             InstanceIdentifier<QosEntries> qosIid = (InstanceIdentifier<QosEntries>) SouthboundUtil.deserializeInstanceIdentifier(
126                     qos.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
127             if (qosIid != null) {
128                 QosEntriesKey qosEntriesKey = qosIid.firstKeyOf(QosEntries.class);
129                 if (qosEntriesKey != null) {
130                     return qosEntriesKey.getQosId().getValue();
131                 }
132             }
133         }
134         return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
135     }
136
137     private Queue getQueue(UUID queueUuid)
138     {
139         for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
140             if (entry.getKey().equals(queueUuid)) {
141                 return entry.getValue();
142             }
143         }
144         return null;
145     }
146
147     private InstanceIdentifier<Queues> getQueueIid(UUID queueUuid, Node ovsdbNode) {
148         Queue queue = getQueue(queueUuid);
149         if (queue != null && queue.getExternalIdsColumn() != null
150                 && queue.getExternalIdsColumn().getData() != null
151                 && queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
152             return (InstanceIdentifier<Queues>) SouthboundUtil.deserializeInstanceIdentifier(
153                     queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
154         } else {
155             OvsdbNodeAugmentation node = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
156             if (node.getQueues() != null && !node.getQueues().isEmpty()) {
157                 for (Queues q : node.getQueues()) {
158                     if (q.getQueueUuid().equals(new Uuid(queueUuid.toString()))) {
159                         return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
160                                 .augmentation(OvsdbNodeAugmentation.class)
161                                 .child(Queues.class, new QueuesKey(q.getQueueId()));
162                     }
163                 }
164             }
165             LOG.debug("A Queue with UUID {} was not found in Ovsdb Node {}", queueUuid, node);
166             return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
167                     .augmentation(OvsdbNodeAugmentation.class)
168                     .child(Queues.class, new QueuesKey(
169                             new Uri(SouthboundConstants.QUEUE_URI_PREFIX + "://" + queueUuid.toString())));
170         }
171     }
172
173     private void setOtherConfig(ReadWriteTransaction transaction,
174             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
175             InstanceIdentifier<Node> nodeIId) {
176         Map<String, String> oldOtherConfigs = null;
177         Map<String, String> otherConfigs = null;
178
179         if (qos.getOtherConfigColumn() != null) {
180             otherConfigs = qos.getOtherConfigColumn().getData();
181         }
182         if (oldQos != null && oldQos.getOtherConfigColumn() != null) {
183             oldOtherConfigs = oldQos.getOtherConfigColumn().getData();
184         }
185         if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
186             removeOldConfigs(transaction, qosEntryBuilder, oldOtherConfigs, qos, nodeIId);
187         }
188         if (otherConfigs != null && !otherConfigs.isEmpty()) {
189             setNewOtherConfigs(qosEntryBuilder, otherConfigs);
190         }
191     }
192
193     private void removeOldConfigs(ReadWriteTransaction transaction,
194             QosEntriesBuilder qosEntryBuilder, Map<String, String> oldOtherConfigs,
195             Qos qos, InstanceIdentifier<Node> nodeIId) {
196         InstanceIdentifier<QosEntries> qosIId = nodeIId
197                 .augmentation(OvsdbNodeAugmentation.class)
198                 .child(QosEntries.class, qosEntryBuilder.build().getKey());
199         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
200         for (String otherConfigKey : otherConfigKeys) {
201             KeyedInstanceIdentifier<QosOtherConfig, QosOtherConfigKey> otherIId =
202                     qosIId
203                     .child(QosOtherConfig.class, new QosOtherConfigKey(otherConfigKey));
204             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
205         }
206     }
207
208     private void setNewOtherConfigs(QosEntriesBuilder qosEntryBuilder,
209             Map<String, String> otherConfig) {
210         Set<String> otherConfigKeys = otherConfig.keySet();
211         List<QosOtherConfig> otherConfigList = new ArrayList<>();
212         String otherConfigValue;
213         for (String otherConfigKey : otherConfigKeys) {
214             otherConfigValue = otherConfig.get(otherConfigKey);
215             if (otherConfigKey != null && otherConfigValue != null) {
216                 otherConfigList.add(new QosOtherConfigBuilder().setOtherConfigKey(otherConfigKey)
217                         .setOtherConfigValue(otherConfigValue).build());
218             }
219         }
220         qosEntryBuilder.setQosOtherConfig(otherConfigList);
221     }
222
223     private void setExternalIds(ReadWriteTransaction transaction,
224             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
225             InstanceIdentifier<Node> nodeIId) {
226         Map<String, String> oldExternalIds = null;
227         Map<String, String> externalIds = null;
228
229         if (qos.getExternalIdsColumn() != null) {
230             externalIds = qos.getExternalIdsColumn().getData();
231         }
232         if (oldQos != null && oldQos.getExternalIdsColumn() != null) {
233             oldExternalIds = oldQos.getExternalIdsColumn().getData();
234         }
235         if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
236             removeOldExternalIds(transaction, qosEntryBuilder, oldExternalIds, qos, nodeIId);
237         }
238         if (externalIds != null && !externalIds.isEmpty()) {
239             setNewExternalIds(qosEntryBuilder, externalIds);
240         }
241     }
242
243     private void removeOldExternalIds(ReadWriteTransaction transaction,
244             QosEntriesBuilder qosEntryBuilder, Map<String, String> oldExternalIds,
245             Qos qos, InstanceIdentifier<Node> nodeIId) {
246         InstanceIdentifier<QosEntries> qosIId = nodeIId
247                 .augmentation(OvsdbNodeAugmentation.class)
248                 .child(QosEntries.class, qosEntryBuilder.build().getKey());
249         Set<String> externalIdsKeys = oldExternalIds.keySet();
250         for (String extIdKey : externalIdsKeys) {
251             KeyedInstanceIdentifier<QosExternalIds, QosExternalIdsKey> externalIId =
252                     qosIId
253                     .child(QosExternalIds.class, new QosExternalIdsKey(extIdKey));
254             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
255         }
256     }
257
258     private void setNewExternalIds(QosEntriesBuilder qosEntryBuilder,
259             Map<String, String> externalIds) {
260         Set<String> externalIdsKeys = externalIds.keySet();
261         List<QosExternalIds> externalIdsList = new ArrayList<>();
262         String extIdValue;
263         for (String extIdKey : externalIdsKeys) {
264             extIdValue = externalIds.get(extIdKey);
265             if (extIdKey != null && extIdValue != null) {
266                 externalIdsList.add(new QosExternalIdsBuilder().setQosExternalIdKey(extIdKey)
267                         .setQosExternalIdValue(extIdValue).build());
268             }
269         }
270         qosEntryBuilder.setQosExternalIds(externalIdsList);
271     }
272
273     private void setQueueList(ReadWriteTransaction transaction,
274             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
275             InstanceIdentifier<Node> nodeIId, Node ovsdbNode) {
276         Map<Long,UUID> oldQueueList = null;
277         Map<Long,UUID> queueList = null;
278
279         if (qos.getQueuesColumn() != null) {
280             queueList = qos.getQueuesColumn().getData();
281         }
282         if (oldQos != null && oldQos.getQueuesColumn() != null) {
283             oldQueueList = oldQos.getQueuesColumn().getData();
284         }
285         if ((oldQueueList != null) && !oldQueueList.isEmpty()) {
286             removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
287         }
288         if (queueList != null && !queueList.isEmpty()) {
289             setNewQueues(qosEntryBuilder, queueList, ovsdbNode);
290         }
291     }
292
293     private void removeOldQueues(ReadWriteTransaction transaction,
294             QosEntriesBuilder qosEntryBuilder, Map<Long, UUID> oldQueueList,
295             Qos qos, InstanceIdentifier<Node> nodeIId) {
296         InstanceIdentifier<QosEntries> qosIId = nodeIId
297                 .augmentation(OvsdbNodeAugmentation.class)
298                 .child(QosEntries.class, qosEntryBuilder.build().getKey());
299         Collection<Long> queueListKeys = oldQueueList.keySet();
300         for (Long queueListKey : queueListKeys) {
301             KeyedInstanceIdentifier<QueueList, QueueListKey> otherIId =
302                     qosIId
303                     .child(QueueList.class, new QueueListKey(new Long(queueListKey.toString())));
304             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
305         }
306     }
307
308     private void setNewQueues(QosEntriesBuilder qosEntryBuilder,
309             Map<Long, UUID> queueList, Node ovsdbNode) {
310         Set<Entry<Long, UUID>> queueEntries = queueList.entrySet();
311         List<QueueList> newQueueList = new ArrayList<>();
312         for (Entry<Long, UUID> queueEntry : queueEntries) {
313             InstanceIdentifier<Queues> queueIid = getQueueIid(queueEntry.getValue(), ovsdbNode);
314             if (queueIid != null) {
315             newQueueList.add(
316                     new QueueListBuilder()
317                     .setQueueNumber(queueEntry.getKey())
318                     .setQueueRef(new OvsdbQueueRef(queueIid))
319                     .setQueueUuid(new Uuid(queueEntry.getValue().toString())).build());
320             }
321         }
322         qosEntryBuilder.setQueueList(newQueueList);
323     }
324 }