b4a785718ddd41d967aac3e124839226d579326f
[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.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalRouter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalRouters;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.Identifiable;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class LogicalRouterRemoveCommand extends AbstractTransactCommand<LogicalRouters, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(LogicalRouterRemoveCommand.class);
34
35     public LogicalRouterRemoveCommand(HwvtepOperationalState state,
36             Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<LogicalRouters>> removed =
43               extractRemoved(getChanges(),LogicalRouters.class);
44       if (removed != null) {
45           for (Entry<InstanceIdentifier<Node>, List<LogicalRouters>> created: removed.entrySet()) {
46               if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
47                   for (LogicalRouters lRouter : created.getValue()) {
48                       InstanceIdentifier<LogicalRouters> lsKey = created.getKey().augmentation(
49                               HwvtepGlobalAugmentation.class).child(LogicalRouters.class, lRouter.getKey());
50                       updateCurrentTxDeleteData(LogicalRouters.class, lsKey, lRouter);
51                   }
52                   getOperationalState().getDeviceInfo().scheduleTransaction(new TransactCommand() {
53                       @Override
54                       public void execute(TransactionBuilder transactionBuilder) {
55                           LOG.debug("Running delete logical router in seperate tx {}", created.getKey());
56                           removeLogicalRouter(transactionBuilder, created.getKey(), created.getValue());
57                       }
58
59                       @Override
60                       public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier nodeIid,
61                                                  Identifiable data, InstanceIdentifier key, Object... extraData) {
62                       }
63
64                       @Override
65                       public void doDeviceTransaction(TransactionBuilder transaction, InstanceIdentifier nodeIid,
66                                                       Identifiable data, InstanceIdentifier key, Object... extraData) {
67                       }
68                   });
69               }
70           }
71       }
72     }
73
74       private void removeLogicalRouter(TransactionBuilder transaction,
75               final InstanceIdentifier<Node> instanceIdentifier, final List<LogicalRouters> lRouterList) {
76           for (LogicalRouters lrouter: lRouterList) {
77               LOG.debug("Removing logical router named: {}", lrouter.getHwvtepNodeName().getValue());
78               Optional<LogicalRouters> operationalRouterOptional =
79                       getOperationalState().getLogicalRouters(instanceIdentifier, lrouter.getKey());
80
81               if (operationalRouterOptional.isPresent() &&
82                       operationalRouterOptional.get().getLogicalRouterUuid() != null) {
83                   LogicalRouter logicalRouter = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalRouter.class, null);
84                   UUID logicalRouterUuid = new UUID(operationalRouterOptional.get().getLogicalRouterUuid().getValue());
85                   transaction.add(op.delete(logicalRouter.getSchema())
86                           .where(logicalRouter.getUuidColumn().getSchema().opEqual(logicalRouterUuid)).build());
87                   transaction.add(op.comment("Logical Router: Deleting " + lrouter.getHwvtepNodeName().getValue()));
88               } else {
89                   LOG.warn("Unable to delete logical router {} because it was not found in the operational data store",
90                           lrouter.getHwvtepNodeName().getValue());
91               }
92           }
93       }
94
95       @Override
96       protected List<LogicalRouters> getData(final HwvtepGlobalAugmentation augmentation) {
97           return augmentation.getLogicalRouters();
98       }
99
100       @Override
101       protected boolean areEqual(final LogicalRouters a , final LogicalRouters b) {
102           return a.getKey().equals(b.getKey());
103       }
104 }