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