Migrate users of Optional.get()
[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.Map;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
16 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
17 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
18 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class QueueRemovedCommand implements TransactCommand {
29     private static final Logger LOG = LoggerFactory.getLogger(QueueRemovedCommand.class);
30
31     @Override
32     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
33             final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
34         execute(transaction, state, TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
35                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
36     }
37
38     @Override
39     public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
40             final Collection<DataTreeModification<Node>> modifications,
41             final InstanceIdentifierCodec instanceIdentifierCodec) {
42         execute(transaction, state, TransactUtils.extractOriginal(modifications, OvsdbNodeAugmentation.class),
43                 TransactUtils.extractUpdated(modifications, OvsdbNodeAugmentation.class));
44     }
45
46     private static void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
47                                 final Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
48                                 final Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
49         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
50                 .entrySet()) {
51             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
52             OvsdbNodeAugmentation original = originalEntry.getValue();
53             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
54
55             if (original != null && update != null) {
56                 Map<QueuesKey, Queues> origQueues = original.getQueues();
57                 Map<QueuesKey, Queues> updatedQueues = update.getQueues();
58                 if (origQueues != null && !origQueues.isEmpty()) {
59                     for (Queues origQueue : origQueues.values()) {
60                         OvsdbNodeAugmentation operNode =
61                                 state.getBridgeNode(ovsdbNodeIid).orElseThrow().augmentation(
62                                         OvsdbNodeAugmentation.class);
63                         if (updatedQueues == null || !updatedQueues.containsKey(origQueue.key())) {
64                             LOG.debug("Received request to delete Queue entry {}", origQueue.getQueueId());
65                             Uuid queueUuid = getQueueUuid(operNode.getQueues(), origQueue.key());
66                             if (queueUuid != null) {
67                                 Queue queue = transaction.getTypedRowSchema(Queue.class);
68                                 transaction.add(op.delete(queue.getSchema())
69                                         .where(queue.getUuidColumn().getSchema().opEqual(
70                                                 new UUID(queueUuid.getValue())))
71                                         .build());
72                                 LOG.info("Deleted queue Uuid : {}  for the  Ovsdb Node  : {}", queueUuid, operNode);
73                             } else {
74                                 LOG.warn("Unable to delete Queue{} for node {} because it was not found in the "
75                                         + "operational store, and thus we cannot retrieve its UUID",
76                                         ovsdbNodeIid, origQueue.getQueueId());
77                             }
78                         }
79                     }
80                 }
81             }
82         }
83     }
84
85     private static Uuid getQueueUuid(final Map<QueuesKey, Queues> operQueues, final QueuesKey queueId) {
86         if (operQueues != null) {
87             Queues queueEntry = operQueues.get(queueId);
88             if (queueEntry != null) {
89                 return queueEntry.getQueueUuid();
90             }
91         }
92         return null;
93     }
94 }