Use a utility function for key-value to map conversions
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QosUpdateCommand.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.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
22 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
23 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
24 import org.opendaylight.ovsdb.utils.yang.YangUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
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 QosUpdateCommand extends AbstractTransactCommand {
38     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
39
40
41     public QosUpdateCommand(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             updateQos(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             updateQos(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
58         }
59     }
60
61     private void updateQos(
62             TransactionBuilder transaction,
63             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
64
65         List<QosEntries> qosEntries = ovsdbNode.getQosEntries();
66
67         if (!getOperationalState().getBridgeNode(iid).isPresent()) {
68             return;
69         }
70         OvsdbNodeAugmentation operNode = getOperationalState().getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
71         List<QosEntries> operQosEntries = operNode.getQosEntries();
72
73         if (qosEntries != null) {
74             for (QosEntries qosEntry : qosEntries) {
75                 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
76
77                 if (qosEntry.getQosType() != null) {
78                     qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
79                 }
80
81                 Uuid qosUuid = getQosEntryUuid(operQosEntries, qosEntry.getQosId());
82                 UUID uuid = null;
83                 if (qosUuid != null) {
84                     uuid = new UUID(qosUuid.getValue());
85                 }
86
87                 List<QueueList> queueList = qosEntry.getQueueList();
88                 Map<Long, UUID>newQueueList = new HashMap<>();
89                 if (queueList != null && !queueList.isEmpty()) {
90                     for (QueueList queue : queueList) {
91                         newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
92                     }
93                 }
94                 qos.setQueues(newQueueList);
95
96                 Map<String, String> externalIdsMap = new HashMap<>();
97                 try {
98                     YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
99                             QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
100                 } catch (NullPointerException e) {
101                     LOG.warn("Incomplete Qos external IDs", e);
102                 }
103                 externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
104                 qos.setExternalIds(externalIdsMap);
105
106                 try {
107                     qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
108                             QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
109                 } catch (NullPointerException e) {
110                     LOG.warn("Incomplete Qos other_config", e);
111                 }
112                 if (uuid == null) {
113                     transaction.add(op.insert(qos)).build();
114                 } else {
115                     Qos extraQos = TyperUtils.getTypedRowWrapper(
116                             transaction.getDatabaseSchema(), Qos.class, null);
117                     extraQos.getUuidColumn().setData(uuid);
118                     transaction.add(op.update(qos)
119                             .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
120                 }
121                 transaction.build();
122             }
123         }
124     }
125
126     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
127         if (operQosEntries != null && !operQosEntries.isEmpty()) {
128             for (QosEntries qosEntry : operQosEntries) {
129                 if (qosEntry.getQosId().equals(qosId)) {
130                     return qosEntry.getQosUuid();
131                 }
132             }
133         }
134         return null;
135     }
136 }