2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.ha.handlers;
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
24 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
25 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
26 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAJobScheduler;
27 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
28 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalNodeMerger;
29 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSAugmentationMerger;
30 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSNodeMerger;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class NodeCopier implements INodeCopier {
45 static Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
47 GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
48 PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
49 GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
50 PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
52 HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
55 public NodeCopier(DataBroker db) {
59 public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
60 InstanceIdentifier<Node> srcPath,
61 InstanceIdentifier<Node> dstPath,
62 LogicalDatastoreType logicalDatastoreType,
63 ReadWriteTransaction tx) throws ReadFailedException {
64 if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
65 Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
67 public void onSuccess(Optional<Node> nodeOptional) {
68 HAJobScheduler.getInstance().submitJob(() -> {
70 ReadWriteTransaction tx1 = new BatchedTransaction();
71 if (nodeOptional.isPresent()) {
72 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
75 * In case the Parent HA Global Node is not present and Child HA node is present
76 * It means that both the child are disconnected/removed hence the parent is deleted.
77 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
78 * OnGLobalNode() delete function
79 * So we should delete the existing config child node as cleanup
81 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
83 } catch (ReadFailedException e) {
84 LOG.error("Failed to read source node {}",srcPath);
90 public void onFailure(Throwable throwable) {
95 HwvtepGlobalAugmentation srcGlobalAugmentation =
96 srcGlobalNodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class);
97 if (srcGlobalAugmentation == null) {
99 * If Source HA Global Node is not present
100 * It means that both the child are disconnected/removed hence the parent is deleted.
101 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
102 * So we should delete the existing config child node as cleanup
104 HwvtepHAUtil.deleteNodeIfPresent(tx, logicalDatastoreType, dstPath);
107 NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
108 HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
110 Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
111 Node existingDstGlobalNode =
112 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
113 HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
115 globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
116 globalNodeMerger.mergeOperationalData(haNodeBuilder,
117 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
120 if (OPERATIONAL == logicalDatastoreType) {
121 haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
122 existingDstGlobalNodeOptional));
124 haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
125 Node haNode = haNodeBuilder.build();
126 tx.merge(logicalDatastoreType, dstPath, haNode, true);
129 public void copyPSNode(Optional<Node> srcPsNodeOptional,
130 InstanceIdentifier<Node> srcPsPath,
131 InstanceIdentifier<Node> dstPsPath,
132 InstanceIdentifier<Node> dstGlobalPath,
133 LogicalDatastoreType logicalDatastoreType,
134 ReadWriteTransaction tx) throws ReadFailedException {
135 if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
136 Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
138 public void onSuccess(Optional<Node> nodeOptional) {
139 HAJobScheduler.getInstance().submitJob(() -> {
141 ReadWriteTransaction tx1 = new BatchedTransaction();
142 if (nodeOptional.isPresent()) {
143 copyPSNode(nodeOptional,
144 srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
147 * Deleting node please refer @see #copyGlobalNode for explanation
149 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
151 } catch (ReadFailedException e) {
152 LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
158 public void onFailure(Throwable throwable) {
163 NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
164 PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
166 PhysicalSwitchAugmentation srcPsAugmenatation =
167 srcPsNodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class);
169 Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
170 PhysicalSwitchAugmentation existingDstPsAugmentation =
171 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
172 if (OPERATIONAL == logicalDatastoreType) {
173 psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
174 srcPsAugmenatation, dstPsPath);
175 psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
177 psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
178 psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
180 mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
182 dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
183 Node dstPsNode = dstPsNodeBuilder.build();
184 tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
187 public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
188 PhysicalSwitchAugmentationBuilder builder,
189 InstanceIdentifier<Node> haNodePath) {
190 builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
191 builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
192 builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
193 builder.setTunnelIps(psAugmentation.getTunnelIps());
194 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));