effb4e06b279a7e67b5fa8bc6adc1a51660bf5ec
[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                     } else if (queue.getQueueUuid() != null) {
89                         newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
90                     }
91                 }
92             }
93             qos.setQueues(newQueueList);
94
95             Map<String, String> externalIdsMap = new HashMap<>();
96             try {
97                 YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
98                         QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
99             } catch (NullPointerException e) {
100                 LOG.warn("Incomplete Qos external IDs", e);
101             }
102             externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
103
104                     instanceIdentifierCodec.serialize(
105                     SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class).getNodeId())
106                     .augmentation(OvsdbNodeAugmentation.class)
107                     .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
108             qos.setExternalIds(externalIdsMap);
109
110             try {
111                 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
112                         QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
113             } catch (NullPointerException e) {
114                 LOG.warn("Incomplete Qos other_config", e);
115             }
116
117             Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.getQosId());
118             if (operQosUuid == null) {
119                 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
120                         + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes()));
121                 transaction.add(op.insert(qos).withId(namedUuid.toString())).build();
122                 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
123             } else {
124                 UUID uuid = new UUID(operQosUuid.getValue());
125                 Qos extraQos = TyperUtils.getTypedRowWrapper(
126                         transaction.getDatabaseSchema(), Qos.class, null);
127                 extraQos.getUuidColumn().setData(uuid);
128                 transaction.add(op.update(qos)
129                         .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
130                 LOG.info("Updated  QoS Uuid : {} for node : {} ", operQosUuid, operNode.getConnectionInfo());
131             }
132             transaction.build();
133         }
134     }
135
136     private String getQueueUuid(OvsdbQueueRef queueRef, OvsdbNodeAugmentation operNode) {
137         QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
138         if (operNode.getQueues() != null && !operNode.getQueues().isEmpty()) {
139             for (Queues queue : operNode.getQueues()) {
140                 if (queue.getQueueId().equals(queueKey.getQueueId())) {
141                     return queue.getQueueUuid().getValue();
142                 }
143             }
144         }
145         return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
146                 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes());
147     }
148
149     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
150         if (operQosEntries != null && !operQosEntries.isEmpty()) {
151             for (QosEntries qosEntry : operQosEntries) {
152                 if (qosEntry.getQosId().equals(qosId)) {
153                     return qosEntry.getQosUuid();
154                 }
155             }
156         }
157         return null;
158     }
159 }