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