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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.ovsdb.lib.notation.Condition;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.Mutate;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Port;
27 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
28 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
29 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
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.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.collect.ImmutableMap;
45 public class QosUpdateCommand extends AbstractTransactCommand {
46 private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
49 public QosUpdateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
50 super(state, changes);
54 public void execute(TransactionBuilder transaction) {
55 Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> created =
56 TransactUtils.extractCreated(getChanges(),OvsdbNodeAugmentation.class);
57 for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
59 updateQos(transaction, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
61 Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
62 TransactUtils.extractUpdated(getChanges(),OvsdbNodeAugmentation.class);
63 for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
65 updateQos(transaction, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
69 private void updateQos(
70 TransactionBuilder transaction,
71 InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
73 List<QosEntries> qosEntries = ovsdbNode.getQosEntries();
75 if (!getOperationalState().getBridgeNode(iid).isPresent()) {
78 OvsdbNodeAugmentation operNode = getOperationalState().getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
79 List<QosEntries> operQosEntries = operNode.getQosEntries();
81 if (qosEntries != null) {
82 for (QosEntries qosEntry : qosEntries) {
83 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
85 if (qosEntry.getQosType() != null) {
86 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
89 Uuid qosUuid = getQosEntryUuid(operQosEntries, qosEntry.getQosId());
91 if (qosUuid != null) {
92 uuid = new UUID(qosUuid.getValue());
95 List<QueueList> queueList = qosEntry.getQueueList();
96 Map<Long, UUID>newQueueList = new HashMap<>();
97 if (queueList != null && !queueList.isEmpty()) {
98 for (QueueList queue : queueList) {
99 newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
102 qos.setQueues(newQueueList);
104 List<QosExternalIds> externalIds = qosEntry.getQosExternalIds();
105 Map<String, String> externalIdsMap = new HashMap<>();
106 if (externalIds != null) {
107 for (QosExternalIds externalId : externalIds) {
108 externalIdsMap.put(externalId.getQosExternalIdKey(), externalId.getQosExternalIdValue());
111 externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
113 qos.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
114 } catch (NullPointerException e) {
115 LOG.warn("Incomplete Qos external IDs", e);
118 List<QosOtherConfig> otherConfigs = qosEntry.getQosOtherConfig();
119 if (otherConfigs != null) {
120 Map<String, String> otherConfigsMap = new HashMap<>();
121 for (QosOtherConfig otherConfig : otherConfigs) {
122 otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
125 qos.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
126 } catch (NullPointerException e) {
127 LOG.warn("Incomplete Qos other_config", e);
131 transaction.add(op.insert(qos)).build();
133 Qos extraQos = TyperUtils.getTypedRowWrapper(
134 transaction.getDatabaseSchema(), Qos.class, null);
135 extraQos.getUuidColumn().setData(uuid);
136 transaction.add(op.update(qos)
137 .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
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();