Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalRouterRemoveCommand.java
1 /*
2  * Copyright © 2017 Hewlett Packard Enterprise, Co. 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 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import com.google.common.base.Optional;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalRouter;
25 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalRouters;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class LogicalRouterRemoveCommand extends AbstractTransactCommand<LogicalRouters, HwvtepGlobalAugmentation> {
34     private static final Logger LOG = LoggerFactory.getLogger(LogicalRouterRemoveCommand.class);
35
36     public LogicalRouterRemoveCommand(final HwvtepOperationalState state,
37             final Collection<DataTreeModification<Node>> changes) {
38         super(state, changes);
39     }
40
41     @Override
42     public void execute(final TransactionBuilder transaction) {
43         Map<InstanceIdentifier<Node>, List<LogicalRouters>> removed =
44               extractRemoved(getChanges(),LogicalRouters.class);
45
46         for (Entry<InstanceIdentifier<Node>, List<LogicalRouters>> created: removed.entrySet()) {
47             if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
48                 getOperationalState().getDeviceInfo().scheduleTransaction(new TransactCommand() {
49                     @Override
50                     public void execute(final TransactionBuilder transactionBuilder) {
51                         HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
52                         HwvtepOperationalState operState = new HwvtepOperationalState(
53                                 connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
54                         hwvtepOperationalState = operState;
55                         deviceTransaction = transactionBuilder;
56                         LOG.debug("Running delete logical router in seperate tx {}", created.getKey());
57                         removeLogicalRouter(transactionBuilder, created.getKey(), created.getValue());
58                     }
59
60
61                     @Override
62                     public void onSuccess(final TransactionBuilder deviceTransaction) {
63                         LogicalRouterRemoveCommand.this.onSuccess(deviceTransaction);
64                     }
65
66                     @Override
67                     public void onFailure(final TransactionBuilder deviceTransaction) {
68                         LogicalRouterRemoveCommand.this.onFailure(deviceTransaction);
69                     }
70                 });
71             }
72         }
73     }
74
75     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
76             justification = "https://github.com/spotbugs/spotbugs/issues/811")
77     private void removeLogicalRouter(final TransactionBuilder transaction,
78             final InstanceIdentifier<Node> instanceIdentifier, final List<LogicalRouters> routerList) {
79         for (LogicalRouters lrouter: routerList) {
80             LOG.debug("Removing logical router named: {}", lrouter.getHwvtepNodeName().getValue());
81             Optional<LogicalRouters> operationalRouterOptional =
82                     getOperationalState().getLogicalRouters(instanceIdentifier, lrouter.key());
83
84             if (operationalRouterOptional.isPresent()
85                     && operationalRouterOptional.get().getLogicalRouterUuid() != null) {
86                 LogicalRouter logicalRouter = transaction.getTypedRowSchema(LogicalRouter.class);
87                 UUID logicalRouterUuid = new UUID(operationalRouterOptional.get().getLogicalRouterUuid().getValue());
88                 transaction.add(op.delete(logicalRouter.getSchema())
89                         .where(logicalRouter.getUuidColumn().getSchema().opEqual(logicalRouterUuid)).build());
90                 transaction.add(op.comment("Logical Router: Deleting " + lrouter.getHwvtepNodeName().getValue()));
91                 updateControllerTxHistory(TransactionType.DELETE, logicalRouter);
92             } else {
93                 LOG.warn("Unable to delete logical router {} because it was not found in the operational data store",
94                         lrouter.getHwvtepNodeName().getValue());
95             }
96         }
97     }
98
99     @Override
100     protected List<LogicalRouters> getData(final HwvtepGlobalAugmentation augmentation) {
101         return augmentation.getLogicalRouters();
102     }
103
104     @Override
105     protected boolean areEqual(final LogicalRouters routers1, final LogicalRouters routers2) {
106         return routers1.key().equals(routers2.key());
107     }
108 }