3246ea49942bf144b9b544bf2f01271b1c839b4e
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QosRemovedCommand.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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.Qos;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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.QosEntries;
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 QosRemovedCommand implements TransactCommand {
34     private static final Logger LOG = LoggerFactory.getLogger(QosRemovedCommand.class);
35
36     @Override
37     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
38                         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
39         execute(transaction, state,
40                 TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
41                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
42     }
43
44     @Override
45     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
46                         Collection<DataTreeModification<Node>> modifications) {
47         execute(transaction, state,
48                 TransactUtils.extractOriginal(modifications, OvsdbNodeAugmentation.class),
49                 TransactUtils.extractUpdated(modifications, OvsdbNodeAugmentation.class));
50     }
51
52     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
53                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
54                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
55         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
56                 .entrySet()) {
57             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
58             OvsdbNodeAugmentation original = originalEntry.getValue();
59             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
60
61             if (original != null && update != null) {
62                 List<QosEntries> origQosEntries = original.getQosEntries();
63                 List<QosEntries> updatedQosEntries = update.getQosEntries();
64                 if (origQosEntries != null && !origQosEntries.isEmpty()) {
65                     for (QosEntries origQosEntry : origQosEntries) {
66                         OvsdbNodeAugmentation operNode =
67                                 state.getBridgeNode(ovsdbNodeIid).get().getAugmentation(OvsdbNodeAugmentation.class);
68                         List<QosEntries> operQosEntries = operNode.getQosEntries();
69
70                         boolean found = false;
71                         if (updatedQosEntries != null && !updatedQosEntries.isEmpty()) {
72                             for (QosEntries updatedQosEntry : updatedQosEntries) {
73                                 if (origQosEntry.getQosId().equals(updatedQosEntry.getQosId())) {
74                                     found = true;
75                                     break;
76                                 }
77                             }
78                         }
79                         if (!found) {
80                             LOG.debug("Received request to delete QoS entry {}", origQosEntry.getQosId());
81                             Uuid qosUuid = getQosEntryUuid(operQosEntries, origQosEntry.getQosId());
82                             if (qosUuid != null) {
83                                 Qos qos =
84                                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class, null);
85                                 transaction.add(op.delete(qos.getSchema())
86                                         .where(qos.getUuidColumn().getSchema().opEqual(new UUID(qosUuid.getValue())))
87                                         .build());
88                                 LOG.info("Removed QoS Uuid : {} for node : {} ",
89                                         origQosEntry.getQosId(), ovsdbNodeIid);
90                             } else {
91                                 LOG.warn("Unable to delete QoS{} for node {} because it was not found in the "
92                                         + "operational store, and thus we cannot retrieve its UUID",
93                                         origQosEntry.getQosId(), ovsdbNodeIid);
94                             }
95                         }
96                     }
97                 }
98             }
99         }
100     }
101
102     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
103         if (operQosEntries != null && !operQosEntries.isEmpty()) {
104             for (QosEntries qosEntry : operQosEntries) {
105                 if (qosEntry.getQosId().equals(qosId)) {
106                     return qosEntry.getQosUuid();
107                 }
108             }
109         }
110         return null;
111     }
112
113 }