Merge "BUG-5006: rework SouthboundProviderTest, clean up"
[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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.ovsdb.lib.notation.Condition;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.Mutate;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Port;
27 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
28 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
29 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
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.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.collect.ImmutableMap;
44
45 public class QosUpdateCommand extends AbstractTransactCommand {
46     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
47
48
49     public QosUpdateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
50         super(state, changes);
51     }
52
53     @Override
54     public void execute(TransactionBuilder transaction) {
55         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> created =
56                 TransactUtils.extractCreated(getChanges(),OvsdbNodeAugmentation.class);
57         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
58             created.entrySet()) {
59             updateQos(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
60         }
61         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
62                 TransactUtils.extractUpdated(getChanges(),OvsdbNodeAugmentation.class);
63         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
64             updated.entrySet()) {
65             updateQos(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
66         }
67     }
68
69     private void updateQos(
70             TransactionBuilder transaction,
71             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
72
73         List<QosEntries> qosEntries = ovsdbNode.getQosEntries();
74
75         if (!getOperationalState().getBridgeNode(iid).isPresent()) {
76             return;
77         }
78         OvsdbNodeAugmentation operNode = getOperationalState().getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
79         List<QosEntries> operQosEntries = operNode.getQosEntries();
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                 Uuid qosUuid = getQosEntryUuid(operQosEntries, qosEntry.getQosId());
90                 UUID uuid = null;
91                 if (qosUuid != null) {
92                     uuid = new UUID(qosUuid.getValue());
93                 }
94
95                 List<QueueList> queueList = qosEntry.getQueueList();
96                 Map<Long, UUID>newQueueList = new HashMap<>();
97                 if (queueList != null && !queueList.isEmpty()) {
98                     for (QueueList queue : queueList) {
99                         newQueueList.put(queue.getQueueNumber(), new UUID(queue.getQueueUuid().getValue()));
100                     }
101                 }
102                 qos.setQueues(newQueueList);
103
104                 List<QosExternalIds> externalIds = qosEntry.getQosExternalIds();
105                 Map<String, String> externalIdsMap = new HashMap<>();
106                 if (externalIds != null) {
107                     for (QosExternalIds externalId : externalIds) {
108                         externalIdsMap.put(externalId.getQosExternalIdKey(), externalId.getQosExternalIdValue());
109                     }
110                 }
111                 externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
112                 try {
113                     qos.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
114                 } catch (NullPointerException e) {
115                     LOG.warn("Incomplete Qos external IDs", e);
116                 }
117
118                 List<QosOtherConfig> otherConfigs = qosEntry.getQosOtherConfig();
119                 if (otherConfigs != null) {
120                     Map<String, String> otherConfigsMap = new HashMap<>();
121                     for (QosOtherConfig otherConfig : otherConfigs) {
122                         otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
123                     }
124                     try {
125                         qos.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
126                     } catch (NullPointerException e) {
127                         LOG.warn("Incomplete Qos other_config", e);
128                     }
129                 }
130                 if (uuid == null) {
131                     transaction.add(op.insert(qos)).build();
132                 } else {
133                     Qos extraQos = TyperUtils.getTypedRowWrapper(
134                             transaction.getDatabaseSchema(), Qos.class, null);
135                     extraQos.getUuidColumn().setData(uuid);
136                     transaction.add(op.update(qos)
137                             .where(extraQos.getUuidColumn().getSchema().opEqual(uuid)).build());
138                 }
139                 transaction.build();
140             }
141         }
142     }
143
144     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
145         if (operQosEntries != null && !operQosEntries.isEmpty()) {
146             for (QosEntries qosEntry : operQosEntries) {
147                 if (qosEntry.getQosId().equals(qosId)) {
148                     return qosEntry.getQosUuid();
149                 }
150             }
151         }
152         return null;
153     }
154 }