4d40bc02f7c181b38d02596a1a0f9514ddd90de2
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QueueUpdateCommand.java
1 /*
2  * Copyright (c) 2016 Intel Corporation and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
9
10 import static java.nio.charset.StandardCharsets.UTF_8;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
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.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.Queues;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
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.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class QueueUpdateCommand implements TransactCommand {
40     private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
41
42     @Override
43     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
44             final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
45         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, Queues.class),
46                 instanceIdentifierCodec);
47     }
48
49     @Override
50     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
51             final Collection<DataTreeModification<Node>> modifications,
52             final InstanceIdentifierCodec instanceIdentifierCodec) {
53         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, Queues.class),
54                 instanceIdentifierCodec);
55     }
56
57     @SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
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()) {
65                 return;
66             }
67
68             Queues queueEntry = queueMapEntry.getValue();
69             Queue queue = transaction.getTypedRowWrapper(Queue.class);
70
71             if (queueEntry.getDscp() != null) {
72                 try {
73                     Set<Long> dscpSet = new HashSet<>();
74                     if (dscpSet.add(Long.valueOf(queueEntry.getDscp().toString()))) {
75                         queue.setDscp(dscpSet);
76                     }
77                 } catch (NumberFormatException e) {
78                     LOG.warn("Invalid DSCP {} setting for Queue {}", queueEntry.getDscp(), queueEntry, e);
79                 }
80             }
81
82             Map<String, String> externalIdsMap = new HashMap<>();
83             try {
84                 YangUtils.copyYangKeyValueListToMap(externalIdsMap, queueEntry.getQueuesExternalIds(),
85                         QueuesExternalIds::getQueuesExternalIdKey, QueuesExternalIds::getQueuesExternalIdValue);
86             } catch (NullPointerException e) {
87                 LOG.warn("Incomplete Queue external IDs", e);
88             }
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);
95
96             try {
97                 queue.setOtherConfig(YangUtils.convertYangKeyValueListToMap(queueEntry.getQueuesOtherConfig(),
98                         QueuesOtherConfig::getQueueOtherConfigKey, QueuesOtherConfig::getQueueOtherConfigValue));
99             } catch (NullPointerException e) {
100                 LOG.warn("Incomplete Queue other_config", e);
101             }
102
103             OvsdbNodeAugmentation operNode =
104                 state.getBridgeNode(iid).orElseThrow().augmentation(OvsdbNodeAugmentation.class);
105             Uuid operQueueUuid = getQueueEntryUuid(operNode.getQueues(), queueEntry.key());
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);
112             } else {
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 : {}",
119                         queue, operNode);
120             }
121         }
122     }
123
124     private static Uuid getQueueEntryUuid(final Map<QueuesKey, Queues> operQueues, final QueuesKey queueId) {
125         if (operQueues != null) {
126             Queues queueEntry = operQueues.get(queueId);
127             if (queueEntry != null) {
128                 return queueEntry.getQueueUuid();
129             }
130         }
131         return null;
132     }
133 }