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 org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
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.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
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.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class QosUpdateCommand implements TransactCommand {
45 private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
48 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
49 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
50 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, QosEntries.class));
54 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
55 Collection<DataTreeModification<Node>> modifications) {
56 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, QosEntries.class));
59 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
60 Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated) {
61 for (Entry<InstanceIdentifier<QosEntries>, QosEntries> qosMapEntry: createdOrUpdated.entrySet()) {
62 InstanceIdentifier<OvsdbNodeAugmentation> iid =
63 qosMapEntry.getKey().firstIdentifierOf(OvsdbNodeAugmentation.class);
64 if (!state.getBridgeNode(iid).isPresent()) {
67 OvsdbNodeAugmentation operNode =
68 state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
70 QosEntries qosEntry = qosMapEntry.getValue();
71 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
73 if (qosEntry.getQosType() != null) {
74 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
77 List<QueueList> queueList = qosEntry.getQueueList();
78 Map<Long, UUID> newQueueList = new HashMap<>();
79 if (queueList != null && !queueList.isEmpty()) {
80 for (QueueList queue : queueList) {
81 if (queue.getQueueRef() != null) {
82 newQueueList.put(queue.getQueueNumber(),
83 new UUID(getQueueUuid(queue.getQueueRef(), operNode)));
84 } else if (queue.getQueueUuid() != null) {
85 newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
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,
99 SouthboundUtil.serializeInstanceIdentifier(
100 SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
101 .augmentation(OvsdbNodeAugmentation.class)
102 .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
103 qos.setExternalIds(externalIdsMap);
106 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
107 QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
108 } catch (NullPointerException e) {
109 LOG.warn("Incomplete Qos other_config", e);
112 Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.getQosId());
113 if (operQosUuid == null) {
114 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
115 + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes()));
116 transaction.add(op.insert(qos).withId(namedUuid.toString())).build();
117 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
119 UUID uuid = new UUID(operQosUuid.getValue());
120 Qos extraQos = TyperUtils.getTypedRowWrapper(
121 transaction.getDatabaseSchema(), Qos.class, null);
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());
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());
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();