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