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