ovsdb enable checkstyle on error
[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 com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
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.QueuesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class OvsdbQueueUpdateCommand extends AbstractTransactionCommand {
47     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQueueUpdateCommand.class);
48
49     private Map<UUID, Queue> updatedQueueRows;
50     private Map<UUID, Queue> oldQueueRows;
51
52     public OvsdbQueueUpdateCommand(OvsdbConnectionInstance key,
53             TableUpdates updates, DatabaseSchema dbSchema) {
54         super(key, updates, dbSchema);
55         updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class,getUpdates(), getDbSchema());
56         oldQueueRows = TyperUtils.extractRowsOld(Queue.class, getUpdates(), getDbSchema());
57     }
58
59     @Override
60     public void execute(ReadWriteTransaction transaction) {
61         if (updatedQueueRows != null && !updatedQueueRows.isEmpty()) {
62             updateQueue(transaction);
63         }
64     }
65
66     /**
67      * Update the Queues values after finding the related {@OpenVSwitch} list.
68      * <p>
69      * Queue and OpenVSwitch are independent tables in the Open_vSwitch schema
70      * but the OVSDB yang model includes the Queue fields in the
71      * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Queue
72      * updates together and in other cases independently. This method here
73      * assumes the latter.
74      * </p>
75      *
76      * @param transaction the {@link ReadWriteTransaction}
77      */
78     private void updateQueue(ReadWriteTransaction transaction) {
79
80         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
81         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
82         if (ovsdbNode.isPresent()) {
83             for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
84                 Queue queue = entry.getValue();
85                 QueuesBuilder queuesBuilder = new QueuesBuilder();
86                 queuesBuilder.setQueueId(new Uri(getQueueId(queue)));
87                 queuesBuilder.setQueueUuid(new Uuid(entry.getKey().toString()));
88                 Collection<Long> dscp = queue.getDscpColumn().getData();
89                 if (!dscp.isEmpty()) {
90                     queuesBuilder.setDscp(dscp.iterator().next().shortValue());
91                 }
92                 Queue oldQueue = oldQueueRows.get(entry.getKey());
93                 setOtherConfig(transaction, queuesBuilder, oldQueue, queue, nodeIId);
94                 setExternalIds(transaction, queuesBuilder, oldQueue, queue, nodeIId);
95
96                 Queues queues = queuesBuilder.build();
97                 LOG.debug("Update Ovsdb Node {} with queue entries {}",ovsdbNode.get(), queues);
98                 InstanceIdentifier<Queues> iid = nodeIId
99                         .augmentation(OvsdbNodeAugmentation.class)
100                         .child(Queues.class, queues.getKey());
101                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
102                         iid, queues);
103             }
104         }
105     }
106
107     @SuppressWarnings("unchecked")
108     private String getQueueId(Queue queue) {
109         if (queue.getExternalIdsColumn() != null
110                 && queue.getExternalIdsColumn().getData() != null
111                 && queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
112             InstanceIdentifier<Queues> queueIid =
113                     (InstanceIdentifier<Queues>) SouthboundUtil.deserializeInstanceIdentifier(
114                             queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
115             if (queueIid != null) {
116                 QueuesKey queuesKey = queueIid.firstKeyOf(Queues.class);
117                 if (queuesKey != null) {
118                     return queuesKey.getQueueId().getValue();
119                 }
120             }
121         }
122         return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
123     }
124
125     private void setOtherConfig(ReadWriteTransaction transaction,
126             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
127             InstanceIdentifier<Node> nodeIId) {
128         Map<String, String> oldOtherConfigs = null;
129         Map<String, String> otherConfigs = null;
130
131         if (queue.getOtherConfigColumn() != null) {
132             otherConfigs = queue.getOtherConfigColumn().getData();
133         }
134         if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
135             oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
136         }
137         if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
138             removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
139         }
140         if (otherConfigs != null && !otherConfigs.isEmpty()) {
141             setNewOtherConfigs(queuesBuilder, otherConfigs);
142         }
143     }
144
145     private void removeOldConfigs(ReadWriteTransaction transaction,
146             QueuesBuilder queuesBuilder, Map<String, String> oldOtherConfigs,
147             Queue queue, InstanceIdentifier<Node> nodeIId) {
148         InstanceIdentifier<Queues> queueIId = nodeIId
149                 .augmentation(OvsdbNodeAugmentation.class)
150                 .child(Queues.class, queuesBuilder.build().getKey());
151         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
152         for (String otherConfigKey : otherConfigKeys) {
153             KeyedInstanceIdentifier<QueuesOtherConfig, QueuesOtherConfigKey> otherIId =
154                     queueIId
155                     .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
156             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
157         }
158     }
159
160     private void setNewOtherConfigs(QueuesBuilder queuesBuilder,
161             Map<String, String> otherConfig) {
162         Set<String> otherConfigKeys = otherConfig.keySet();
163         List<QueuesOtherConfig> otherConfigList = new ArrayList<>();
164         String otherConfigValue;
165         for (String otherConfigKey : otherConfigKeys) {
166             otherConfigValue = otherConfig.get(otherConfigKey);
167             if (otherConfigKey != null && otherConfigValue != null) {
168                 otherConfigList.add(new QueuesOtherConfigBuilder().setQueueOtherConfigKey(otherConfigKey)
169                         .setQueueOtherConfigValue(otherConfigValue).build());
170             }
171         }
172         queuesBuilder.setQueuesOtherConfig(otherConfigList);
173     }
174
175     private void setExternalIds(ReadWriteTransaction transaction,
176             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
177             InstanceIdentifier<Node> nodeIId) {
178         Map<String, String> oldExternalIds = null;
179         Map<String, String> externalIds = null;
180
181         if (queue.getExternalIdsColumn() != null) {
182             externalIds = queue.getExternalIdsColumn().getData();
183         }
184         if (oldQueue != null && oldQueue.getExternalIdsColumn() != null) {
185             oldExternalIds = oldQueue.getExternalIdsColumn().getData();
186         }
187         if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
188             removeOldExternalIds(transaction, queuesBuilder, oldExternalIds, queue, nodeIId);
189         }
190         if (externalIds != null && !externalIds.isEmpty()) {
191             setNewExternalIds(queuesBuilder, externalIds);
192         }
193     }
194
195     private void removeOldExternalIds(ReadWriteTransaction transaction,
196             QueuesBuilder queuesBuilder, Map<String, String> oldExternalIds,
197             Queue queue, InstanceIdentifier<Node> nodeIId) {
198         InstanceIdentifier<Queues> queueIId = nodeIId
199                 .augmentation(OvsdbNodeAugmentation.class)
200                 .child(Queues.class, queuesBuilder.build().getKey());
201         Set<String> externalIdsKeys = oldExternalIds.keySet();
202         for (String extIdKey : externalIdsKeys) {
203             KeyedInstanceIdentifier<QueuesExternalIds, QueuesExternalIdsKey> externalIId =
204                     queueIId
205                     .child(QueuesExternalIds.class, new QueuesExternalIdsKey(extIdKey));
206             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
207         }
208     }
209
210     private void setNewExternalIds(QueuesBuilder queuesBuilder,
211             Map<String, String> externalIds) {
212         Set<String> externalIdsKeys = externalIds.keySet();
213         List<QueuesExternalIds> externalIdsList = new ArrayList<>();
214         String externalIdValue;
215         for (String extIdKey : externalIdsKeys) {
216             externalIdValue = externalIds.get(extIdKey);
217             if (extIdKey != null && externalIdValue != null) {
218                 externalIdsList.add(new QueuesExternalIdsBuilder().setQueuesExternalIdKey(extIdKey)
219                         .setQueuesExternalIdValue(externalIdValue).build());
220             }
221         }
222         queuesBuilder.setQueuesExternalIds(externalIdsList);
223     }
224
225 }