Do not read bridge node multiple times
[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 java.nio.charset.StandardCharsets.UTF_8;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.opendaylight.mdsal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
22 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.utils.yang.YangUtils;
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.OvsdbQueueRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
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.QueuesKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class QosUpdateCommand implements TransactCommand {
43     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
44
45     @Override
46     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
47             final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
48         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, QosEntries.class),
49                 instanceIdentifierCodec);
50     }
51
52     @Override
53     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
54             final Collection<DataTreeModification<Node>> modifications,
55             final InstanceIdentifierCodec instanceIdentifierCodec) {
56         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, QosEntries.class),
57                 instanceIdentifierCodec);
58     }
59
60     @SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
61     private static void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
62             final Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated,
63             final InstanceIdentifierCodec instanceIdentifierCodec) {
64         for (Entry<InstanceIdentifier<QosEntries>, QosEntries> qosMapEntry: createdOrUpdated.entrySet()) {
65             InstanceIdentifier<OvsdbNodeAugmentation> iid =
66                     qosMapEntry.getKey().firstIdentifierOf(OvsdbNodeAugmentation.class);
67             final var optBridgeNode = state.getBridgeNode(iid);
68             if (optBridgeNode.isEmpty()) {
69                 return;
70             }
71
72             OvsdbNodeAugmentation operNode = optBridgeNode.orElseThrow().augmentation(OvsdbNodeAugmentation.class);
73
74             QosEntries qosEntry = qosMapEntry.getValue();
75             Qos qos = transaction.getTypedRowWrapper(Qos.class);
76
77             if (qosEntry.getQosType() != null) {
78                 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
79             }
80
81             Map<QueueListKey, QueueList> queueList = qosEntry.getQueueList();
82             Map<Long, UUID> newQueueList = new HashMap<>();
83             if (queueList != null && !queueList.isEmpty()) {
84                 for (QueueList queue : queueList.values()) {
85                     if (queue.getQueueRef() != null) {
86                         newQueueList.put(queue.getQueueNumber().toJava(),
87                                 new UUID(getQueueUuid(queue.getQueueRef(), operNode)));
88                     }
89                 }
90             }
91             qos.setQueues(newQueueList);
92
93             Map<String, String> externalIdsMap = new HashMap<>();
94             try {
95                 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
96                         QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
97             } catch (NullPointerException e) {
98                 LOG.warn("Incomplete Qos external IDs", e);
99             }
100             externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
101
102                     instanceIdentifierCodec.serialize(
103                     SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
104                     .augmentation(OvsdbNodeAugmentation.class)
105                     .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
106             qos.setExternalIds(externalIdsMap);
107
108             try {
109                 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
110                         QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
111             } catch (NullPointerException e) {
112                 LOG.warn("Incomplete Qos other_config", e);
113             }
114
115             Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.key());
116             if (operQosUuid == null) {
117                 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
118                         + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes(UTF_8)));
119                 transaction.add(op.insert(qos).withId(namedUuid.toString()));
120                 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
121             } else {
122                 UUID uuid = new UUID(operQosUuid.getValue());
123                 Qos extraQos = transaction.getTypedRowSchema(Qos.class);
124                 extraQos.getUuidColumn().setData(uuid);
125                 transaction.add(op.update(qos)
126                         .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
127                 LOG.info("Updated  QoS Uuid : {} for node : {} ", operQosUuid, operNode.getConnectionInfo());
128             }
129         }
130     }
131
132     private static String getQueueUuid(final OvsdbQueueRef queueRef, final OvsdbNodeAugmentation operNode) {
133         QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
134         Map<QueuesKey, Queues> queues = operNode.getQueues();
135         if (queues != null) {
136             Queues queue = queues.get(queueKey);
137             if (queue != null) {
138                 return queue.getQueueUuid().getValue();
139             }
140         }
141         return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
142                 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes(UTF_8));
143     }
144
145     private static Uuid getQosEntryUuid(final Map<QosEntriesKey, QosEntries> operQosEntries,
146             final QosEntriesKey qosId) {
147         if (operQosEntries != null) {
148             QosEntries qosEntry = operQosEntries.get(qosId);
149             if (qosEntry != null) {
150                 return qosEntry.getQosUuid();
151             }
152         }
153         return null;
154     }
155 }