d1ad3cc737e7e94e1de87b59a0eb402408f74640
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PlainLogicalSwitchRemoveCmd.java
1 /*
2  * Copyright (c) 2015 China Telecom Beijing Research Institute 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 java.util.Collection;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicInteger;
18
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.Scheduler;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PlainLogicalSwitchRemoveCmd extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(PlainLogicalSwitchRemoveCmd.class);
34     private AtomicInteger retryCount = new AtomicInteger(5);
35     private LogicalSwitches logicalSwitches;
36     private InstanceIdentifier<Node> nodeIid;
37
38     public PlainLogicalSwitchRemoveCmd(HwvtepOperationalState state,
39                                        Collection<DataTreeModification<Node>> changes,
40                                        LogicalSwitches logicalSwitches,
41                                        int retryCount) {
42         super(state, changes);
43         this.logicalSwitches = logicalSwitches;
44         this.retryCount = new AtomicInteger(retryCount);
45         this.nodeIid = getOperationalState().getConnectionInstance().getInstanceIdentifier();
46     }
47
48     @Override
49     public void execute(TransactionBuilder transaction) {
50         LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(
51                 transaction.getDatabaseSchema(), LogicalSwitch.class, null);
52         transaction.add(op.delete(logicalSwitch.getSchema())
53                 .where(logicalSwitch.getNameColumn().getSchema().opEqual(
54                         logicalSwitches.getHwvtepNodeName().getValue())).build());
55     }
56
57     @Override
58     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
59         return augmentation.getLogicalSwitches();
60     }
61
62     @Override
63     protected boolean areEqual(LogicalSwitches logicalSwitches1 , LogicalSwitches logicalSwitches2) {
64         return logicalSwitches1.key().equals(logicalSwitches2.key())
65                 && Objects.equals(logicalSwitches1.getTunnelKey(), logicalSwitches2.getTunnelKey());
66     }
67
68     public boolean retry() {
69         boolean ret = retryCount.decrementAndGet() > 0;
70         if (ret) {
71             Scheduler.getScheduledExecutorService().schedule(() -> {
72                 getOperationalState().getConnectionInstance().transact(this);
73             }, HwvtepSouthboundConstants.LS_REMOVE_DELAY_SECS, TimeUnit.SECONDS);
74         } else {
75             LOG.error("Failed in deletion of logical switch {}", logicalSwitches);
76         }
77         return ret;
78     }
79
80     protected boolean isDeleteCmd() {
81         return true;
82     }
83 }