2 * Copyright (c) 2016 Intel Communications Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.southbound.transactions.md;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
16 import java.util.Map.Entry;
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;
49 import com.google.common.base.Optional;
51 public class OvsdbQosUpdateCommand extends AbstractTransactionCommand {
52 private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosUpdateCommand.class);
54 private Map<UUID, Qos> updatedQosRows;
55 private Map<UUID, Qos> oldQosRows;
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());
65 public void execute(ReadWriteTransaction transaction) {
66 if (updatedQosRows != null && !updatedQosRows.isEmpty()) {
67 updateQos(transaction, updatedQosRows);
72 * Update the QosEntries values after finding the related {@link OpenVSwitch} list.
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
81 * @param transaction the {@link ReadWriteTransaction}
82 * @param updatedQosRows updated {@link Qos} rows
85 private void updateQos(ReadWriteTransaction transaction,
86 Map<UUID, Qos> updatedQosRows) {
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);
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,
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);
120 return SouthboundConstants.QOS_URI_PREFIX + "://" + qos.getUuid().toString();
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;
130 if (qos.getOtherConfigColumn() != null) {
131 otherConfigs = qos.getOtherConfigColumn().getData();
133 if (oldQos != null && oldQos.getOtherConfigColumn() != null) {
134 oldOtherConfigs = oldQos.getOtherConfigColumn().getData();
136 if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
137 removeOldConfigs(transaction, qosEntryBuilder, oldOtherConfigs, qos, nodeIId);
139 if (otherConfigs != null && !otherConfigs.isEmpty()) {
140 setNewOtherConfigs(qosEntryBuilder, otherConfigs);
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 =
154 .child(QosOtherConfig.class, new QosOtherConfigKey(otherConfigKey));
155 transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
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());
171 qosEntryBuilder.setQosOtherConfig(otherConfigList);
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;
180 if (qos.getExternalIdsColumn() != null) {
181 externalIds = qos.getExternalIdsColumn().getData();
183 if (oldQos != null && oldQos.getExternalIdsColumn() != null) {
184 oldExternalIds = oldQos.getExternalIdsColumn().getData();
186 if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
187 removeOldExternalIds(transaction, qosEntryBuilder, oldExternalIds, qos, nodeIId);
189 if (externalIds != null && !externalIds.isEmpty()) {
190 setNewExternalIds(qosEntryBuilder, externalIds);
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 =
204 .child(QosExternalIds.class, new QosExternalIdsKey(extIdKey));
205 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
209 private void setNewExternalIds(QosEntriesBuilder qosEntryBuilder,
210 Map<String, String> externalIds) {
211 Set<String> externalIdsKeys = externalIds.keySet();
212 List<QosExternalIds> externalIdsList = new ArrayList<>();
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());
221 qosEntryBuilder.setQosExternalIds(externalIdsList);
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;
230 if (qos.getQueuesColumn() != null) {
231 queueList = qos.getQueuesColumn().getData();
233 if (oldQos != null && oldQos.getQueuesColumn() != null) {
234 oldQueueList = oldQos.getQueuesColumn().getData();
236 if ((oldQueueList != null) && !oldQueueList.isEmpty()) {
237 removeOldQueues(transaction, qosEntryBuilder, oldQueueList, qos, nodeIId);
239 if (queueList != null && !queueList.isEmpty()) {
240 setNewQueues(qosEntryBuilder, queueList);
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 =
254 .child(QueueList.class, new QueueListKey(new Long(queueListKey.toString())));
255 transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
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) {
265 new QueueListBuilder().setQueueNumber(queueEntry.getKey())
266 .setQueueUuid(new Uuid(queueEntry.getValue().toString())).build());
268 qosEntryBuilder.setQueueList(newQueueList);