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