Update MRI projects for Aluminium
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QosUpdateCommand.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.Map;
16 import java.util.Map.Entry;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
21 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
22 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
23 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
24 import org.opendaylight.ovsdb.utils.yang.YangUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQueueRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
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.qos.entries.QosExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListKey;
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;
40
41 public class QosUpdateCommand implements TransactCommand {
42     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
43
44     @Override
45     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
46             final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
47         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, QosEntries.class),
48                 instanceIdentifierCodec);
49     }
50
51     @Override
52     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
53             final Collection<DataTreeModification<Node>> modifications,
54             final InstanceIdentifierCodec instanceIdentifierCodec) {
55         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, QosEntries.class),
56                 instanceIdentifierCodec);
57     }
58
59     private void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
60             final Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated,
61             final InstanceIdentifierCodec instanceIdentifierCodec) {
62         for (Entry<InstanceIdentifier<QosEntries>, QosEntries> qosMapEntry: createdOrUpdated.entrySet()) {
63             InstanceIdentifier<OvsdbNodeAugmentation> iid =
64                     qosMapEntry.getKey().firstIdentifierOf(OvsdbNodeAugmentation.class);
65             if (!state.getBridgeNode(iid).isPresent()) {
66                 return;
67             }
68             OvsdbNodeAugmentation operNode =
69                 state.getBridgeNode(iid).get().augmentation(OvsdbNodeAugmentation.class);
70
71             QosEntries qosEntry = qosMapEntry.getValue();
72             Qos qos = transaction.getTypedRowWrapper(Qos.class);
73
74             if (qosEntry.getQosType() != null) {
75                 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
76             }
77
78             Map<QueueListKey, QueueList> queueList = qosEntry.getQueueList();
79             Map<Long, UUID> newQueueList = new HashMap<>();
80             if (queueList != null && !queueList.isEmpty()) {
81                 for (QueueList queue : queueList.values()) {
82                     if (queue.getQueueRef() != null) {
83                         newQueueList.put(queue.getQueueNumber().toJava(),
84                                 new UUID(getQueueUuid(queue.getQueueRef(), operNode)));
85                     }
86                 }
87             }
88             qos.setQueues(newQueueList);
89
90             Map<String, String> externalIdsMap = new HashMap<>();
91             try {
92                 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
93                         QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
94             } catch (NullPointerException e) {
95                 LOG.warn("Incomplete Qos external IDs", e);
96             }
97             externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
98
99                     instanceIdentifierCodec.serialize(
100                     SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
101                     .augmentation(OvsdbNodeAugmentation.class)
102                     .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
103             qos.setExternalIds(externalIdsMap);
104
105             try {
106                 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
107                         QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
108             } catch (NullPointerException e) {
109                 LOG.warn("Incomplete Qos other_config", e);
110             }
111
112             Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.key());
113             if (operQosUuid == null) {
114                 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
115                         + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes(UTF_8)));
116                 transaction.add(op.insert(qos).withId(namedUuid.toString()));
117                 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
118             } else {
119                 UUID uuid = new UUID(operQosUuid.getValue());
120                 Qos extraQos = transaction.getTypedRowSchema(Qos.class);
121                 extraQos.getUuidColumn().setData(uuid);
122                 transaction.add(op.update(qos)
123                         .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
124                 LOG.info("Updated  QoS Uuid : {} for node : {} ", operQosUuid, operNode.getConnectionInfo());
125             }
126         }
127     }
128
129     private static String getQueueUuid(final OvsdbQueueRef queueRef, final OvsdbNodeAugmentation operNode) {
130         QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
131         Map<QueuesKey, Queues> queues = operNode.getQueues();
132         if (queues != null) {
133             Queues queue = queues.get(queueKey);
134             if (queue != null) {
135                 return queue.getQueueUuid().getValue();
136             }
137         }
138         return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
139                 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes(UTF_8));
140     }
141
142     private static Uuid getQosEntryUuid(final Map<QosEntriesKey, QosEntries> operQosEntries,
143             final QosEntriesKey qosId) {
144         if (operQosEntries != null) {
145             QosEntries qosEntry = operQosEntries.get(qosId);
146             if (qosEntry != null) {
147                 return qosEntry.getQosUuid();
148             }
149         }
150         return null;
151     }
152 }