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;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
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.rev100924.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.ovsdb.node.attributes.QosEntries;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class QosUpdateCommand implements TransactCommand {
41 private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
44 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
45 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
46 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, OvsdbNodeAugmentation.class));
50 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
51 Collection<DataTreeModification<Node>> modifications) {
52 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, OvsdbNodeAugmentation.class));
55 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
56 Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> createdOrUpdated) {
57 for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
58 createdOrUpdated.entrySet()) {
59 updateQos(transaction, state, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
63 private void updateQos(
64 TransactionBuilder transaction, BridgeOperationalState state,
65 InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
67 List<QosEntries> qosEntries = ovsdbNode.getQosEntries();
69 if (!state.getBridgeNode(iid).isPresent()) {
72 OvsdbNodeAugmentation operNode = state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
73 List<QosEntries> operQosEntries = operNode.getQosEntries();
75 if (qosEntries != null) {
76 for (QosEntries qosEntry : qosEntries) {
77 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
79 if (qosEntry.getQosType() != null) {
80 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
83 Uuid qosUuid = getQosEntryUuid(operQosEntries, qosEntry.getQosId());
85 if (qosUuid != null) {
86 uuid = new UUID(qosUuid.getValue());
89 List<QueueList> queueList = qosEntry.getQueueList();
90 Map<Long, UUID>newQueueList = new HashMap<>();
91 if (queueList != null && !queueList.isEmpty()) {
92 for (QueueList queue : queueList) {
93 newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
96 qos.setQueues(newQueueList);
98 Map<String, String> externalIdsMap = new HashMap<>();
100 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
101 QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
102 } catch (NullPointerException e) {
103 LOG.warn("Incomplete Qos external IDs", e);
105 externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
106 qos.setExternalIds(externalIdsMap);
109 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
110 QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
111 } catch (NullPointerException e) {
112 LOG.warn("Incomplete Qos other_config", e);
115 transaction.add(op.insert(qos)).build();
117 Qos extraQos = TyperUtils.getTypedRowWrapper(
118 transaction.getDatabaseSchema(), Qos.class, null);
119 extraQos.getUuidColumn().setData(uuid);
120 transaction.add(op.update(qos)
121 .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
128 private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
129 if (operQosEntries != null && !operQosEntries.isEmpty()) {
130 for (QosEntries qosEntry : operQosEntries) {
131 if (qosEntry.getQosId().equals(qosId)) {
132 return qosEntry.getQosUuid();