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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class NodeCopier implements INodeCopier {
46 static Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
48 GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
49 PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
50 GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
51 PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
53 HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
56 public NodeCopier(DataBroker db) {
60 public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
61 InstanceIdentifier<Node> srcPath,
62 InstanceIdentifier<Node> dstPath,
63 LogicalDatastoreType logicalDatastoreType,
64 ReadWriteTransaction tx) throws ReadFailedException {
65 if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
66 Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
68 public void onSuccess(Optional<Node> nodeOptional) {
69 HAJobScheduler.getInstance().submitJob(() -> {
71 ReadWriteTransaction tx1 = new BatchedTransaction();
72 if (nodeOptional.isPresent()) {
73 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
76 * In case the Parent HA Global Node is not present and Child HA node is present
77 * It means that both the child are disconnected/removed hence the parent is deleted.
78 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
79 * OnGLobalNode() delete function
80 * So we should delete the existing config child node as cleanup
82 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
84 } catch (ReadFailedException e) {
85 LOG.error("Failed to read source node {}",srcPath);
91 public void onFailure(Throwable throwable) {
96 HwvtepGlobalAugmentation srcGlobalAugmentation =
97 srcGlobalNodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class);
98 if (srcGlobalAugmentation == null) {
100 * If Source HA Global Node is not present
101 * It means that both the child are disconnected/removed hence the parent is deleted.
102 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
103 * So we should delete the existing config child node as cleanup
105 HwvtepHAUtil.deleteNodeIfPresent(tx, logicalDatastoreType, dstPath);
108 NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
109 HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
111 Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
112 Node existingDstGlobalNode =
113 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
114 HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
116 globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
117 globalNodeMerger.mergeOperationalData(haNodeBuilder,
118 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
121 if (OPERATIONAL == logicalDatastoreType) {
122 haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
123 existingDstGlobalNodeOptional));
124 //Also update the manager section in config which helps in cluster reboot scenarios
125 haBuilder.getManagers().stream().forEach((manager) -> {
126 InstanceIdentifier<Managers> managerIid = dstPath.augmentation(HwvtepGlobalAugmentation.class)
127 .child(Managers.class, manager.getKey());
128 tx.put(CONFIGURATION, managerIid, manager, true);
132 haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
133 haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
134 Node haNode = haNodeBuilder.build();
135 if (OPERATIONAL == logicalDatastoreType) {
136 tx.merge(logicalDatastoreType, dstPath, haNode, true);
138 tx.put(logicalDatastoreType, dstPath, haNode, true);
142 public void copyPSNode(Optional<Node> srcPsNodeOptional,
143 InstanceIdentifier<Node> srcPsPath,
144 InstanceIdentifier<Node> dstPsPath,
145 InstanceIdentifier<Node> dstGlobalPath,
146 LogicalDatastoreType logicalDatastoreType,
147 ReadWriteTransaction tx) throws ReadFailedException {
148 if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
149 Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
151 public void onSuccess(Optional<Node> nodeOptional) {
152 HAJobScheduler.getInstance().submitJob(() -> {
154 ReadWriteTransaction tx1 = new BatchedTransaction();
155 if (nodeOptional.isPresent()) {
156 copyPSNode(nodeOptional,
157 srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
160 * Deleting node please refer @see #copyGlobalNode for explanation
162 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
164 } catch (ReadFailedException e) {
165 LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
171 public void onFailure(Throwable throwable) {
176 NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
177 PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
179 PhysicalSwitchAugmentation srcPsAugmenatation =
180 srcPsNodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class);
182 Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
183 PhysicalSwitchAugmentation existingDstPsAugmentation =
184 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
185 if (OPERATIONAL == logicalDatastoreType) {
186 psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
187 srcPsAugmenatation, dstPsPath);
188 psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
190 psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
191 psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
193 mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
195 dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
196 Node dstPsNode = dstPsNodeBuilder.build();
197 tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
198 LOG.debug("Copied {} physical switch node from {} to {}", logicalDatastoreType, srcPsPath, dstPsPath);
201 public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
202 PhysicalSwitchAugmentationBuilder builder,
203 InstanceIdentifier<Node> haNodePath) {
204 builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
205 if (psAugmentation != null) {
206 builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
207 builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
208 builder.setTunnelIps(psAugmentation.getTunnelIps());
209 if (psAugmentation.getHwvtepNodeName() != null) {
210 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));