Fix findbugs violations in hwvtepsouthbound-impl
[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 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.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalRouter;
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(HwvtepOperationalState state,
37             Collection<DataTreeModification<Node>> changes) {
38         super(state, changes);
39     }
40
41     @Override
42     public void execute(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(TransactionBuilder transactionBuilder) {
51                         HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
52                         HwvtepOperationalState operState = new HwvtepOperationalState(
53                                 connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
54                         threadLocalOperationalState.set(operState);
55                         threadLocalDeviceTransaction.set(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(TransactionBuilder deviceTransaction) {
63                         LogicalRouterRemoveCommand.this.onSuccess(deviceTransaction);
64                     }
65
66                     @Override
67                     public void onFailure(TransactionBuilder deviceTransaction) {
68                         LogicalRouterRemoveCommand.this.onFailure(deviceTransaction);
69                     }
70                 });
71             }
72         }
73     }
74
75     private void removeLogicalRouter(TransactionBuilder transaction,
76             final InstanceIdentifier<Node> instanceIdentifier, final List<LogicalRouters> routerList) {
77         for (LogicalRouters lrouter: routerList) {
78             LOG.debug("Removing logical router named: {}", lrouter.getHwvtepNodeName().getValue());
79             Optional<LogicalRouters> operationalRouterOptional =
80                     getOperationalState().getLogicalRouters(instanceIdentifier, lrouter.getKey());
81
82             if (operationalRouterOptional.isPresent()
83                     && operationalRouterOptional.get().getLogicalRouterUuid() != null) {
84                 LogicalRouter logicalRouter = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
85                         LogicalRouter.class, null);
86                 UUID logicalRouterUuid = new UUID(operationalRouterOptional.get().getLogicalRouterUuid().getValue());
87                 transaction.add(op.delete(logicalRouter.getSchema())
88                         .where(logicalRouter.getUuidColumn().getSchema().opEqual(logicalRouterUuid)).build());
89                 transaction.add(op.comment("Logical Router: Deleting " + lrouter.getHwvtepNodeName().getValue()));
90             } else {
91                 LOG.warn("Unable to delete logical router {} because it was not found in the operational data store",
92                         lrouter.getHwvtepNodeName().getValue());
93             }
94         }
95     }
96
97     @Override
98     protected List<LogicalRouters> getData(final HwvtepGlobalAugmentation augmentation) {
99         return augmentation.getLogicalRouters();
100     }
101
102     @Override
103     protected boolean areEqual(final LogicalRouters routers1, final LogicalRouters routers2) {
104         return routers1.getKey().equals(routers2.getKey());
105     }
106 }