ab86ae74fe690c0db02c612c473c5324b50aa004
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / HwvtepHAUtil.java
1 /*
2  * Copyright © 2016, 2017 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 package org.opendaylight.netvirt.elan.l2gw.ha;
9
10 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Strings;
14 import java.nio.charset.StandardCharsets;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.ExecutionException;
22 import java.util.stream.Collectors;
23 import javax.annotation.Nullable;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.genius.infra.Datastore;
26 import org.opendaylight.genius.infra.Datastore.Configuration;
27 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
28 import org.opendaylight.genius.infra.TypedWriteTransaction;
29 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
30 import org.opendaylight.netvirt.elan.l2gw.ha.commands.SwitchesCmd;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ManagersBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ManagersKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigs;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigsKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 public final class HwvtepHAUtil {
65
66     private static final Logger LOG = LoggerFactory.getLogger(HwvtepHAUtil.class);
67
68     //TODO reuse HWvtepSouthboundConstants
69     public static final String HA_ENABLED = "ha_enabled";
70     public static final String HWVTEP_ENTITY_TYPE = "hwvtep";
71     public static final String TEP_PREFIX = "vxlan_over_ipv4:";
72     public static final String HA_ID = "ha_id";
73     public static final String HA_CHILDREN = "ha_children";
74     public static final String PHYSICALSWITCH = "/physicalswitch/";
75     public static final TopologyId HWVTEP_TOPOLOGY_ID = new TopologyId(new Uri("hwvtep:1"));
76     public static final String UUID = "uuid";
77     public static final String HWVTEP_URI_PREFIX = "hwvtep";
78     public static final String MANAGER_KEY = "managerKey";
79     public static final String L2GW_JOB_KEY = ":l2gw";
80
81     private HwvtepHAUtil() { }
82
83     public static HwvtepPhysicalLocatorRef buildLocatorRef(InstanceIdentifier<Node> nodeIid, String tepIp) {
84         InstanceIdentifier<TerminationPoint> tepId = buildTpId(nodeIid, tepIp);
85         return new HwvtepPhysicalLocatorRef(tepId);
86     }
87
88     public static String getNodeIdVal(InstanceIdentifier<?> iid) {
89         return iid.firstKeyOf(Node.class).getNodeId().getValue();
90     }
91
92     public static Uuid getUUid(String key) {
93         return new Uuid(java.util.UUID.nameUUIDFromBytes(key.getBytes(StandardCharsets.UTF_8)).toString());
94     }
95
96     public static InstanceIdentifier<TerminationPoint> buildTpId(InstanceIdentifier<Node> nodeIid,String tepIp) {
97         String tpKeyStr = TEP_PREFIX + tepIp;
98         TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpKeyStr));
99         InstanceIdentifier<TerminationPoint> plIid = nodeIid.child(TerminationPoint.class, tpKey);
100         return plIid;
101     }
102
103     public static String getTepIpVal(HwvtepPhysicalLocatorRef locatorRef) {
104         InstanceIdentifier<TerminationPoint> tpId = (InstanceIdentifier<TerminationPoint>) locatorRef.getValue();
105         return tpId.firstKeyOf(TerminationPoint.class).getTpId().getValue().substring("vxlan_over_ipv4:".length());
106     }
107
108     public static InstanceIdentifier<Node> createInstanceIdentifierFromHAId(String haUUidVal) {
109         String nodeString = HWVTEP_URI_PREFIX + "://"
110             + UUID + "/" + java.util.UUID.nameUUIDFromBytes(haUUidVal.getBytes(StandardCharsets.UTF_8)).toString();
111         NodeId nodeId = new NodeId(new Uri(nodeString));
112         NodeKey nodeKey = new NodeKey(nodeId);
113         TopologyKey topoKey = new TopologyKey(HWVTEP_TOPOLOGY_ID);
114         return InstanceIdentifier.builder(NetworkTopology.class)
115                 .child(Topology.class, topoKey)
116                 .child(Node.class, nodeKey)
117                 .build();
118     }
119
120     public static InstanceIdentifier<Node> convertToInstanceIdentifier(String nodeIdString) {
121         NodeId nodeId = new NodeId(new Uri(nodeIdString));
122         NodeKey nodeKey = new NodeKey(nodeId);
123         TopologyKey topoKey = new TopologyKey(HWVTEP_TOPOLOGY_ID);
124         return InstanceIdentifier.builder(NetworkTopology.class)
125                 .child(Topology.class, topoKey)
126                 .child(Node.class, nodeKey)
127                 .build();
128     }
129
130     /**
131      * Build other config data for HA node .
132      *
133      * @param key The key as in HA child device other config
134      * @param val The value as in HA child device other config
135      * @return return other config object
136      */
137     public static ManagerOtherConfigsBuilder getOtherConfigBuilder(String key, String val) {
138         ManagerOtherConfigsBuilder otherConfigsBuilder = new ManagerOtherConfigsBuilder();
139         ManagerOtherConfigsKey otherConfigsKey = new ManagerOtherConfigsKey(key);
140         otherConfigsBuilder.withKey(otherConfigsKey);
141         otherConfigsBuilder.setOtherConfigKey(key);
142         otherConfigsBuilder.setOtherConfigValue(val);
143         return otherConfigsBuilder;
144     }
145
146     public static String convertToGlobalNodeId(String psNodeId) {
147         int idx = psNodeId.indexOf(PHYSICALSWITCH);
148         if (idx > 0) {
149             return psNodeId.substring(0, idx);
150         }
151         return psNodeId;
152     }
153
154     /**
155      * Trnaform logical switch to nodepath passed .
156      *
157      * @param src {@link HwvtepLogicalSwitchRef} Logical Switch Ref which needs to be transformed
158      * @param nodePath {@link InstanceIdentifier} src needs to be transformed to this path
159      * @return ref {@link HwvtepLogicalSwitchRef} the transforrmed result
160      */
161     public static HwvtepLogicalSwitchRef convertLogicalSwitchRef(HwvtepLogicalSwitchRef src,
162                                                                  InstanceIdentifier<Node> nodePath) {
163         InstanceIdentifier<LogicalSwitches> srcId = (InstanceIdentifier<LogicalSwitches>)src.getValue();
164         HwvtepNodeName switchName = srcId.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName();
165         InstanceIdentifier<LogicalSwitches> iid = nodePath.augmentation(HwvtepGlobalAugmentation.class)
166                 .child(LogicalSwitches.class, new LogicalSwitchesKey(switchName));
167         HwvtepLogicalSwitchRef ref = new HwvtepLogicalSwitchRef(iid);
168         return ref;
169     }
170
171     /**
172      * Trnaform locator reference to nodepath passed .
173      *
174      * @param src {@link HwvtepPhysicalLocatorRef} Logical Switch Ref which needs to be transformed
175      * @param nodePath {@link InstanceIdentifier} src needs to be transformed to this path
176      * @return physicalLocatorRef {@link HwvtepPhysicalLocatorRef} the transforrmed result
177      */
178     public static HwvtepPhysicalLocatorRef convertLocatorRef(HwvtepPhysicalLocatorRef src,
179                                                              InstanceIdentifier<Node> nodePath) {
180         InstanceIdentifier<TerminationPoint> srcTepPath = (InstanceIdentifier<TerminationPoint>)src.getValue();
181         TpId tpId = srcTepPath.firstKeyOf(TerminationPoint.class).getTpId();
182         InstanceIdentifier<TerminationPoint> tpPath =
183                 nodePath.child(TerminationPoint.class, new TerminationPointKey(tpId));
184         HwvtepPhysicalLocatorRef physicalLocatorRef = new HwvtepPhysicalLocatorRef(tpPath);
185         return physicalLocatorRef;
186     }
187
188     public static boolean isEmptyList(@Nullable List list) {
189         return list == null || list.isEmpty();
190     }
191
192     public static boolean isEmpty(Collection collection) {
193         return collection == null || collection.isEmpty();
194     }
195
196     @Nullable
197     public static Node getOriginal(DataObjectModification<Node> mod) {
198         Node node = null;
199         switch (mod.getModificationType()) {
200             case SUBTREE_MODIFIED:
201             case DELETE:
202                 node = mod.getDataBefore();
203                 break;
204             case WRITE:
205                 if (mod.getDataBefore() !=  null) {
206                     node = mod.getDataBefore();
207                 }
208                 break;
209             default:
210                 break;
211         }
212         return node;
213     }
214
215     @Nullable
216     public static Node getUpdated(DataObjectModification<Node> mod) {
217         switch (mod.getModificationType()) {
218             case SUBTREE_MODIFIED:
219             case WRITE:
220                 return mod.getDataAfter();
221             default:
222                 return null;
223         }
224     }
225
226     @Nullable
227     public static Node getCreated(DataObjectModification<Node> mod) {
228         if (mod.getModificationType() == DataObjectModification.ModificationType.WRITE
229                 && mod.getDataBefore() == null) {
230             return mod.getDataAfter();
231         }
232         return null;
233     }
234
235     @Nullable
236     public static Node getRemoved(DataObjectModification<Node> mod) {
237         if (mod.getModificationType() == DataObjectModification.ModificationType.DELETE) {
238             return mod.getDataBefore();
239         }
240         return null;
241     }
242
243     @Nullable
244     public static String getPsName(InstanceIdentifier<Node> psNodeIid) {
245         String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
246         if (psNodeId.contains(PHYSICALSWITCH)) {
247             return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) +  PHYSICALSWITCH.length());
248         }
249         return null;
250     }
251
252     public static InstanceIdentifier<Node> getGlobalNodePathFromPSNode(Node psNode) {
253         String psNodeId = psNode.getNodeId().getValue();
254         if (psNodeId.contains(PHYSICALSWITCH)) {
255             return convertToInstanceIdentifier(psNodeId.substring(0, psNodeId.indexOf(PHYSICALSWITCH)));
256         }
257         return convertToInstanceIdentifier(psNodeId);
258     }
259
260     @Nullable
261     public static InstanceIdentifier<Node> convertPsPath(Node psNode, InstanceIdentifier<Node> nodePath) {
262         String psNodeId = psNode.getNodeId().getValue();
263         if (psNodeId.contains(PHYSICALSWITCH)) {
264             String psName = psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
265             String haPsNodeIdVal = nodePath.firstKeyOf(Node.class).getNodeId().getValue() + PHYSICALSWITCH + psName;
266             InstanceIdentifier<Node> haPsPath = convertToInstanceIdentifier(haPsNodeIdVal);
267             return haPsPath;
268         } else {
269             LOG.error("Failed to find ps path from node {}", psNode);
270             return null;
271         }
272     }
273
274     public static NodeBuilder getNodeBuilderForPath(InstanceIdentifier<Node> haPath) {
275         NodeBuilder nodeBuilder = new NodeBuilder();
276         nodeBuilder.setNodeId(haPath.firstKeyOf(Node.class).getNodeId());
277         return nodeBuilder;
278     }
279
280     @Nullable
281     public static String getHAIdFromManagerOtherConfig(Node node) {
282         if (node.augmentation(HwvtepGlobalAugmentation.class) == null) {
283             return null;
284         }
285         HwvtepGlobalAugmentation globalAugmentation = node.augmentation(HwvtepGlobalAugmentation.class);
286         if (globalAugmentation != null) {
287             List<Managers> managers = globalAugmentation.getManagers();
288             if (managers != null && !managers.isEmpty() && managers.get(0).getManagerOtherConfigs() != null) {
289                 for (ManagerOtherConfigs configs : managers.get(0).getManagerOtherConfigs()) {
290                     if (HA_ID.equals(configs.getOtherConfigKey())) {
291                         return configs.getOtherConfigValue();
292                     }
293                 }
294             }
295         }
296         return null;
297     }
298
299     /**
300      * Returns ha child node path from ha node of config data tree.
301      *
302      * @param haGlobalConfigNodeOptional HA global node
303      * @return ha Child ids
304      */
305     public static  List<NodeId> getChildNodeIdsFromManagerOtherConfig(Optional<Node> haGlobalConfigNodeOptional) {
306         List<NodeId> childNodeIds = new ArrayList<>();
307         if (!haGlobalConfigNodeOptional.isPresent()) {
308             return childNodeIds;
309         }
310         HwvtepGlobalAugmentation augmentation =
311                 haGlobalConfigNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
312         if (augmentation != null && augmentation.getManagers() != null
313                 && augmentation.getManagers().size() > 0) {
314             Managers managers = augmentation.getManagers().get(0);
315             if (null == managers.getManagerOtherConfigs()) {
316                 return childNodeIds;
317             }
318             for (ManagerOtherConfigs otherConfigs : managers.getManagerOtherConfigs()) {
319                 if (HA_CHILDREN.equals(otherConfigs.getOtherConfigKey())) {
320                     String nodeIdsVal = otherConfigs.getOtherConfigValue();
321                     if (nodeIdsVal != null) {
322                         String[] parts = nodeIdsVal.split(",");
323                         for (String part : parts) {
324                             childNodeIds.add(new NodeId(part));
325                         }
326                     }
327
328                 }
329             }
330         }
331         return childNodeIds;
332     }
333
334     public static HwvtepGlobalAugmentation getGlobalAugmentationOfNode(Node node) {
335         HwvtepGlobalAugmentation result = null;
336         if (node != null) {
337             result = node.augmentation(HwvtepGlobalAugmentation.class);
338         }
339         if (result == null) {
340             result = new HwvtepGlobalAugmentationBuilder().build();
341         }
342         return result;
343     }
344
345     public static PhysicalSwitchAugmentation getPhysicalSwitchAugmentationOfNode(Node psNode) {
346         PhysicalSwitchAugmentation result = null;
347         if (psNode != null) {
348             result = psNode.augmentation(PhysicalSwitchAugmentation.class);
349         }
350         if (result == null) {
351             result = new PhysicalSwitchAugmentationBuilder().build();
352         }
353         return result;
354     }
355
356     /**
357      * Transform child managers (Source) to HA managers using HA node path.
358      *
359      * @param childNode Child Node
360      * @param haGlobalCfg HA global config node
361      * @return Transformed managers
362      */
363     public static List<Managers> buildManagersForHANode(Node childNode, Optional<Node> haGlobalCfg) {
364
365         Set<NodeId> nodeIds = new HashSet<>();
366         nodeIds.add(childNode.getNodeId());
367         List<NodeId> childNodeIds = getChildNodeIdsFromManagerOtherConfig(haGlobalCfg);
368         nodeIds.addAll(childNodeIds);
369
370         ManagersBuilder builder1 = new ManagersBuilder();
371
372         builder1.withKey(new ManagersKey(new Uri(MANAGER_KEY)));
373         List<ManagerOtherConfigs> otherConfigses = new ArrayList<>();
374         String children = nodeIds.stream().map(NodeId::getValue).collect(Collectors.joining(","));
375         otherConfigses.add(getOtherConfigBuilder(HA_CHILDREN, children).build());
376         builder1.setManagerOtherConfigs(otherConfigses);
377         List<Managers> managers = new ArrayList<>();
378         managers.add(builder1.build());
379         return managers;
380     }
381
382     /**
383      * Transform child switch (Source) to HA swicthes using HA node path.
384      *
385      * @param childNode  HA child node
386      * @param haNodePath  HA node path
387      * @param haNode Ha node object
388      * @return Transformed switches
389      */
390     public static List<Switches> buildSwitchesForHANode(Node childNode,
391                                                         InstanceIdentifier<Node> haNodePath,
392                                                         Optional<Node> haNode) {
393         List<Switches> psList = new ArrayList<>();
394         boolean switchesAlreadyPresent = false;
395         if (haNode.isPresent()) {
396             Node node = haNode.get();
397             HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
398             if (augmentation != null) {
399                 if (augmentation.getSwitches() != null) {
400                     if (augmentation.getSwitches().size() > 0) {
401                         switchesAlreadyPresent = true;
402                     }
403                 }
404             }
405         }
406         if (!switchesAlreadyPresent) {
407             HwvtepGlobalAugmentation augmentation = childNode.augmentation(HwvtepGlobalAugmentation.class);
408             if (augmentation != null && augmentation.getSwitches() != null) {
409                 List<Switches> src = augmentation.getSwitches();
410                 if (src != null && src.size() > 0) {
411                     psList.add(new SwitchesCmd().transform(haNodePath, src.get(0)));
412                 }
413             }
414         }
415         return psList;
416     }
417
418     /**
419      * Build HA Global node from child nodes in config data tress.
420      *
421      * @param tx Transaction
422      * @param childNode Child Node object
423      * @param haNodePath Ha node path
424      * @param haGlobalCfg HA global node object
425      */
426     public static void buildGlobalConfigForHANode(TypedWriteTransaction<Configuration> tx,
427                                                   Node childNode,
428                                                   InstanceIdentifier<Node> haNodePath,
429                                                   Optional<Node> haGlobalCfg) {
430
431         NodeBuilder nodeBuilder = new NodeBuilder();
432         HwvtepGlobalAugmentationBuilder hwvtepGlobalBuilder = new HwvtepGlobalAugmentationBuilder();
433         hwvtepGlobalBuilder.setSwitches(buildSwitchesForHANode(childNode, haNodePath, haGlobalCfg));
434         hwvtepGlobalBuilder.setManagers(buildManagersForHANode(childNode, haGlobalCfg));
435
436         nodeBuilder.setNodeId(haNodePath.firstKeyOf(Node.class).getNodeId());
437         nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, hwvtepGlobalBuilder.build());
438         Node configHANode = nodeBuilder.build();
439         tx.merge(haNodePath, configHANode, CREATE_MISSING_PARENTS);
440     }
441
442     public static <D extends Datastore> void deleteNodeIfPresent(TypedReadWriteTransaction<D> tx,
443             InstanceIdentifier<?> iid) throws ExecutionException, InterruptedException {
444         if (tx.read(iid).get().isPresent()) {
445             LOG.info("Deleting child node {}", getNodeIdVal(iid));
446             tx.delete(iid);
447         }
448     }
449
450     /**
451      * Delete PS data of HA node of Config Data tree.
452      *
453      * @param key Node object
454      * @param haNode Ha Node from which to be deleted
455      * @param tx Transaction
456      */
457     public static void deletePSNodesOfNode(InstanceIdentifier<Node> key, Node haNode,
458             TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
459         //read from switches attribute and clean up them
460         HwvtepGlobalAugmentation globalAugmentation = haNode.augmentation(HwvtepGlobalAugmentation.class);
461         if (globalAugmentation == null) {
462             return;
463         }
464         HashMap<InstanceIdentifier<Node>,Boolean> deleted = new HashMap<>();
465         List<Switches> switches = globalAugmentation.getSwitches();
466         if (switches != null) {
467             for (Switches switche : switches) {
468                 InstanceIdentifier<Node> psId = (InstanceIdentifier<Node>)switche.getSwitchRef().getValue();
469                 deleteNodeIfPresent(tx, psId);
470                 deleted.put(psId, Boolean.TRUE);
471             }
472         }
473         //also read from managed by attribute of switches and cleanup them as a back up if the above cleanup fails
474         Optional<Topology> topologyOptional = tx .read(key.firstIdentifierOf(Topology.class)).get();
475         String deletedNodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
476         if (topologyOptional.isPresent()) {
477             Topology topology = topologyOptional.get();
478             if (topology.getNode() != null) {
479                 for (Node psNode : topology.getNode()) {
480                     PhysicalSwitchAugmentation ps = psNode.augmentation(PhysicalSwitchAugmentation.class);
481                     if (ps != null) {
482                         InstanceIdentifier<Node> iid = (InstanceIdentifier<Node>)ps.getManagedBy().getValue();
483                         String nodeIdVal = iid.firstKeyOf(Node.class).getNodeId().getValue();
484                         if (deletedNodeId.equals(nodeIdVal)) {
485                             InstanceIdentifier<Node> psNodeId =
486                                     convertToInstanceIdentifier(psNode.getNodeId().getValue());
487                             if (deleted.containsKey(psNodeId)) {
488                                 deleteNodeIfPresent(tx, psNodeId);
489                             }
490                         }
491                     }
492                 }
493             }
494         }
495     }
496
497     public static void addToCacheIfHAChildNode(InstanceIdentifier<Node> childPath, Node childNode,
498             HwvtepNodeHACache hwvtepNodeHACache) {
499         String haId = HwvtepHAUtil.getHAIdFromManagerOtherConfig(childNode);
500         if (!Strings.isNullOrEmpty(haId)) {
501             InstanceIdentifier<Node> parentId = HwvtepHAUtil.createInstanceIdentifierFromHAId(haId);
502             hwvtepNodeHACache.addChild(parentId, childPath/*child*/);
503         }
504     }
505 }