Migrate users of Optional.get()
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbQosRemovedCommand.java
1 /*
2  * Copyright (c) 2016 Intel Communications Systems, 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.transactions.md;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
23 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class OvsdbQosRemovedCommand extends AbstractTransactionCommand {
36     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQosRemovedCommand.class);
37
38     private final Map<UUID, Qos> removedQosRows;
39
40     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
41     public OvsdbQosRemovedCommand(OvsdbConnectionInstance key,
42             TableUpdates updates, DatabaseSchema dbSchema) {
43         super(key, updates, dbSchema);
44         removedQosRows = TyperUtils.extractRowsRemoved(Qos.class, getUpdates(), getDbSchema());
45     }
46
47     @Override
48     public void execute(ReadWriteTransaction transaction) {
49         if (removedQosRows == null || removedQosRows.isEmpty()) {
50             return;
51         }
52
53         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
54         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
55         if (ovsdbNode.isPresent()) {
56             List<InstanceIdentifier<QosEntries>> result = new ArrayList<>();
57             InstanceIdentifier<Node> ovsdbNodeIid =
58                     SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance().getNodeId());
59             for (UUID qosUuid : removedQosRows.keySet()) {
60                 QosEntriesKey qosKey = getQosEntriesKey(ovsdbNode.orElseThrow(), qosUuid);
61                 if (qosKey != null) {
62                     InstanceIdentifier<QosEntries> iid = ovsdbNodeIid
63                         .augmentation(OvsdbNodeAugmentation.class)
64                         .child(QosEntries.class, qosKey);
65                     result.add(iid);
66                 }
67             }
68             deleteQos(transaction, result);
69         }
70     }
71
72     private static QosEntriesKey getQosEntriesKey(Node node, UUID qosUuid) {
73         Map<QosEntriesKey, QosEntries> qosList = node.augmentation(OvsdbNodeAugmentation.class).getQosEntries();
74         if (qosList == null || qosList.isEmpty()) {
75             LOG.debug("Deleting Qos {}, Ovsdb Node {} does not have a Qos list.", qosUuid, node);
76             return null;
77         }
78         Uuid quUuid = new Uuid(qosUuid.toString());
79         for (QosEntries qos : qosList.values()) {
80             if (qos.getQosUuid().equals(quUuid)) {
81                 return qos.key();
82             }
83         }
84         LOG.debug("Deleted Queue {} not found in Ovsdb Node {}", qosUuid, node);
85         return null;
86     }
87
88     private static void deleteQos(ReadWriteTransaction transaction,
89             List<InstanceIdentifier<QosEntries>> qosEntryIids) {
90         for (InstanceIdentifier<QosEntries> qosEntryIid: qosEntryIids) {
91             transaction.delete(LogicalDatastoreType.OPERATIONAL, qosEntryIid);
92         }
93     }
94 }