Use a utility function for key-value to map conversions
[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 extends AbstractTransactCommand {
38     private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
39
40
41     public QueueUpdateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
42         super(state, changes);
43     }
44
45     @Override
46     public void execute(TransactionBuilder transaction) {
47         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> created =
48                 TransactUtils.extractCreated(getChanges(),OvsdbNodeAugmentation.class);
49         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
50             created.entrySet()) {
51             updateQueue(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
52         }
53         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
54                 TransactUtils.extractUpdated(getChanges(),OvsdbNodeAugmentation.class);
55         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
56             updated.entrySet()) {
57             updateQueue(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
58         }
59     }
60
61     private void updateQueue(
62             TransactionBuilder transaction,
63             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
64
65         List<Queues> queueList = ovsdbNode.getQueues();
66
67         if (!getOperationalState().getBridgeNode(iid).isPresent()) {
68             return;
69         }
70         OvsdbNodeAugmentation operNode = getOperationalState().getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
71         List<Queues> operQueues = operNode.getQueues();
72
73         if (queueList != null) {
74             for (Queues queueEntry : queueList) {
75                 Queue queue = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Queue.class);
76
77                 if (queueEntry.getDscp() != null) {
78                     try {
79                         Set<Long> dscpSet = new HashSet<>();
80                             if (dscpSet.add(new Long(queueEntry.getDscp().toString()))) {
81                             queue.setDscp(dscpSet);
82                         }
83                     } catch (NumberFormatException e) {
84                         LOG.warn("Invalid DSCP {} setting for Queue {}", queueEntry.getDscp(), queueEntry, e);
85                     }
86                 }
87
88                 Uuid queueUuid = getQueueEntryUuid(operQueues, queueEntry.getQueueId());
89                 UUID uuid = null;
90                 if (queueUuid != null) {
91                     uuid = new UUID(queueUuid.getValue());
92                 }
93
94                 Map<String, String> externalIdsMap = new HashMap<>();
95                 try {
96                     YangUtils.copyYangKeyValueListToMap(externalIdsMap, queueEntry.getQueuesExternalIds(),
97                             QueuesExternalIds::getQueuesExternalIdKey, QueuesExternalIds::getQueuesExternalIdValue);
98                 } catch (NullPointerException e) {
99                     LOG.warn("Incomplete Queue external IDs", e);
100                 }
101                 externalIdsMap.put(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY, queueEntry.getQueueId().getValue());
102                 queue.setExternalIds(externalIdsMap);
103
104                 try {
105                     queue.setOtherConfig(YangUtils.convertYangKeyValueListToMap(queueEntry.getQueuesOtherConfig(),
106                             QueuesOtherConfig::getQueueOtherConfigKey, QueuesOtherConfig::getQueueOtherConfigValue));
107                 } catch (NullPointerException e) {
108                     LOG.warn("Incomplete Queue other_config", e);
109                 }
110                 if (uuid == null) {
111                     transaction.add(op.insert(queue)).build();
112                 } else {
113                     transaction.add(op.update(queue)).build();
114                     Queue extraQueue = TyperUtils.getTypedRowWrapper(
115                             transaction.getDatabaseSchema(), Queue.class, null);
116                     extraQueue.getUuidColumn().setData(uuid);
117                     transaction.add(op.update(queue.getSchema())
118                             .where(extraQueue.getUuidColumn().getSchema().opEqual(uuid)).build());
119                 }
120                 transaction.build();
121             }
122         }
123     }
124
125     private Uuid getQueueEntryUuid(List<Queues> operQueues, Uri queueId) {
126         if (operQueues != null && !operQueues.isEmpty()) {
127             for (Queues queueEntry : operQueues) {
128                 if (queueEntry.getQueueId().equals(queueId)) {
129                     return queueEntry.getQueueUuid();
130                 }
131             }
132         }
133         return null;
134     }
135 }