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.HashSet;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
24 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
25 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
26 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
27 import org.opendaylight.ovsdb.utils.yang.YangUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class QueueUpdateCommand implements TransactCommand {
41 private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
44 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
45 final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
46 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, Queues.class),
47 instanceIdentifierCodec);
51 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
52 final Collection<DataTreeModification<Node>> modifications,
53 final InstanceIdentifierCodec instanceIdentifierCodec) {
54 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, Queues.class),
55 instanceIdentifierCodec);
58 private static void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
59 final Map<InstanceIdentifier<Queues>, Queues> createdOrUpdated,
60 final InstanceIdentifierCodec instanceIdentifierCodec) {
61 for (Entry<InstanceIdentifier<Queues>, Queues> queueMapEntry: createdOrUpdated.entrySet()) {
62 InstanceIdentifier<OvsdbNodeAugmentation> iid =
63 queueMapEntry.getKey().firstIdentifierOf(OvsdbNodeAugmentation.class);
64 if (!state.getBridgeNode(iid).isPresent()) {
68 Queues queueEntry = queueMapEntry.getValue();
69 Queue queue = transaction.getTypedRowWrapper(Queue.class);
71 if (queueEntry.getDscp() != null) {
73 Set<Long> dscpSet = new HashSet<>();
74 if (dscpSet.add(Long.valueOf(queueEntry.getDscp().toString()))) {
75 queue.setDscp(dscpSet);
77 } catch (NumberFormatException e) {
78 LOG.warn("Invalid DSCP {} setting for Queue {}", queueEntry.getDscp(), queueEntry, e);
82 Map<String, String> externalIdsMap = new HashMap<>();
84 YangUtils.copyYangKeyValueListToMap(externalIdsMap, queueEntry.getQueuesExternalIds(),
85 QueuesExternalIds::getQueuesExternalIdKey, QueuesExternalIds::getQueuesExternalIdValue);
86 } catch (NullPointerException e) {
87 LOG.warn("Incomplete Queue external IDs", e);
89 externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
90 instanceIdentifierCodec.serialize(
91 SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
92 .augmentation(OvsdbNodeAugmentation.class)
93 .child(Queues.class, new QueuesKey(queueEntry.getQueueId()))));
94 queue.setExternalIds(externalIdsMap);
97 queue.setOtherConfig(YangUtils.convertYangKeyValueListToMap(queueEntry.getQueuesOtherConfig(),
98 QueuesOtherConfig::getQueueOtherConfigKey, QueuesOtherConfig::getQueueOtherConfigValue));
99 } catch (NullPointerException e) {
100 LOG.warn("Incomplete Queue other_config", e);
103 OvsdbNodeAugmentation operNode =
104 state.getBridgeNode(iid).get().augmentation(OvsdbNodeAugmentation.class);
105 Uuid operQueueUuid = getQueueEntryUuid(operNode.getQueues(), queueEntry.getQueueId());
106 if (operQueueUuid == null) {
107 UUID namedUuid = new UUID(SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
108 + TransactUtils.bytesToHexString(queueEntry.getQueueId().getValue().getBytes(UTF_8)));
109 transaction.add(op.insert(queue).withId(namedUuid.toString()));
110 LOG.info("Added queue Uuid : {} for Ovsdb Node : {}",
111 namedUuid, operNode);
113 UUID uuid = new UUID(operQueueUuid.getValue());
114 Queue extraQueue = transaction.getTypedRowSchema(Queue.class);
115 extraQueue.getUuidColumn().setData(uuid);
116 transaction.add(op.update(queue)
117 .where(extraQueue.getUuidColumn().getSchema().opEqual(uuid)).build());
118 LOG.info("Updated queue entries: {} for Ovsdb Node : {}",
124 private static Uuid getQueueEntryUuid(final List<Queues> operQueues, final Uri queueId) {
125 if (operQueues != null && !operQueues.isEmpty()) {
126 for (Queues queueEntry : operQueues) {
127 if (queueEntry.getQueueId().equals(queueId)) {
128 return queueEntry.getQueueUuid();