bug 8029 added ref counts for physical locators.
[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.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class LogicalRouterRemoveCommand extends AbstractTransactCommand<LogicalRouters, HwvtepGlobalAugmentation> {
35     private static final Logger LOG = LoggerFactory.getLogger(LogicalRouterRemoveCommand.class);
36
37     public LogicalRouterRemoveCommand(HwvtepOperationalState state,
38             Collection<DataTreeModification<Node>> changes) {
39         super(state, changes);
40     }
41
42     @Override
43     public void execute(TransactionBuilder transaction) {
44         Map<InstanceIdentifier<Node>, List<LogicalRouters>> removed =
45               extractRemoved(getChanges(),LogicalRouters.class);
46       if (removed != null) {
47           for (Entry<InstanceIdentifier<Node>, List<LogicalRouters>> created: removed.entrySet()) {
48               if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
49                   getOperationalState().getDeviceInfo().scheduleTransaction(new TransactCommand() {
50                       @Override
51                       public void execute(TransactionBuilder transactionBuilder) {
52                           HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
53                           HwvtepOperationalState operState = new HwvtepOperationalState(
54                                   connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
55                           threadLocalOperationalState.set(operState);
56                           threadLocalDeviceTransaction.set(transactionBuilder);
57                           LOG.debug("Running delete logical router in seperate tx {}", created.getKey());
58                           removeLogicalRouter(transactionBuilder, created.getKey(), created.getValue());
59                       }
60
61
62                       @Override
63                       public void onSuccess(TransactionBuilder deviceTransaction) {
64                           LogicalRouterRemoveCommand.this.onSuccess(deviceTransaction);
65                       }
66
67                       @Override
68                       public void onFailure(TransactionBuilder deviceTransaction) {
69                           LogicalRouterRemoveCommand.this.onFailure(deviceTransaction);
70                       }
71                   });
72               }
73           }
74       }
75     }
76
77       private void removeLogicalRouter(TransactionBuilder transaction,
78               final InstanceIdentifier<Node> instanceIdentifier, final List<LogicalRouters> lRouterList) {
79           for (LogicalRouters lrouter: lRouterList) {
80               LOG.debug("Removing logical router named: {}", lrouter.getHwvtepNodeName().getValue());
81               Optional<LogicalRouters> operationalRouterOptional =
82                       getOperationalState().getLogicalRouters(instanceIdentifier, lrouter.getKey());
83
84               if (operationalRouterOptional.isPresent() &&
85                       operationalRouterOptional.get().getLogicalRouterUuid() != null) {
86                   LogicalRouter logicalRouter = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalRouter.class, null);
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               } else {
92                   LOG.warn("Unable to delete logical router {} because it was not found in the operational data store",
93                           lrouter.getHwvtepNodeName().getValue());
94               }
95           }
96       }
97
98       @Override
99       protected List<LogicalRouters> getData(final HwvtepGlobalAugmentation augmentation) {
100           return augmentation.getLogicalRouters();
101       }
102
103       @Override
104       protected boolean areEqual(final LogicalRouters a , final LogicalRouters b) {
105           return a.getKey().equals(b.getKey());
106       }
107 }