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