571256a2181ccbe186082060d74dbba16f4aa72a
[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 package org.opendaylight.ovsdb.southbound.transactions.md;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.Collection;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import java.util.Set;
16 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
23 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
24 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
25 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
26 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
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.opendaylight.yangtools.yang.binding.util.BindingMap;
43 import org.opendaylight.yangtools.yang.common.Uint8;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class OvsdbQueueUpdateCommand extends AbstractTransactionCommand {
48     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQueueUpdateCommand.class);
49
50     private final InstanceIdentifierCodec instanceIdentifierCodec;
51
52     private final Map<UUID, Queue> updatedQueueRows;
53     private final Map<UUID, Queue> oldQueueRows;
54
55     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
56     public OvsdbQueueUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
57             TableUpdates updates, DatabaseSchema dbSchema) {
58         super(key, updates, dbSchema);
59         this.instanceIdentifierCodec = instanceIdentifierCodec;
60         updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class,getUpdates(), getDbSchema());
61         oldQueueRows = TyperUtils.extractRowsOld(Queue.class, getUpdates(), getDbSchema());
62     }
63
64     @Override
65     public void execute(ReadWriteTransaction transaction) {
66         if (updatedQueueRows != null && !updatedQueueRows.isEmpty()) {
67             updateQueue(transaction);
68         }
69     }
70
71     /**
72      * Update the Queues values after finding the related {@OpenVSwitch} list.
73      * <p>
74      * Queue and OpenVSwitch are independent tables in the Open_vSwitch schema
75      * but the OVSDB yang model includes the Queue fields in the
76      * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Queue
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      */
83     private void updateQueue(ReadWriteTransaction transaction) {
84
85         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
86         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
87         if (ovsdbNode.isPresent()) {
88             for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
89                 Queue queue = entry.getValue();
90                 QueuesBuilder queuesBuilder = new QueuesBuilder();
91                 queuesBuilder.setQueueId(new Uri(getQueueId(queue)));
92                 queuesBuilder.setQueueUuid(new Uuid(entry.getKey().toString()));
93                 Collection<Long> dscp = queue.getDscpColumn().getData();
94                 if (!dscp.isEmpty()) {
95                     queuesBuilder.setDscp(Uint8.valueOf(dscp.iterator().next().shortValue()));
96                 }
97                 Queue oldQueue = oldQueueRows.get(entry.getKey());
98                 setOtherConfig(transaction, queuesBuilder, oldQueue, queue, nodeIId);
99                 setExternalIds(transaction, queuesBuilder, oldQueue, queue, nodeIId);
100
101                 Queues queues = queuesBuilder.build();
102                 LOG.debug("Update Ovsdb Node {} with queue entries {}",ovsdbNode.get(), queues);
103                 InstanceIdentifier<Queues> iid = nodeIId
104                         .augmentation(OvsdbNodeAugmentation.class)
105                         .child(Queues.class, queues.key());
106                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
107                         iid, queues);
108             }
109         }
110     }
111
112     @SuppressWarnings("unchecked")
113     private String getQueueId(Queue queue) {
114         if (queue.getExternalIdsColumn() != null
115                 && queue.getExternalIdsColumn().getData() != null) {
116             if (queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
117                 InstanceIdentifier<Queues> queueIid =
118                         (InstanceIdentifier<Queues>) instanceIdentifierCodec.bindingDeserializerOrNull(
119                                 queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
120                 if (queueIid != null) {
121                     QueuesKey queuesKey = queueIid.firstKeyOf(Queues.class);
122                     if (queuesKey != null) {
123                         return queuesKey.getQueueId().getValue();
124                     }
125                 }
126             } else if (queue.getExternalIdsColumn().getData()
127                     .containsKey(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY)) {
128                 return queue.getExternalIdsColumn().getData().get(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY);
129             }
130         }
131         return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
132     }
133
134     private static void setOtherConfig(ReadWriteTransaction transaction,
135             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
136             InstanceIdentifier<Node> nodeIId) {
137         Map<String, String> oldOtherConfigs = null;
138         Map<String, String> otherConfigs = null;
139
140         if (queue.getOtherConfigColumn() != null) {
141             otherConfigs = queue.getOtherConfigColumn().getData();
142         }
143         if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
144             oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
145         }
146         if (oldOtherConfigs != null && !oldOtherConfigs.isEmpty()) {
147             removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
148         }
149         if (otherConfigs != null && !otherConfigs.isEmpty()) {
150             setNewOtherConfigs(queuesBuilder, otherConfigs);
151         }
152     }
153
154     private static void removeOldConfigs(ReadWriteTransaction transaction,
155             QueuesBuilder queuesBuilder, Map<String, String> oldOtherConfigs,
156             Queue queue, InstanceIdentifier<Node> nodeIId) {
157         InstanceIdentifier<Queues> queueIId = nodeIId
158                 .augmentation(OvsdbNodeAugmentation.class)
159                 .child(Queues.class, queuesBuilder.build().key());
160         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
161         for (String otherConfigKey : otherConfigKeys) {
162             KeyedInstanceIdentifier<QueuesOtherConfig, QueuesOtherConfigKey> otherIId =
163                     queueIId
164                     .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
165             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
166         }
167     }
168
169     private static void setNewOtherConfigs(QueuesBuilder queuesBuilder, Map<String, String> otherConfig) {
170         var otherConfigList = BindingMap.<QueuesOtherConfigKey, QueuesOtherConfig>orderedBuilder();
171         for (Entry<String, String> entry : otherConfig.entrySet()) {
172             String otherConfigKey = entry.getKey();
173             String otherConfigValue = entry.getValue();
174             if (otherConfigKey != null && otherConfigValue != null) {
175                 otherConfigList.add(new QueuesOtherConfigBuilder().setQueueOtherConfigKey(otherConfigKey)
176                         .setQueueOtherConfigValue(otherConfigValue).build());
177             }
178         }
179         queuesBuilder.setQueuesOtherConfig(otherConfigList.build());
180     }
181
182     private static void setExternalIds(ReadWriteTransaction transaction,
183             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
184             InstanceIdentifier<Node> nodeIId) {
185         Map<String, String> oldExternalIds = null;
186         Map<String, String> externalIds = null;
187
188         if (queue.getExternalIdsColumn() != null) {
189             externalIds = queue.getExternalIdsColumn().getData();
190         }
191         if (oldQueue != null && oldQueue.getExternalIdsColumn() != null) {
192             oldExternalIds = oldQueue.getExternalIdsColumn().getData();
193         }
194         if (oldExternalIds != null && !oldExternalIds.isEmpty()) {
195             removeOldExternalIds(transaction, queuesBuilder, oldExternalIds, queue, nodeIId);
196         }
197         if (externalIds != null && !externalIds.isEmpty()) {
198             setNewExternalIds(queuesBuilder, externalIds);
199         }
200     }
201
202     private static void removeOldExternalIds(ReadWriteTransaction transaction,
203             QueuesBuilder queuesBuilder, Map<String, String> oldExternalIds,
204             Queue queue, InstanceIdentifier<Node> nodeIId) {
205         InstanceIdentifier<Queues> queueIId = nodeIId
206                 .augmentation(OvsdbNodeAugmentation.class)
207                 .child(Queues.class, queuesBuilder.build().key());
208         Set<String> externalIdsKeys = oldExternalIds.keySet();
209         for (String extIdKey : externalIdsKeys) {
210             KeyedInstanceIdentifier<QueuesExternalIds, QueuesExternalIdsKey> externalIId =
211                     queueIId
212                     .child(QueuesExternalIds.class, new QueuesExternalIdsKey(extIdKey));
213             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
214         }
215     }
216
217     private static void setNewExternalIds(QueuesBuilder queuesBuilder, Map<String, String> externalIds) {
218         var externalIdsList = BindingMap.<QueuesExternalIdsKey, QueuesExternalIds>orderedBuilder();
219         for (Entry<String, String> entry : externalIds.entrySet()) {
220             String extIdKey = entry.getKey();
221             String externalIdValue = entry.getValue();
222             if (extIdKey != null && externalIdValue != null) {
223                 externalIdsList.add(new QueuesExternalIdsBuilder().setQueuesExternalIdKey(extIdKey)
224                         .setQueuesExternalIdValue(externalIdValue).build());
225             }
226         }
227         queuesBuilder.setQueuesExternalIds(externalIdsList.build());
228     }
229 }