Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.rev130715.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             if (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             } else if (queue.getExternalIdsColumn().getData()
122                     .containsKey(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY)) {
123                 return queue.getExternalIdsColumn().getData().get(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY);
124             }
125         }
126         return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
127     }
128
129     private void setOtherConfig(ReadWriteTransaction transaction,
130             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
131             InstanceIdentifier<Node> nodeIId) {
132         Map<String, String> oldOtherConfigs = null;
133         Map<String, String> otherConfigs = null;
134
135         if (queue.getOtherConfigColumn() != null) {
136             otherConfigs = queue.getOtherConfigColumn().getData();
137         }
138         if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
139             oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
140         }
141         if ((oldOtherConfigs != null) && !oldOtherConfigs.isEmpty()) {
142             removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
143         }
144         if (otherConfigs != null && !otherConfigs.isEmpty()) {
145             setNewOtherConfigs(queuesBuilder, otherConfigs);
146         }
147     }
148
149     private void removeOldConfigs(ReadWriteTransaction transaction,
150             QueuesBuilder queuesBuilder, Map<String, String> oldOtherConfigs,
151             Queue queue, InstanceIdentifier<Node> nodeIId) {
152         InstanceIdentifier<Queues> queueIId = nodeIId
153                 .augmentation(OvsdbNodeAugmentation.class)
154                 .child(Queues.class, queuesBuilder.build().getKey());
155         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
156         for (String otherConfigKey : otherConfigKeys) {
157             KeyedInstanceIdentifier<QueuesOtherConfig, QueuesOtherConfigKey> otherIId =
158                     queueIId
159                     .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
160             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
161         }
162     }
163
164     private void setNewOtherConfigs(QueuesBuilder queuesBuilder,
165             Map<String, String> otherConfig) {
166         Set<String> otherConfigKeys = otherConfig.keySet();
167         List<QueuesOtherConfig> otherConfigList = new ArrayList<>();
168         String otherConfigValue;
169         for (String otherConfigKey : otherConfigKeys) {
170             otherConfigValue = otherConfig.get(otherConfigKey);
171             if (otherConfigKey != null && otherConfigValue != null) {
172                 otherConfigList.add(new QueuesOtherConfigBuilder().setQueueOtherConfigKey(otherConfigKey)
173                         .setQueueOtherConfigValue(otherConfigValue).build());
174             }
175         }
176         queuesBuilder.setQueuesOtherConfig(otherConfigList);
177     }
178
179     private void setExternalIds(ReadWriteTransaction transaction,
180             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
181             InstanceIdentifier<Node> nodeIId) {
182         Map<String, String> oldExternalIds = null;
183         Map<String, String> externalIds = null;
184
185         if (queue.getExternalIdsColumn() != null) {
186             externalIds = queue.getExternalIdsColumn().getData();
187         }
188         if (oldQueue != null && oldQueue.getExternalIdsColumn() != null) {
189             oldExternalIds = oldQueue.getExternalIdsColumn().getData();
190         }
191         if ((oldExternalIds != null) && !oldExternalIds.isEmpty()) {
192             removeOldExternalIds(transaction, queuesBuilder, oldExternalIds, queue, nodeIId);
193         }
194         if (externalIds != null && !externalIds.isEmpty()) {
195             setNewExternalIds(queuesBuilder, externalIds);
196         }
197     }
198
199     private void removeOldExternalIds(ReadWriteTransaction transaction,
200             QueuesBuilder queuesBuilder, Map<String, String> oldExternalIds,
201             Queue queue, InstanceIdentifier<Node> nodeIId) {
202         InstanceIdentifier<Queues> queueIId = nodeIId
203                 .augmentation(OvsdbNodeAugmentation.class)
204                 .child(Queues.class, queuesBuilder.build().getKey());
205         Set<String> externalIdsKeys = oldExternalIds.keySet();
206         for (String extIdKey : externalIdsKeys) {
207             KeyedInstanceIdentifier<QueuesExternalIds, QueuesExternalIdsKey> externalIId =
208                     queueIId
209                     .child(QueuesExternalIds.class, new QueuesExternalIdsKey(extIdKey));
210             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIId);
211         }
212     }
213
214     private void setNewExternalIds(QueuesBuilder queuesBuilder,
215             Map<String, String> externalIds) {
216         Set<String> externalIdsKeys = externalIds.keySet();
217         List<QueuesExternalIds> externalIdsList = new ArrayList<>();
218         String externalIdValue;
219         for (String extIdKey : externalIdsKeys) {
220             externalIdValue = externalIds.get(extIdKey);
221             if (extIdKey != null && externalIdValue != null) {
222                 externalIdsList.add(new QueuesExternalIdsBuilder().setQueuesExternalIdKey(extIdKey)
223                         .setQueuesExternalIdValue(externalIdValue).build());
224             }
225         }
226         queuesBuilder.setQueuesExternalIds(externalIdsList);
227     }
228
229 }