ovsdb enable checkstyle on error
[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.rev100924.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, OvsdbNodeAugmentation.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, OvsdbNodeAugmentation.class));
58     }
59
60     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
61                          Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> createdOrUpdated) {
62         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
63             createdOrUpdated.entrySet()) {
64             updateQos(transaction, state, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
65         }
66     }
67
68     private void updateQos(
69             TransactionBuilder transaction, BridgeOperationalState state,
70             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
71
72         List<QosEntries> qosEntries = ovsdbNode.getQosEntries();
73
74         if (!state.getBridgeNode(iid).isPresent()) {
75             return;
76         }
77         OvsdbNodeAugmentation operNode = state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
78         List<QosEntries> operQosEntries = operNode.getQosEntries();
79         List<Queues> operQueues = operNode.getQueues();
80
81         if (qosEntries != null) {
82             for (QosEntries qosEntry : qosEntries) {
83                 Qos qos = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class);
84
85                 if (qosEntry.getQosType() != null) {
86                     qos.setType(SouthboundMapper.createQosType(qosEntry.getQosType()));
87                 }
88
89                 List<QueueList> queueList = qosEntry.getQueueList();
90                 Map<Long, UUID> newQueueList = new HashMap<>();
91                 if (queueList != null && !queueList.isEmpty()) {
92                     for (QueueList queue : queueList) {
93                         if (queue.getQueueRef() != null) {
94                             newQueueList.put(queue.getQueueNumber(),
95                                     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 }