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