Use stateless transaction commands
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QueueUpdateCommand.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.ovsdb.utils.yang.YangUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class QueueUpdateCommand implements TransactCommand {
38     private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
39
40     @Override
41     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
42                         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
43         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, OvsdbNodeAugmentation.class));
44     }
45
46     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
47                          Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> createdOrUpdated) {
48         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
49             createdOrUpdated.entrySet()) {
50             updateQueue(transaction, state, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
51         }
52     }
53
54     private void updateQueue(
55             TransactionBuilder transaction, BridgeOperationalState state,
56             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
57
58         List<Queues> queueList = ovsdbNode.getQueues();
59
60         if (!state.getBridgeNode(iid).isPresent()) {
61             return;
62         }
63         OvsdbNodeAugmentation operNode = state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
64         List<Queues> operQueues = operNode.getQueues();
65
66         if (queueList != null) {
67             for (Queues queueEntry : queueList) {
68                 Queue queue = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Queue.class);
69
70                 if (queueEntry.getDscp() != null) {
71                     try {
72                         Set<Long> dscpSet = new HashSet<>();
73                             if (dscpSet.add(new Long(queueEntry.getDscp().toString()))) {
74                             queue.setDscp(dscpSet);
75                         }
76                     } catch (NumberFormatException e) {
77                         LOG.warn("Invalid DSCP {} setting for Queue {}", queueEntry.getDscp(), queueEntry, e);
78                     }
79                 }
80
81                 Uuid queueUuid = getQueueEntryUuid(operQueues, queueEntry.getQueueId());
82                 UUID uuid = null;
83                 if (queueUuid != null) {
84                     uuid = new UUID(queueUuid.getValue());
85                 }
86
87                 Map<String, String> externalIdsMap = new HashMap<>();
88                 try {
89                     YangUtils.copyYangKeyValueListToMap(externalIdsMap, queueEntry.getQueuesExternalIds(),
90                             QueuesExternalIds::getQueuesExternalIdKey, QueuesExternalIds::getQueuesExternalIdValue);
91                 } catch (NullPointerException e) {
92                     LOG.warn("Incomplete Queue external IDs", e);
93                 }
94                 externalIdsMap.put(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY, queueEntry.getQueueId().getValue());
95                 queue.setExternalIds(externalIdsMap);
96
97                 try {
98                     queue.setOtherConfig(YangUtils.convertYangKeyValueListToMap(queueEntry.getQueuesOtherConfig(),
99                             QueuesOtherConfig::getQueueOtherConfigKey, QueuesOtherConfig::getQueueOtherConfigValue));
100                 } catch (NullPointerException e) {
101                     LOG.warn("Incomplete Queue other_config", e);
102                 }
103                 if (uuid == null) {
104                     transaction.add(op.insert(queue)).build();
105                 } else {
106                     transaction.add(op.update(queue)).build();
107                     Queue extraQueue = TyperUtils.getTypedRowWrapper(
108                             transaction.getDatabaseSchema(), Queue.class, null);
109                     extraQueue.getUuidColumn().setData(uuid);
110                     transaction.add(op.update(queue.getSchema())
111                             .where(extraQueue.getUuidColumn().getSchema().opEqual(uuid)).build());
112                 }
113                 transaction.build();
114             }
115         }
116     }
117
118     private Uuid getQueueEntryUuid(List<Queues> operQueues, Uri queueId) {
119         if (operQueues != null && !operQueues.isEmpty()) {
120             for (Queues queueEntry : operQueues) {
121                 if (queueEntry.getQueueId().equals(queueId)) {
122                     return queueEntry.getQueueUuid();
123                 }
124             }
125         }
126         return null;
127     }
128 }