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