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