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.Queue;
25 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
26 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
27 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
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.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigKey;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Optional;
47 public class OvsdbQueueUpdateCommand extends AbstractTransactionCommand {
48 private static final Logger LOG = LoggerFactory.getLogger(OvsdbQueueUpdateCommand.class);
50 private Map<UUID, Queue> updatedQueueRows;
51 private Map<UUID, Queue> oldQueueRows;
53 public OvsdbQueueUpdateCommand(OvsdbConnectionInstance key,
54 TableUpdates updates, DatabaseSchema dbSchema) {
55 super(key, updates, dbSchema);
56 updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class,getUpdates(), getDbSchema());
57 oldQueueRows = TyperUtils.extractRowsOld(Queue.class, getUpdates(), getDbSchema());
61 public void execute(ReadWriteTransaction transaction) {
62 if (updatedQueueRows != null && !updatedQueueRows.isEmpty()) {
63 updateQueue(transaction);
68 * Update the Queues values after finding the related {@OpenVSwitch} list.
70 * Queue and OpenVSwitch are independent tables in the Open_vSwitch schema
71 * but the OVSDB yang model includes the Queue fields in the
72 * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Queue
73 * updates together and in other cases independently. This method here
77 * @param transaction the {@link ReadWriteTransaction}
78 * @param updatedQueueRows updated {@link Queue} rows
81 private void updateQueue(ReadWriteTransaction transaction) {
83 final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
84 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
85 if (ovsdbNode.isPresent()) {
86 for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
87 Queue queue = entry.getValue();
88 Queue oldQueue = oldQueueRows.get(entry.getKey());
89 QueuesBuilder queuesBuilder = new QueuesBuilder();
90 queuesBuilder.setQueueId(new Uri(getQueueId(queue)));
91 queuesBuilder.setQueueUuid(new Uuid(entry.getKey().toString()));
92 Collection<Long> dscp = queue.getDscpColumn().getData();
93 if (!dscp.isEmpty()) {
94 queuesBuilder.setDscp(dscp.iterator().next().shortValue());
96 setOtherConfig(transaction, queuesBuilder, oldQueue, queue, nodeIId);
97 setExternalIds(transaction, queuesBuilder, oldQueue, queue, nodeIId);
99 Queues queues = queuesBuilder.build();
100 LOG.debug("Update Ovsdb Node {} with queue entries {}",ovsdbNode.get(), queues);
101 InstanceIdentifier<Queues> iid = nodeIId
102 .augmentation(OvsdbNodeAugmentation.class)
103 .child(Queues.class, queues.getKey());
104 transaction.merge(LogicalDatastoreType.OPERATIONAL,
110 private String getQueueId(Queue queue) {
111 if (queue.getExternalIdsColumn() != null
112 && queue.getExternalIdsColumn().getData() != null
113 && queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY)) {
114 return queue.getExternalIdsColumn().getData().get(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY);
116 return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
120 private void setOtherConfig(ReadWriteTransaction transaction,
121 QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
122 InstanceIdentifier<Node> nodeIId) {
123 Map<String, String> oldOtherConfigs = null;
124 Map<String, String> otherConfigs = null;
126 if (queue.getOtherConfigColumn() != null) {
127 otherConfigs = queue.getOtherConfigColumn().getData();
129 if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
130 oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
132 if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
133 removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
135 if (otherConfigs != null && !otherConfigs.isEmpty()) {
136 setNewOtherConfigs(queuesBuilder, otherConfigs);
140 private void removeOldConfigs(ReadWriteTransaction transaction,
141 QueuesBuilder queuesBuilder, Map<String, String> oldOtherConfigs,
142 Queue queue, InstanceIdentifier<Node> nodeIId) {
143 InstanceIdentifier<Queues> queueIId = nodeIId
144 .augmentation(OvsdbNodeAugmentation.class)
145 .child(Queues.class, queuesBuilder.build().getKey());
146 Set<String> otherConfigKeys = oldOtherConfigs.keySet();
147 for (String otherConfigKey : otherConfigKeys) {
148 KeyedInstanceIdentifier<QueuesOtherConfig, QueuesOtherConfigKey> otherIId =
150 .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
151 transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
155 private void setNewOtherConfigs(QueuesBuilder queuesBuilder,
156 Map<String, String> otherConfig) {
157 Set<String> otherConfigKeys = otherConfig.keySet();
158 List<QueuesOtherConfig> otherConfigList = new ArrayList<>();
159 String otherConfigValue;
160 for (String otherConfigKey : otherConfigKeys) {
161 otherConfigValue = otherConfig.get(otherConfigKey);
162 if (otherConfigKey != null && otherConfigValue != null) {
163 otherConfigList.add(new QueuesOtherConfigBuilder().setQueueOtherConfigKey(otherConfigKey)
164 .setQueueOtherConfigValue(otherConfigValue).build());
167 queuesBuilder.setQueuesOtherConfig(otherConfigList);
170 private void setExternalIds(ReadWriteTransaction transaction,
171 QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
172 InstanceIdentifier<Node> nodeIId) {
173 Map<String, String> oldExternalIds = null;
174 Map<String, String> externalIds = null;
176 if (queue.getExternalIdsColumn() != null) {
177 externalIds = queue.getExternalIdsColumn().getData();
179 if (oldQueue != null && oldQueue.getExternalIdsColumn() != null) {
180 oldExternalIds = oldQueue.getExternalIdsColumn().getData();
182 if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
183 removeOldExternalIds(transaction, queuesBuilder, oldExternalIds, queue, nodeIId);
185 if (externalIds != null && !externalIds.isEmpty()) {
186 setNewExternalIds(queuesBuilder, externalIds);
190 private void removeOldExternalIds(ReadWriteTransaction transaction,
191 QueuesBuilder queuesBuilder, Map<String, String> oldExternalIds,
192 Queue queue, InstanceIdentifier<Node> nodeIId) {
193 InstanceIdentifier<Queues> queueIId = nodeIId
194 .augmentation(OvsdbNodeAugmentation.class)
195 .child(Queues.class, queuesBuilder.build().getKey());
196 Set<String> externalIdsKeys = oldExternalIds.keySet();
197 for (String extIdKey : externalIdsKeys) {
198 KeyedInstanceIdentifier<QueuesExternalIds, QueuesExternalIdsKey> externalIId =
200 .child(QueuesExternalIds.class, new QueuesExternalIdsKey(extIdKey));
201 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
205 private void setNewExternalIds(QueuesBuilder queuesBuilder,
206 Map<String, String> externalIds) {
207 Set<String> externalIdsKeys = externalIds.keySet();
208 List<QueuesExternalIds> externalIdsList = new ArrayList<>();
209 String externalIdValue;
210 for (String extIdKey : externalIdsKeys) {
211 externalIdValue = externalIds.get(extIdKey);
212 if (extIdKey != null && externalIdValue != null) {
213 externalIdsList.add(new QueuesExternalIdsBuilder().setQueuesExternalIdKey(extIdKey)
214 .setQueuesExternalIdValue(externalIdValue).build());
217 queuesBuilder.setQueuesExternalIds(externalIdsList);