Update MRI projects for Aluminium
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Optional;
19 import org.opendaylight.mdsal.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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalRoutersKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class LogicalRouterRemoveCommand
35         extends AbstractTransactCommand<LogicalRouters, LogicalRoutersKey, HwvtepGlobalAugmentation> {
36     private static final Logger LOG = LoggerFactory.getLogger(LogicalRouterRemoveCommand.class);
37
38     public LogicalRouterRemoveCommand(final HwvtepOperationalState state,
39             final Collection<DataTreeModification<Node>> changes) {
40         super(state, changes);
41     }
42
43     @Override
44     public void execute(final 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(final 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(final TransactionBuilder deviceTransaction) {
65                         LogicalRouterRemoveCommand.this.onSuccess(deviceTransaction);
66                     }
67
68                     @Override
69                     public void onFailure(final 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(final 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 = transaction.getTypedRowSchema(LogicalRouter.class);
89                 UUID logicalRouterUuid = new UUID(operationalRouterOptional.get().getLogicalRouterUuid().getValue());
90                 transaction.add(op.delete(logicalRouter.getSchema())
91                         .where(logicalRouter.getUuidColumn().getSchema().opEqual(logicalRouterUuid)).build());
92                 transaction.add(op.comment("Logical Router: Deleting " + lrouter.getHwvtepNodeName().getValue()));
93                 updateControllerTxHistory(TransactionType.DELETE, logicalRouter);
94             } else {
95                 LOG.warn("Unable to delete logical router {} because it was not found in the operational data store",
96                         lrouter.getHwvtepNodeName().getValue());
97             }
98         }
99     }
100
101     @Override
102     protected Map<LogicalRoutersKey, LogicalRouters> getData(final HwvtepGlobalAugmentation augmentation) {
103         return augmentation.getLogicalRouters();
104     }
105
106     @Override
107     protected boolean areEqual(final LogicalRouters routers1, final LogicalRouters routers2) {
108         return routers1.key().equals(routers2.key());
109     }
110
111     @Override
112     public boolean isDeleteCmd() {
113         return true;
114     }
115 }