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