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