Make TransactionBuilder type-aware
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QueueRemovedCommand.java
1 /*
2  * Copyright (c) 2016 Intel  Inc. 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.Queue;
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.Queues;
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 QueueRemovedCommand implements TransactCommand {
30     private static final Logger LOG = LoggerFactory.getLogger(QueueRemovedCommand.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, TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
36                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
37     }
38
39     @Override
40     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
41             final Collection<DataTreeModification<Node>> modifications,
42             final InstanceIdentifierCodec instanceIdentifierCodec) {
43         execute(transaction, state, TransactUtils.extractOriginal(modifications, OvsdbNodeAugmentation.class),
44                 TransactUtils.extractUpdated(modifications, OvsdbNodeAugmentation.class));
45     }
46
47     private void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
48                          final Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
49                          final Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
50         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
51                 .entrySet()) {
52             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
53             OvsdbNodeAugmentation original = originalEntry.getValue();
54             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
55
56             if (original != null && update != null) {
57                 List<Queues> origQueues = original.getQueues();
58                 List<Queues> updatedQueues = update.getQueues();
59                 if (origQueues != null && !origQueues.isEmpty()) {
60                     for (Queues origQueue : origQueues) {
61                         OvsdbNodeAugmentation operNode =
62                                 state.getBridgeNode(ovsdbNodeIid).get().augmentation(
63                                         OvsdbNodeAugmentation.class);
64                         List<Queues> operQueues = operNode.getQueues();
65
66                         boolean found = false;
67                         if (updatedQueues != null && !updatedQueues.isEmpty()) {
68                             for (Queues updatedQueue : updatedQueues) {
69                                 if (origQueue.getQueueId().equals(updatedQueue.getQueueId())) {
70                                     found = true;
71                                     break;
72                                 }
73                             }
74                         }
75                         if (!found) {
76                             LOG.debug("Received request to delete Queue entry {}", origQueue.getQueueId());
77                             Uuid queueUuid = getQueueUuid(operQueues, origQueue.getQueueId());
78                             if (queueUuid != null) {
79                                 Queue queue = transaction.getTypedRowSchema(Queue.class);
80                                 transaction.add(op.delete(queue.getSchema())
81                                         .where(queue.getUuidColumn().getSchema().opEqual(
82                                                 new UUID(queueUuid.getValue())))
83                                         .build());
84                                 LOG.info("Deleted queue Uuid : {}  for the  Ovsdb Node  : {}", queueUuid, operNode);
85                             } else {
86                                 LOG.warn("Unable to delete Queue{} for node {} because it was not found in the "
87                                         + "operational store, and thus we cannot retrieve its UUID",
88                                         ovsdbNodeIid, origQueue.getQueueId());
89                             }
90                         }
91                     }
92                 }
93             }
94         }
95     }
96
97     private Uuid getQueueUuid(final List<Queues> operQueues, final Uri queueId) {
98         if (operQueues != null && !operQueues.isEmpty()) {
99             for (Queues queueEntry : operQueues) {
100                 if (queueEntry.getQueueId().equals(queueId)) {
101                     return queueEntry.getQueueUuid();
102                 }
103             }
104         }
105         return null;
106     }
107
108 }