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