Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class QosUpdateCommand implements TransactCommand {
46     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
47
48     @Override
49     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
50                         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
51         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, QosEntries.class));
52     }
53
54     @Override
55     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
56                         Collection<DataTreeModification<Node>> modifications) {
57         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, QosEntries.class));
58     }
59
60     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
61                          Map<InstanceIdentifier<QosEntries>, QosEntries> createdOrUpdated) {
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().getAugmentation(OvsdbNodeAugmentation.class);
70
71             QosEntries qosEntry = qosMapEntry.getValue();
72             Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
73
74             if (qosEntry.getQosType() != null) {
75                 qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
76             }
77
78             List<QueueList> queueList = qosEntry.getQueueList();
79             Map<Long, UUID> newQueueList = new HashMap<>();
80             if (queueList != null && !queueList.isEmpty()) {
81                 for (QueueList queue : queueList) {
82                     if (queue.getQueueRef() != null) {
83                         newQueueList.put(queue.getQueueNumber(),
84                                 new UUID(getQueueUuid(queue.getQueueRef(), operNode)));
85                     } else if (queue.getQueueUuid() != null) {
86                         newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
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                     SouthboundUtil.serializeInstanceIdentifier(
101                     SouthboundMapper.createInstanceIdentifier(iid.firstKeyOf(Node.class, NodeKey.class).getNodeId())
102                     .augmentation(OvsdbNodeAugmentation.class)
103                     .child(QosEntries.class, new QosEntriesKey(qosEntry.getQosId()))));
104             qos.setExternalIds(externalIdsMap);
105
106             try {
107                 qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
108                         QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
109             } catch (NullPointerException e) {
110                 LOG.warn("Incomplete Qos other_config", e);
111             }
112
113             Uuid operQosUuid = getQosEntryUuid(operNode.getQosEntries(), qosEntry.getQosId());
114             if (operQosUuid == null) {
115                 UUID namedUuid = new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
116                         + TransactUtils.bytesToHexString(qosEntry.getQosId().getValue().getBytes()));
117                 transaction.add(op.insert(qos).withId(namedUuid.toString())).build();
118                 LOG.info("Added QoS Uuid: {} for node : {} ", namedUuid, operNode.getConnectionInfo());
119             } else {
120                 UUID uuid = new UUID(operQosUuid.getValue());
121                 Qos extraQos = TyperUtils.getTypedRowWrapper(
122                         transaction.getDatabaseSchema(), Qos.class, null);
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             transaction.build();
129         }
130     }
131
132     private String getQueueUuid(OvsdbQueueRef queueRef, OvsdbNodeAugmentation operNode) {
133         QueuesKey queueKey = queueRef.getValue().firstKeyOf(Queues.class);
134         if (operNode.getQueues() != null && !operNode.getQueues().isEmpty()) {
135             for (Queues queue : operNode.getQueues()) {
136                 if (queue.getQueueId().equals(queueKey.getQueueId())) {
137                     return queue.getQueueUuid().getValue();
138                 }
139             }
140         }
141         return SouthboundConstants.QUEUE_NAMED_UUID_PREFIX
142                 + TransactUtils.bytesToHexString(queueKey.getQueueId().getValue().getBytes());
143     }
144
145     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
146         if (operQosEntries != null && !operQosEntries.isEmpty()) {
147             for (QosEntries qosEntry : operQosEntries) {
148                 if (qosEntry.getQosId().equals(qosId)) {
149                     return qosEntry.getQosUuid();
150                 }
151             }
152         }
153         return null;
154     }
155 }