bug 8673 physical switch node is not removed
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepGlobalRemoveCommand.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.transactions.md;
10
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
15 import org.opendaylight.ovsdb.lib.message.TableUpdates;
16 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Optional;
26 import com.google.common.util.concurrent.CheckedFuture;
27
28 public class HwvtepGlobalRemoveCommand extends AbstractTransactionCommand {
29     private static final Logger LOG = LoggerFactory.getLogger(HwvtepGlobalRemoveCommand.class);
30     private static final long ONE_CONNECTED_MANAGER = 1;
31     private static final long ONE_ACTIVE_CONNECTION_IN_PASSIVE_MODE = 1;
32     private final InstanceIdentifier<Node> nodeInstanceIdentifier;
33
34     public HwvtepGlobalRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
35         super(key, updates, dbSchema);
36         this.nodeInstanceIdentifier = key.getInstanceIdentifier();
37     }
38
39     public HwvtepGlobalRemoveCommand(InstanceIdentifier<Node> nodeInstanceIdentifier) {
40         super(null, null, null);
41         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
42     }
43
44     @Override
45     public void execute(ReadWriteTransaction transaction) {
46         CheckedFuture<Optional<Node>, ReadFailedException> hwvtepGlobalFuture = transaction.read(
47                 LogicalDatastoreType.OPERATIONAL, nodeInstanceIdentifier);
48         try {
49             Optional<Node> hwvtepGlobalOptional = hwvtepGlobalFuture.get();
50             if (hwvtepGlobalOptional.isPresent()) {
51                 Node hwvtepNode = hwvtepGlobalOptional.get();
52                 HwvtepGlobalAugmentation hgAugmentation = hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class);
53                 if (checkIfOnlyConnectedManager(hgAugmentation)) {
54                     if (hgAugmentation != null) {
55                         if (hgAugmentation.getSwitches() != null) {
56                             for (Switches hwSwitch : hgAugmentation.getSwitches()) {
57                                 LOG.debug("Deleting hwvtep switch {}", hwSwitch);
58                                 transaction.delete(
59                                         LogicalDatastoreType.OPERATIONAL, hwSwitch.getSwitchRef().getValue());
60                             }
61                         } else {
62                             LOG.debug("{} had no switches", hwvtepNode.getNodeId().getValue());
63                         }
64                     } else {
65                         LOG.warn("{} had no HwvtepGlobalAugmentation", hwvtepNode.getNodeId().getValue());
66                     }
67                     transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceIdentifier);
68                 } else {
69                     LOG.debug("Other southbound plugin instances in cluster are connected to the device,"
70                             + " not deleting OvsdbNode form data store.");
71                 }
72             }
73         } catch (Exception e) {
74             LOG.warn("Failure to delete ovsdbNode", e);
75         }
76     }
77
78     private boolean checkIfOnlyConnectedManager(HwvtepGlobalAugmentation hgAugmentation) {
79         Managers onlyConnectedManager = null;
80         if (hgAugmentation != null) {
81             int connectedManager = 0;
82             if (hgAugmentation.getManagers() != null) {
83                 for (Managers manager : hgAugmentation.getManagers()) {
84                     if (manager.isIsConnected()) {
85                         connectedManager++;
86                         if (connectedManager > ONE_CONNECTED_MANAGER) {
87                             return false;
88                         }
89                         onlyConnectedManager = manager;
90                     }
91                 }
92             }
93             if (connectedManager == 0) {
94                 return true;
95             }
96         }
97         /*When switch is listening in passive mode, this number represent number of active connection to the device
98         This is to handle the controller initiated connection scenario, where all the controller will connect, but
99         switch will have only one manager.
100         */
101         /* CLUSTERING-TODO-ITEM: For hwvtep we don't have getNumberOfConnections()
102          * FIXME: Add it to yang?
103         if (onlyConnectedManager.getNumberOfConnections() > ONE_ACTIVE_CONNECTION_IN_PASSIVE_MODE) {
104             return false;
105         }*/
106         return true;
107     }
108 }