2 * Copyright (c) 2016 Intel Corporation and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
10 import static java.nio.charset.StandardCharsets.UTF_8;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
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.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
23 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
26 import org.opendaylight.ovsdb.utils.yang.YangUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQueueRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class QosUpdateCommand implements TransactCommand {
44 private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
47 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
48 DataChangeEvent events, InstanceIdentifierCodec instanceIdentifierCodec) {
49 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, QosEntries.class),
50 instanceIdentifierCodec);
54 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
55 Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
56 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, QosEntries.class),
57 instanceIdentifierCodec);
60 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
61 Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated,
62 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()) {
69 OvsdbNodeAugmentation operNode =
70 state.getBridgeNode(iid).get().augmentation(OvsdbNodeAugmentation.class);
72 QosEntries qosEntry = qosMapEntry.getValue();
73 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
75 if (qosEntry.getQosType() != null) {
76 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
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)));
89 qos.setQueues(newQueueList);
91 Map<String, String> externalIdsMap = new HashMap<>();
93 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
94 QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
95 } catch (NullPointerException e) {
96 LOG.warn("Incomplete Qos external IDs", e);
98 externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
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);
107 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
108 QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
109 } catch (NullPointerException e) {
110 LOG.warn("Incomplete Qos other_config", e);
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());
120 UUID uuid = new UUID(operQosUuid.getValue());
121 Qos extraQos = TyperUtils.getTypedRowWrapper(
122 transaction.getDatabaseSchema(), Qos.class, null);
123 extraQos.getUuidColumn().setData(uuid);
124 transaction.add(op.update(qos)
125 .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
126 LOG.info("Updated QoS Uuid : {} for node : {} ", operQosUuid, operNode.getConnectionInfo());
131 private String getQueueUuid(OvsdbQueueRef queueRef, OvsdbNodeAugmentation operNode) {
132 QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
133 if (operNode.getQueues() != null && !operNode.getQueues().isEmpty()) {
134 for (Queues queue : operNode.getQueues()) {
135 if (queue.getQueueId().equals(queueKey.getQueueId())) {
136 return queue.getQueueUuid().getValue();
140 return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
141 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes(UTF_8));
144 private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
145 if (operQosEntries != null && !operQosEntries.isEmpty()) {
146 for (QosEntries qosEntry : operQosEntries) {
147 if (qosEntry.getQosId().equals(qosId)) {
148 return qosEntry.getQosUuid();