63d38df496e5f586dbadf714aaa8c30b7d8f8228
[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 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
21 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class QosRemovedCommand implements TransactCommand {
32     private static final Logger LOG = LoggerFactory.getLogger(QosRemovedCommand.class);
33
34     @Override
35     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
36             DataChangeEvent events, InstanceIdentifierCodec instanceIdentifierCodec) {
37         execute(transaction, state,
38                 TransactUtils.extractOriginal(events, OvsdbNodeAugmentation.class),
39                 TransactUtils.extractUpdated(events, OvsdbNodeAugmentation.class));
40     }
41
42     @Override
43     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
44             Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
45         execute(transaction, state,
46                 TransactUtils.extractOriginal(modifications, OvsdbNodeAugmentation.class),
47                 TransactUtils.extractUpdated(modifications, OvsdbNodeAugmentation.class));
48     }
49
50     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
51                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originals,
52                         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated) {
53         for (Map.Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> originalEntry : originals
54                 .entrySet()) {
55             InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeIid = originalEntry.getKey();
56             OvsdbNodeAugmentation original = originalEntry.getValue();
57             OvsdbNodeAugmentation update = updated.get(ovsdbNodeIid);
58
59             if (original != null && update != null) {
60                 List<QosEntries> origQosEntries = original.getQosEntries();
61                 List<QosEntries> updatedQosEntries = update.getQosEntries();
62                 if (origQosEntries != null && !origQosEntries.isEmpty()) {
63                     for (QosEntries origQosEntry : origQosEntries) {
64                         OvsdbNodeAugmentation operNode =
65                                 state.getBridgeNode(ovsdbNodeIid).get().augmentation(OvsdbNodeAugmentation.class);
66                         List<QosEntries> operQosEntries = operNode.getQosEntries();
67
68                         boolean found = false;
69                         if (updatedQosEntries != null && !updatedQosEntries.isEmpty()) {
70                             for (QosEntries updatedQosEntry : updatedQosEntries) {
71                                 if (origQosEntry.getQosId().equals(updatedQosEntry.getQosId())) {
72                                     found = true;
73                                     break;
74                                 }
75                             }
76                         }
77                         if (!found) {
78                             LOG.debug("Received request to delete QoS entry {}", origQosEntry.getQosId());
79                             Uuid qosUuid = getQosEntryUuid(operQosEntries, origQosEntry.getQosId());
80                             if (qosUuid != null) {
81                                 Qos qos =
82                                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Qos.class, null);
83                                 transaction.add(op.delete(qos.getSchema())
84                                         .where(qos.getUuidColumn().getSchema().opEqual(new UUID(qosUuid.getValue())))
85                                         .build());
86                                 LOG.info("Removed QoS Uuid : {} for node : {} ",
87                                         origQosEntry.getQosId(), ovsdbNodeIid);
88                             } else {
89                                 LOG.warn("Unable to delete QoS{} for node {} because it was not found in the "
90                                         + "operational store, and thus we cannot retrieve its UUID",
91                                         origQosEntry.getQosId(), ovsdbNodeIid);
92                             }
93                         }
94                     }
95                 }
96             }
97         }
98     }
99
100     private Uuid getQosEntryUuid(List<QosEntries> operQosEntries, Uri qosId) {
101         if (operQosEntries != null && !operQosEntries.isEmpty()) {
102             for (QosEntries qosEntry : operQosEntries) {
103                 if (qosEntry.getQosId().equals(qosId)) {
104                     return qosEntry.getQosUuid();
105                 }
106             }
107         }
108         return null;
109     }
110
111 }