Update MRI projects for Aluminium
[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             Map<QueuesKey, Queues> queues = node.getQueues();
165             if (queues != null) {
166                 final Uuid uuid = new Uuid(queueUuid.toString());
167                 for (Queues q : queues.values()) {
168                     if (uuid.equals(q.getQueueUuid())) {
169                         return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
170                                 .augmentation(OvsdbNodeAugmentation.class)
171                                 .child(Queues.class, q.key());
172                     }
173                 }
174             }
175             LOG.debug("A Queue with UUID {} was not found in Ovsdb Node {}", queueUuid, node);
176             return SouthboundMapper.createInstanceIdentifier(ovsdbNode.getNodeId())
177                     .augmentation(OvsdbNodeAugmentation.class)
178                     .child(Queues.class, new QueuesKey(
179                             new Uri(SouthboundConstants.QUEUE_URI_PREFIX + "://" + queueUuid.toString())));
180         }
181     }
182
183     private void setOtherConfig(ReadWriteTransaction transaction,
184             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
185             InstanceIdentifier<Node> nodeIId) {
186         Map<String, String> oldOtherConfigs = null;
187         Map<String, String> otherConfigs = null;
188
189         if (qos.getOtherConfigColumn() != null) {
190             otherConfigs = qos.getOtherConfigColumn().getData();
191         }
192         if (oldQos != null && oldQos.getOtherConfigColumn() != null) {
193             oldOtherConfigs = oldQos.getOtherConfigColumn().getData();
194         }
195         if (oldOtherConfigs != null && !oldOtherConfigs.isEmpty()) {
196             removeOldConfigs(transaction, qosEntryBuilder, oldOtherConfigs, qos, nodeIId);
197         }
198         if (otherConfigs != null && !otherConfigs.isEmpty()) {
199             setNewOtherConfigs(qosEntryBuilder, otherConfigs);
200         }
201     }
202
203     private void removeOldConfigs(ReadWriteTransaction transaction,
204             QosEntriesBuilder qosEntryBuilder, Map<String, String> oldOtherConfigs,
205             Qos qos, InstanceIdentifier<Node> nodeIId) {
206         InstanceIdentifier<QosEntries> qosIId = nodeIId
207                 .augmentation(OvsdbNodeAugmentation.class)
208                 .child(QosEntries.class, qosEntryBuilder.build().key());
209         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
210         for (String otherConfigKey : otherConfigKeys) {
211             KeyedInstanceIdentifier<QosOtherConfig, QosOtherConfigKey> otherIId =
212                     qosIId
213                     .child(QosOtherConfig.class, new QosOtherConfigKey(otherConfigKey));
214             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
215         }
216     }
217
218     private void setNewOtherConfigs(QosEntriesBuilder qosEntryBuilder,
219             Map<String, String> otherConfig) {
220         List<QosOtherConfig> otherConfigList = new ArrayList<>();
221         for (Entry<String, String> entry : otherConfig.entrySet()) {
222             String otherConfigKey = entry.getKey();
223             String otherConfigValue = entry.getValue();
224             if (otherConfigKey != null && otherConfigValue != null) {
225                 otherConfigList.add(new QosOtherConfigBuilder().setOtherConfigKey(otherConfigKey)
226                         .setOtherConfigValue(otherConfigValue).build());
227             }
228         }
229         qosEntryBuilder.setQosOtherConfig(otherConfigList);
230     }
231
232     private void setExternalIds(ReadWriteTransaction transaction,
233             QosEntriesBuilder qosEntryBuilder, Qos oldQos, Qos qos,
234             InstanceIdentifier<Node> nodeIId) {
235         Map<String, String> oldExternalIds = null;
236         Map<String, String> externalIds = null;
237
238         if (qos.getExternalIdsColumn() != null) {
239             externalIds = qos.getExternalIdsColumn().getData();
240         }
241         if (oldQos != null && oldQos.getExternalIdsColumn() != null) {
242             oldExternalIds = oldQos.getExternalIdsColumn().getData();
243         }
244         if (oldExternalIds != null && !oldExternalIds.isEmpty()) {
245             removeOldExternalIds(transaction, qosEntryBuilder, oldExternalIds, qos, nodeIId);
246         }
247         if (externalIds != null && !externalIds.isEmpty()) {
248             setNewExternalIds(qosEntryBuilder, externalIds);
249         }
250     }
251
252     private void removeOldExternalIds(ReadWriteTransaction transaction,
253             QosEntriesBuilder qosEntryBuilder, Map<String, String> oldExternalIds,
254             Qos qos, InstanceIdentifier<Node> nodeIId) {
255         InstanceIdentifier<QosEntries> qosIId = nodeIId
256                 .augmentation(OvsdbNodeAugmentation.class)
257                 .child(QosEntries.class, qosEntryBuilder.build().key());
258         Set<String> externalIdsKeys = oldExternalIds.keySet();
259         for (String extIdKey : externalIdsKeys) {
260             KeyedInstanceIdentifier<QosExternalIds, QosExternalIdsKey> externalIId =
261                     qosIId
262                     .child(QosExternalIds.class, new QosExternalIdsKey(extIdKey));
263             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
264         }
265     }
266
267     private void setNewExternalIds(QosEntriesBuilder qosEntryBuilder,
268             Map<String, String> externalIds) {
269         List<QosExternalIds> externalIdsList = new ArrayList<>();
270         for (Entry<String, String> entry : externalIds.entrySet()) {
271             String extIdKey = entry.getKey();
272             String extIdValue = entry.getValue();
273             if (extIdKey != null && extIdValue != null) {
274                 externalIdsList.add(new QosExternalIdsBuilder().setQosExternalIdKey(extIdKey)
275                         .setQosExternalIdValue(extIdValue).build());
276             }
277         }
278         qosEntryBuilder.setQosExternalIds(externalIdsList);
279     }
280
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;
286
287         if (qos.getQueuesColumn() != null) {
288             queueList = qos.getQueuesColumn().getData();
289         }
290         if (oldQos != null && oldQos.getQueuesColumn() != null) {
291             oldQueueList = oldQos.getQueuesColumn().getData();
292         }
293         if (oldQueueList != null && !oldQueueList.isEmpty()) {
294             removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
295         }
296         if (queueList != null && !queueList.isEmpty()) {
297             setNewQueues(qosEntryBuilder, queueList, ovsdbNode);
298         }
299     }
300
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().key());
307         Collection<Long> queueListKeys = oldQueueList.keySet();
308         for (Long queueListKey : queueListKeys) {
309             KeyedInstanceIdentifier<QueueList, QueueListKey> otherIId =
310                     qosIId.child(QueueList.class, new QueueListKey(Long.valueOf(queueListKey.toString())));
311             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
312         }
313     }
314
315     private void setNewQueues(QosEntriesBuilder qosEntryBuilder,
316             Map<Long, UUID> queueList, Node ovsdbNode) {
317         Set<Entry<Long, UUID>> queueEntries = queueList.entrySet();
318         List<QueueList> newQueueList = new ArrayList<>();
319         for (Entry<Long, UUID> queueEntry : queueEntries) {
320             InstanceIdentifier<Queues> queueIid = getQueueIid(queueEntry.getValue(), ovsdbNode);
321             if (queueIid != null) {
322                 newQueueList.add(
323                     new QueueListBuilder()
324                     .setQueueNumber(queueEntry.getKey())
325                     .setQueueRef(new OvsdbQueueRef(queueIid)).build());
326             }
327
328         }
329         qosEntryBuilder.setQueueList(newQueueList);
330     }
331 }