Use stateless transaction commands
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QosRemovedCommand.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
9 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import java.util.List;
14 import java.util.Map;
15
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class QosRemovedCommand implements TransactCommand {
31     private static final Logger LOG = LoggerFactory.getLogger(QosRemovedCommand.class);
32
33     @Override
34     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
35                         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
36         execute(transaction, state,
37                 TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
38                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
39     }
40
41     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
42                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
43                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
44         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
45                 .entrySet()) {
46             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
47             OvsdbNodeAugmentation original = originalEntry.getValue();
48             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
49
50             if (original != null && update != null) {
51                 List<QosEntries> origQosEntries = original.getQosEntries();
52                 List<QosEntries> updatedQosEntries = update.getQosEntries();
53                 if (origQosEntries != null && !origQosEntries.isEmpty()) {
54                     for (QosEntries origQosEntry : origQosEntries) {
55                         OvsdbNodeAugmentation operNode =
56                                 state.getBridgeNode(ovsdbNodeIid).get().getAugmentation(OvsdbNodeAugmentation.class);
57                         List<QosEntries> operQosEntries = operNode.getQosEntries();
58
59                         boolean found = false;
60                         if (updatedQosEntries != null && !updatedQosEntries.isEmpty()) {
61                             for (QosEntries updatedQosEntry : updatedQosEntries) {
62                                 if (origQosEntry.getQosId().equals(updatedQosEntry.getQosId())) {
63                                     found = true;
64                                     break;
65                                 }
66                             }
67                         }
68                         if (!found) {
69                             LOG.debug("Received request to delete QoS entry {}", origQosEntry.getQosId());
70                             Uuid qosUuid = getQosEntryUuid(operQosEntries, origQosEntry.getQosId());
71                             if (qosUuid != null) {
72                                 Qos qos =
73                                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class, null);
74                                 transaction.add(op.delete(qos.getSchema())
75                                         .where(qos.getUuidColumn().getSchema().opEqual(new UUID(qosUuid.getValue())))
76                                         .build());
77                             } else {
78                                 LOG.warn(
79                                         "Unable to delete QoS{} for node {} because it was not found in the " +
80                                                 "operational store, "
81                                                 + "and thus we cannot retrieve its UUID", origQosEntry.getQosId(),
82                                         ovsdbNodeIid);
83                             }
84                         }
85                     }
86                 }
87             }
88         }
89     }
90
91     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
92         if (operQosEntries != null && !operQosEntries.isEmpty()) {
93             for (QosEntries qosEntry : operQosEntries) {
94                 if (qosEntry.getQosId().equals(qosId)) {
95                     return qosEntry.getQosUuid();
96                 }
97             }
98         }
99         return null;
100     }
101
102 }