b3e490622a3a5b6144df8e7364fc2c4683082b8e
[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.Iterator;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.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.schema.openvswitch.OpenVSwitch;
24 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
25 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
26 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Optional;
37
38 public class OvsdbQosRemovedCommand extends AbstractTransactionCommand {
39     private static final Logger LOG = LoggerFactory.getLogger(OvsdbQueueUpdateCommand.class);
40
41     private Map<UUID, Qos> removedQosRows;
42     private Map<UUID, OpenVSwitch> updatedOpenVSwitchRows;
43
44     public OvsdbQosRemovedCommand(OvsdbConnectionInstance key,
45             TableUpdates updates, DatabaseSchema dbSchema) {
46         super(key, updates, dbSchema);
47         updatedOpenVSwitchRows = TyperUtils.extractRowsUpdated(OpenVSwitch.class, getUpdates(), getDbSchema());
48         removedQosRows = TyperUtils.extractRowsRemoved(Qos.class, getUpdates(), getDbSchema());
49     }
50
51     @Override
52     public void execute(ReadWriteTransaction transaction) {
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             if (removedQosRows != null && !removedQosRows.isEmpty()) {
60                 for (UUID qosUuid : removedQosRows.keySet()) {
61                     QosEntriesKey qosKey = getQosEntriesKey(ovsdbNode.get(), qosUuid);
62                     if (qosKey != null) {
63                         InstanceIdentifier<QosEntries> iid = ovsdbNodeIid
64                             .augmentation(OvsdbNodeAugmentation.class)
65                             .child(QosEntries.class, qosKey);
66                         result.add(iid);
67                     }
68                 }
69             }
70             deleteQos(transaction, result);
71         }
72     }
73
74     private QosEntriesKey getQosEntriesKey(Node node, UUID qosUuid) {
75         List<QosEntries> qosList = node.getAugmentation(OvsdbNodeAugmentation.class).getQosEntries();
76         if (qosList == null || qosList.isEmpty()) {
77             LOG.debug("Deleting Qos {}, Ovsdb Node {} does not have a Qos list.", qosUuid.toString(), node);
78             return null;
79         }
80         Iterator<QosEntries> itr = qosList.iterator();
81         Uuid qUuid = new Uuid(qosUuid.toString());
82         while (itr.hasNext()) {
83             QosEntries qos = itr.next();
84             if (qos.getQosUuid().equals(qUuid)) {
85                 return qos.getKey();
86             }
87         }
88         LOG.debug("Deleted Queue {} not found in Ovsdb Node {}", qosUuid.toString(), node);
89         return null;
90     }
91
92     private void deleteQos(ReadWriteTransaction transaction,
93             List<InstanceIdentifier<QosEntries>> qosEntryIids) {
94         for (InstanceIdentifier<QosEntries> qosEntryIid: qosEntryIids) {
95             transaction.delete(LogicalDatastoreType.OPERATIONAL, qosEntryIid);
96         }
97     }
98 }