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;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
23 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
24 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAJobScheduler;
25 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
26 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalNodeMerger;
27 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSAugmentationMerger;
28 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSNodeMerger;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class NodeCopier implements INodeCopier {
44 private static final Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
46 private final GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
47 private final PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
48 private final GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
49 private final PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
50 private final DataBroker db;
53 public NodeCopier(DataBroker db) {
58 public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
59 InstanceIdentifier<Node> srcPath,
60 InstanceIdentifier<Node> dstPath,
61 LogicalDatastoreType logicalDatastoreType,
62 ReadWriteTransaction tx) throws ReadFailedException {
63 if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
64 Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
66 public void onSuccess(Optional<Node> nodeOptional) {
67 HAJobScheduler.getInstance().submitJob(() -> {
69 ReadWriteTransaction tx1 = new BatchedTransaction();
70 if (nodeOptional.isPresent()) {
71 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
74 * In case the Parent HA Global Node is not present and Child HA node is present
75 * It means that both the child are disconnected/removed hence the parent is deleted.
76 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
77 * OnGLobalNode() delete function
78 * So we should delete the existing config child node as cleanup
80 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
82 } catch (ReadFailedException e) {
83 LOG.error("Failed to read source node {}",srcPath);
89 public void onFailure(Throwable throwable) {
94 HwvtepGlobalAugmentation srcGlobalAugmentation =
95 srcGlobalNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
96 if (srcGlobalAugmentation == null) {
98 * If Source HA Global Node is not present
99 * It means that both the child are disconnected/removed hence the parent is deleted.
100 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
101 * So we should delete the existing config child node as cleanup
103 HwvtepHAUtil.deleteNodeIfPresent(tx, logicalDatastoreType, dstPath);
106 NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
107 HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
109 Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
110 Node existingDstGlobalNode =
111 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
112 HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
114 globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
115 globalNodeMerger.mergeOperationalData(haNodeBuilder,
116 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
119 if (OPERATIONAL == logicalDatastoreType) {
120 haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
121 existingDstGlobalNodeOptional));
122 //Also update the manager section in config which helps in cluster reboot scenarios
123 haBuilder.getManagers().forEach((manager) -> {
124 InstanceIdentifier<Managers> managerIid = dstPath.augmentation(HwvtepGlobalAugmentation.class)
125 .child(Managers.class, manager.key());
126 tx.put(CONFIGURATION, managerIid, manager, true);
130 haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
131 haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
132 Node haNode = haNodeBuilder.build();
133 if (OPERATIONAL == logicalDatastoreType) {
134 tx.merge(logicalDatastoreType, dstPath, haNode, true);
136 tx.put(logicalDatastoreType, dstPath, haNode, true);
141 public void copyPSNode(Optional<Node> srcPsNodeOptional,
142 InstanceIdentifier<Node> srcPsPath,
143 InstanceIdentifier<Node> dstPsPath,
144 InstanceIdentifier<Node> dstGlobalPath,
145 LogicalDatastoreType logicalDatastoreType,
146 ReadWriteTransaction tx) throws ReadFailedException {
147 if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
148 Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
150 public void onSuccess(Optional<Node> nodeOptional) {
151 HAJobScheduler.getInstance().submitJob(() -> {
153 ReadWriteTransaction tx1 = new BatchedTransaction();
154 if (nodeOptional.isPresent()) {
155 copyPSNode(nodeOptional,
156 srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
159 * Deleting node please refer @see #copyGlobalNode for explanation
161 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
163 } catch (ReadFailedException e) {
164 LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
170 public void onFailure(Throwable throwable) {
175 NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
176 PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
178 PhysicalSwitchAugmentation srcPsAugmenatation =
179 srcPsNodeOptional.get().augmentation(PhysicalSwitchAugmentation.class);
181 Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
182 PhysicalSwitchAugmentation existingDstPsAugmentation =
183 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
184 if (OPERATIONAL == logicalDatastoreType) {
185 psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
186 srcPsAugmenatation, dstPsPath);
187 psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
189 psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
190 psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
192 mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
194 dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
195 Node dstPsNode = dstPsNodeBuilder.build();
196 tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
197 LOG.debug("Copied {} physical switch node from {} to {}", logicalDatastoreType, srcPsPath, dstPsPath);
200 public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
201 PhysicalSwitchAugmentationBuilder builder,
202 InstanceIdentifier<Node> haNodePath) {
203 builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
204 if (psAugmentation != null) {
205 builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
206 builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
207 builder.setTunnelIps(psAugmentation.getTunnelIps());
208 if (psAugmentation.getHwvtepNodeName() != null) {
209 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));