Make TransactionBuilder type-aware
[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 java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.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.inet.types.rev130715.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.OvsdbQueueRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
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.QueuesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
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     private void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
61             final Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated,
62             final InstanceIdentifierCodec instanceIdentifierCodec) {
63         for (Entry<InstanceIdentifier<QosEntries>, QosEntries> qosMapEntry: createdOrUpdated.entrySet()) {
64             InstanceIdentifier<OvsdbNodeAugmentation> iid =
65                     qosMapEntry.getKey().firstIdentifierOf(OvsdbNodeAugmentation.class);
66             if (!state.getBridgeNode(iid).isPresent()) {
67                 return;
68             }
69             OvsdbNodeAugmentation operNode =
70                 state.getBridgeNode(iid).get().augmentation(OvsdbNodeAugmentation.class);
71
72             QosEntries qosEntry = qosMapEntry.getValue();
73             Qos qos = transaction.getTypedRowWrapper(Qos.class);
74
75             if (qosEntry.getQosType() != null) {
76                 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
77             }
78
79             List<QueueList> queueList = qosEntry.getQueueList();
80             Map<Long, UUID> newQueueList = new HashMap<>();
81             if (queueList != null && !queueList.isEmpty()) {
82                 for (QueueList queue : queueList) {
83                     if (queue.getQueueRef() != null) {
84                         newQueueList.put(queue.getQueueNumber().toJava(),
85                                 new UUID(getQueueUuid(queue.getQueueRef(), operNode)));
86                     }
87                 }
88             }
89             qos.setQueues(newQueueList);
90
91             Map<String, String> externalIdsMap = new HashMap<>();
92             try {
93                 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
94                         QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
95             } catch (NullPointerException e) {
96                 LOG.warn("Incomplete Qos external IDs", e);
97             }
98             externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
99
100                     instanceIdentifierCodec.serialize(
101                     SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
102                     .augmentation(OvsdbNodeAugmentation.class)
103                     .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
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
113             Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.getQosId());
114             if (operQosUuid == null) {
115                 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
116                         + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes(UTF_8)));
117                 transaction.add(op.insert(qos).withId(namedUuid.toString()));
118                 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
119             } else {
120                 UUID uuid = new UUID(operQosUuid.getValue());
121                 Qos extraQos = transaction.getTypedRowSchema(Qos.class);
122                 extraQos.getUuidColumn().setData(uuid);
123                 transaction.add(op.update(qos)
124                         .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
125                 LOG.info("Updated  QoS Uuid : {} for node : {} ", operQosUuid, operNode.getConnectionInfo());
126             }
127         }
128     }
129
130     private String getQueueUuid(final OvsdbQueueRef queueRef, final OvsdbNodeAugmentation operNode) {
131         QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
132         if (operNode.getQueues() != null && !operNode.getQueues().isEmpty()) {
133             for (Queues queue : operNode.getQueues()) {
134                 if (queue.getQueueId().equals(queueKey.getQueueId())) {
135                     return queue.getQueueUuid().getValue();
136                 }
137             }
138         }
139         return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
140                 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes(UTF_8));
141     }
142
143     private Uuid getQosEntryUuid(final List<QosEntries> operQosEntries, final Uri qosId) {
144         if (operQosEntries != null && !operQosEntries.isEmpty()) {
145             for (QosEntries qosEntry : operQosEntries) {
146                 if (qosEntry.getQosId().equals(qosId)) {
147                     return qosEntry.getQosUuid();
148                 }
149             }
150         }
151         return null;
152     }
153 }