002197896a1b12bf78310e282d482f5920519099
[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
9 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.Queue;
21 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class QueueRemovedCommand implements TransactCommand {
32     private static final Logger LOG = LoggerFactory.getLogger(QueueRemovedCommand.class);
33
34     @Override
35     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
36             DataChangeEvent events, InstanceIdentifierCodec instanceIdentifierCodec) {
37         execute(transaction, state, TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
38                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
39     }
40
41     @Override
42     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
43             Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
44         execute(transaction, state, TransactUtils.extractOriginal(modifications, OvsdbNodeAugmentation.class),
45                 TransactUtils.extractUpdated(modifications, OvsdbNodeAugmentation.class));
46     }
47
48     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
49                          Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
50                          Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
51         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
52                 .entrySet()) {
53             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
54             OvsdbNodeAugmentation original = originalEntry.getValue();
55             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
56
57             if (original != null && update != null) {
58                 List<Queues> origQueues = original.getQueues();
59                 List<Queues> updatedQueues = update.getQueues();
60                 if (origQueues != null && !origQueues.isEmpty()) {
61                     for (Queues origQueue : origQueues) {
62                         OvsdbNodeAugmentation operNode =
63                                 state.getBridgeNode(ovsdbNodeIid).get().augmentation(
64                                         OvsdbNodeAugmentation.class);
65                         List<Queues> operQueues = operNode.getQueues();
66
67                         boolean found = false;
68                         if (updatedQueues != null && !updatedQueues.isEmpty()) {
69                             for (Queues updatedQueue : updatedQueues) {
70                                 if (origQueue.getQueueId().equals(updatedQueue.getQueueId())) {
71                                     found = true;
72                                     break;
73                                 }
74                             }
75                         }
76                         if (!found) {
77                             LOG.debug("Received request to delete Queue entry {}", origQueue.getQueueId());
78                             Uuid queueUuid = getQueueUuid(operQueues, origQueue.getQueueId());
79                             if (queueUuid != null) {
80                                 Queue queue =
81                                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Queue.class,
82                                                 null);
83                                 transaction.add(op.delete(queue.getSchema())
84                                         .where(queue.getUuidColumn().getSchema().opEqual(
85                                                 new UUID(queueUuid.getValue())))
86                                         .build());
87                                 LOG.info("Deleted queue Uuid : {}  for the  Ovsdb Node  : {}", queueUuid, operNode);
88                             } else {
89                                 LOG.warn("Unable to delete Queue{} for node {} because it was not found in the "
90                                         + "operational store, and thus we cannot retrieve its UUID",
91                                         ovsdbNodeIid, origQueue.getQueueId());
92                             }
93                         }
94                     }
95                 }
96             }
97         }
98     }
99
100     private Uuid getQueueUuid(List<Queues> operQueues, Uri queueId) {
101         if (operQueues != null && !operQueues.isEmpty()) {
102             for (Queues queueEntry : operQueues) {
103                 if (queueEntry.getQueueId().equals(queueId)) {
104                     return queueEntry.getQueueUuid();
105                 }
106             }
107         }
108         return null;
109     }
110
111 }