6ba2609281c806d8a70ad8b5ab653678b3f40ba0
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbQueueUpdateCommand.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.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;
44
45 import com.google.common.base.Optional;
46
47 public class OvsdbQueueUpdateCommand extends AbstractTransactionCommand {
48     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQueueUpdateCommand.class);
49
50     private Map<UUID, Queue> updatedQueueRows;
51     private Map<UUID, Queue> oldQueueRows;
52
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());
58     }
59
60     @Override
61     public void execute(ReadWriteTransaction transaction) {
62         if (updatedQueueRows != null && !updatedQueueRows.isEmpty()) {
63             updateQueue(transaction);
64         }
65     }
66
67     /**
68      * Update the Queues values after finding the related {@OpenVSwitch} list.
69      * <p>
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
74      * assumes the latter.
75      * </p>
76      *
77      * @param transaction the {@link ReadWriteTransaction}
78      * @param updatedQueueRows updated {@link Queue} rows
79
80      */
81     private void updateQueue(ReadWriteTransaction transaction) {
82
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());
95                 }
96                 setOtherConfig(transaction, queuesBuilder, oldQueue, queue, nodeIId);
97                 setExternalIds(transaction, queuesBuilder, oldQueue, queue, nodeIId);
98
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,
105                         iid, queues);
106             }
107         }
108     }
109
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);
115         } else {
116             return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
117         }
118     }
119
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;
125
126         if (queue.getOtherConfigColumn() != null) {
127             otherConfigs = queue.getOtherConfigColumn().getData();
128         }
129         if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
130             oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
131         }
132         if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
133             removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
134         }
135         if (otherConfigs != null && !otherConfigs.isEmpty()) {
136             setNewOtherConfigs(queuesBuilder, otherConfigs);
137         }
138     }
139
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 =
149                     queueIId
150                     .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
151             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
152         }
153     }
154
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());
165             }
166         }
167         queuesBuilder.setQueuesOtherConfig(otherConfigList);
168     }
169
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;
175
176         if (queue.getExternalIdsColumn() != null) {
177             externalIds = queue.getExternalIdsColumn().getData();
178         }
179         if (oldQueue != null && oldQueue.getExternalIdsColumn() != null) {
180             oldExternalIds = oldQueue.getExternalIdsColumn().getData();
181         }
182         if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
183             removeOldExternalIds(transaction, queuesBuilder, oldExternalIds, queue, nodeIId);
184         }
185         if (externalIds != null && !externalIds.isEmpty()) {
186             setNewExternalIds(queuesBuilder, externalIds);
187         }
188     }
189
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 =
199                     queueIId
200                     .child(QueuesExternalIds.class, new QueuesExternalIdsKey(extIdKey));
201             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
202         }
203     }
204
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());
215             }
216         }
217         queuesBuilder.setQueuesExternalIds(externalIdsList);
218     }
219
220 }