Make methods static
[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.Map.Entry;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.mdsal.common.api.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.InstanceIdentifierCodec;
26 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
27 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
28 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIdsKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
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     public OvsdbQueueUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
56             TableUpdates updates, DatabaseSchema dbSchema) {
57         super(key, updates, dbSchema);
58         this.instanceIdentifierCodec = instanceIdentifierCodec;
59         updatedQueueRows = TyperUtils.extractRowsUpdated(Queue.class,getUpdates(), getDbSchema());
60         oldQueueRows = TyperUtils.extractRowsOld(Queue.class, getUpdates(), getDbSchema());
61     }
62
63     @Override
64     public void execute(ReadWriteTransaction transaction) {
65         if (updatedQueueRows != null && !updatedQueueRows.isEmpty()) {
66             updateQueue(transaction);
67         }
68     }
69
70     /**
71      * Update the Queues values after finding the related {@OpenVSwitch} list.
72      * <p>
73      * Queue and OpenVSwitch are independent tables in the Open_vSwitch schema
74      * but the OVSDB yang model includes the Queue fields in the
75      * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Queue
76      * updates together and in other cases independently. This method here
77      * assumes the latter.
78      * </p>
79      *
80      * @param transaction the {@link ReadWriteTransaction}
81      */
82     private void updateQueue(ReadWriteTransaction transaction) {
83
84         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
85         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
86         if (ovsdbNode.isPresent()) {
87             for (Entry<UUID, Queue> entry : updatedQueueRows.entrySet()) {
88                 Queue queue = entry.getValue();
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                 Queue oldQueue = oldQueueRows.get(entry.getKey());
97                 setOtherConfig(transaction, queuesBuilder, oldQueue, queue, nodeIId);
98                 setExternalIds(transaction, queuesBuilder, oldQueue, queue, nodeIId);
99
100                 Queues queues = queuesBuilder.build();
101                 LOG.debug("Update Ovsdb Node {} with queue entries {}",ovsdbNode.get(), queues);
102                 InstanceIdentifier<Queues> iid = nodeIId
103                         .augmentation(OvsdbNodeAugmentation.class)
104                         .child(Queues.class, queues.key());
105                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
106                         iid, queues);
107             }
108         }
109     }
110
111     @SuppressWarnings("unchecked")
112     private String getQueueId(Queue queue) {
113         if (queue.getExternalIdsColumn() != null
114                 && queue.getExternalIdsColumn().getData() != null) {
115             if (queue.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
116                 InstanceIdentifier<Queues> queueIid =
117                         (InstanceIdentifier<Queues>) instanceIdentifierCodec.bindingDeserializerOrNull(
118                                 queue.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY));
119                 if (queueIid != null) {
120                     QueuesKey queuesKey = queueIid.firstKeyOf(Queues.class);
121                     if (queuesKey != null) {
122                         return queuesKey.getQueueId().getValue();
123                     }
124                 }
125             } else if (queue.getExternalIdsColumn().getData()
126                     .containsKey(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY)) {
127                 return queue.getExternalIdsColumn().getData().get(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY);
128             }
129         }
130         return SouthboundConstants.QUEUE_URI_PREFIX + "://" + queue.getUuid().toString();
131     }
132
133     private static void setOtherConfig(ReadWriteTransaction transaction,
134             QueuesBuilder queuesBuilder, Queue oldQueue, Queue queue,
135             InstanceIdentifier<Node> nodeIId) {
136         Map<String, String> oldOtherConfigs = null;
137         Map<String, String> otherConfigs = null;
138
139         if (queue.getOtherConfigColumn() != null) {
140             otherConfigs = queue.getOtherConfigColumn().getData();
141         }
142         if (oldQueue != null && oldQueue.getOtherConfigColumn() != null) {
143             oldOtherConfigs = oldQueue.getOtherConfigColumn().getData();
144         }
145         if (oldOtherConfigs != null && !oldOtherConfigs.isEmpty()) {
146             removeOldConfigs(transaction, queuesBuilder, oldOtherConfigs, queue, nodeIId);
147         }
148         if (otherConfigs != null && !otherConfigs.isEmpty()) {
149             setNewOtherConfigs(queuesBuilder, otherConfigs);
150         }
151     }
152
153     private static void removeOldConfigs(ReadWriteTransaction transaction,
154             QueuesBuilder queuesBuilder, Map<String, String> oldOtherConfigs,
155             Queue queue, InstanceIdentifier<Node> nodeIId) {
156         InstanceIdentifier<Queues> queueIId = nodeIId
157                 .augmentation(OvsdbNodeAugmentation.class)
158                 .child(Queues.class, queuesBuilder.build().key());
159         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
160         for (String otherConfigKey : otherConfigKeys) {
161             KeyedInstanceIdentifier<QueuesOtherConfig, QueuesOtherConfigKey> otherIId =
162                     queueIId
163                     .child(QueuesOtherConfig.class, new QueuesOtherConfigKey(otherConfigKey));
164             transaction.delete(LogicalDatastoreType.OPERATIONAL, otherIId);
165         }
166     }
167
168     private static void setNewOtherConfigs(QueuesBuilder queuesBuilder,
169             Map<String, String> otherConfig) {
170         List<QueuesOtherConfig> otherConfigList = new ArrayList<>();
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);
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,
218             Map<String, String> externalIds) {
219         List<QueuesExternalIds> externalIdsList = new ArrayList<>();
220         for (Entry<String, String> entry : externalIds.entrySet()) {
221             String extIdKey = entry.getKey();
222             String externalIdValue = entry.getValue();
223             if (extIdKey != null && externalIdValue != null) {
224                 externalIdsList.add(new QueuesExternalIdsBuilder().setQueuesExternalIdKey(extIdKey)
225                         .setQueuesExternalIdValue(externalIdValue).build());
226             }
227         }
228         queuesBuilder.setQueuesExternalIds(externalIdsList);
229     }
230
231 }