190153c580fc30552f15682063177b62abe20873
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbQosUpdateCommand.java
1 /*
2  * Copyright © 2016, 2017 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.Map.Entry;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.mdsal.common.api.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;
54
55 public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
56     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosUpdateCommand.class);
57
58     private final InstanceIdentifierCodec instanceIdentifierCodec;
59
60     private final Map<UUID, Qos> updatedQosRows;
61     private final Map<UUID, Qos> oldQosRows;
62     private final Map<UUID, Queue> updatedQueueRows;
63
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());
71     }
72
73     @Override
74     public void execute(ReadWriteTransaction transaction) {
75         if (updatedQosRows != null && !updatedQosRows.isEmpty()) {
76             updateQos(transaction, updatedQosRows);
77         }
78     }
79
80     /**
81      * Update the QosEntries values after finding the related
82      * {@link org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch} list.
83      * <p>
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
88      * assumes the latter.
89      * </p>
90      *
91      * @param transaction the {@link ReadWriteTransaction}
92      * @param newUpdatedQosRows updated {@link Qos} rows
93
94      */
95     private void updateQos(ReadWriteTransaction transaction,
96                                   Map<UUID, Qos> newUpdatedQosRows) {
97
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());
112
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.key());
118                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
119                         iid, qosEntry);
120             }
121         }
122     }
123
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();
136                     }
137                 }
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);
140             }
141         }
142         return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
143     }
144
145     private Queue getQueue(UUID queueUuid) {
146         for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
147             if (entry.getKey().equals(queueUuid)) {
148                 return entry.getValue();
149             }
150         }
151         return null;
152     }
153
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));
162         } else {
163             OvsdbNodeAugmentation node = ovsdbNode.augmentation(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()));
170                     }
171                 }
172             }
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())));
178         }
179     }
180
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;
186
187         if (qos.getOtherConfigColumn() != null) {
188             otherConfigs = qos.getOtherConfigColumn().getData();
189         }
190         if (oldQos != null && oldQos.getOtherConfigColumn() != null) {
191             oldOtherConfigs = oldQos.getOtherConfigColumn().getData();
192         }
193         if (oldOtherConfigs != null && !oldOtherConfigs.isEmpty()) {
194             removeOldConfigs(transaction, qosEntryBuilder, oldOtherConfigs, qos, nodeIId);
195         }
196         if (otherConfigs != null && !otherConfigs.isEmpty()) {
197             setNewOtherConfigs(qosEntryBuilder, otherConfigs);
198         }
199     }
200
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().key());
207         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
208         for (String otherConfigKey : otherConfigKeys) {
209             KeyedInstanceIdentifier<QosOtherConfig, QosOtherConfigKey> otherIId =
210                     qosIId
211                     .child(QosOtherConfig.class, new QosOtherConfigKey(otherConfigKey));
212             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
213         }
214     }
215
216     private void setNewOtherConfigs(QosEntriesBuilder qosEntryBuilder,
217             Map<String, String> otherConfig) {
218         List<QosOtherConfig> otherConfigList = new ArrayList<>();
219         for (Entry<String, String> entry : otherConfig.entrySet()) {
220             String otherConfigKey = entry.getKey();
221             String otherConfigValue = entry.getValue();
222             if (otherConfigKey != null && otherConfigValue != null) {
223                 otherConfigList.add(new QosOtherConfigBuilder().setOtherConfigKey(otherConfigKey)
224                         .setOtherConfigValue(otherConfigValue).build());
225             }
226         }
227         qosEntryBuilder.setQosOtherConfig(otherConfigList);
228     }
229
230     private void setExternalIds(ReadWriteTransaction transaction,
231             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
232             InstanceIdentifier<Node> nodeIId) {
233         Map<String, String> oldExternalIds = null;
234         Map<String, String> externalIds = null;
235
236         if (qos.getExternalIdsColumn() != null) {
237             externalIds = qos.getExternalIdsColumn().getData();
238         }
239         if (oldQos != null && oldQos.getExternalIdsColumn() != null) {
240             oldExternalIds = oldQos.getExternalIdsColumn().getData();
241         }
242         if (oldExternalIds != null && !oldExternalIds.isEmpty()) {
243             removeOldExternalIds(transaction, qosEntryBuilder, oldExternalIds, qos, nodeIId);
244         }
245         if (externalIds != null && !externalIds.isEmpty()) {
246             setNewExternalIds(qosEntryBuilder, externalIds);
247         }
248     }
249
250     private void removeOldExternalIds(ReadWriteTransaction transaction,
251             QosEntriesBuilder qosEntryBuilder, Map<String, String> oldExternalIds,
252             Qos qos, InstanceIdentifier<Node> nodeIId) {
253         InstanceIdentifier<QosEntries> qosIId = nodeIId
254                 .augmentation(OvsdbNodeAugmentation.class)
255                 .child(QosEntries.class, qosEntryBuilder.build().key());
256         Set<String> externalIdsKeys = oldExternalIds.keySet();
257         for (String extIdKey : externalIdsKeys) {
258             KeyedInstanceIdentifier<QosExternalIds, QosExternalIdsKey> externalIId =
259                     qosIId
260                     .child(QosExternalIds.class, new QosExternalIdsKey(extIdKey));
261             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
262         }
263     }
264
265     private void setNewExternalIds(QosEntriesBuilder qosEntryBuilder,
266             Map<String, String> externalIds) {
267         List<QosExternalIds> externalIdsList = new ArrayList<>();
268         for (Entry<String, String> entry : externalIds.entrySet()) {
269             String extIdKey = entry.getKey();
270             String extIdValue = entry.getValue();
271             if (extIdKey != null && extIdValue != null) {
272                 externalIdsList.add(new QosExternalIdsBuilder().setQosExternalIdKey(extIdKey)
273                         .setQosExternalIdValue(extIdValue).build());
274             }
275         }
276         qosEntryBuilder.setQosExternalIds(externalIdsList);
277     }
278
279     private void setQueueList(ReadWriteTransaction transaction,
280             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
281             InstanceIdentifier<Node> nodeIId, Node ovsdbNode) {
282         Map<Long,UUID> oldQueueList = null;
283         Map<Long,UUID> queueList = null;
284
285         if (qos.getQueuesColumn() != null) {
286             queueList = qos.getQueuesColumn().getData();
287         }
288         if (oldQos != null && oldQos.getQueuesColumn() != null) {
289             oldQueueList = oldQos.getQueuesColumn().getData();
290         }
291         if (oldQueueList != null && !oldQueueList.isEmpty()) {
292             removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
293         }
294         if (queueList != null && !queueList.isEmpty()) {
295             setNewQueues(qosEntryBuilder, queueList, ovsdbNode);
296         }
297     }
298
299     private void removeOldQueues(ReadWriteTransaction transaction,
300             QosEntriesBuilder qosEntryBuilder, Map<Long, UUID> oldQueueList,
301             Qos qos, InstanceIdentifier<Node> nodeIId) {
302         InstanceIdentifier<QosEntries> qosIId = nodeIId
303                 .augmentation(OvsdbNodeAugmentation.class)
304                 .child(QosEntries.class, qosEntryBuilder.build().key());
305         Collection<Long> queueListKeys = oldQueueList.keySet();
306         for (Long queueListKey : queueListKeys) {
307             KeyedInstanceIdentifier<QueueList, QueueListKey> otherIId =
308                     qosIId.child(QueueList.class, new QueueListKey(Long.valueOf(queueListKey.toString())));
309             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
310         }
311     }
312
313     private void setNewQueues(QosEntriesBuilder qosEntryBuilder,
314             Map<Long, UUID> queueList, Node ovsdbNode) {
315         Set<Entry<Long, UUID>> queueEntries = queueList.entrySet();
316         List<QueueList> newQueueList = new ArrayList<>();
317         for (Entry<Long, UUID> queueEntry : queueEntries) {
318             InstanceIdentifier<Queues> queueIid = getQueueIid(queueEntry.getValue(), ovsdbNode);
319             if (queueIid != null) {
320                 newQueueList.add(
321                     new QueueListBuilder()
322                     .setQueueNumber(queueEntry.getKey())
323                     .setQueueRef(new OvsdbQueueRef(queueIid)).build());
324             }
325
326         }
327         qosEntryBuilder.setQueueList(newQueueList);
328     }
329 }