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 private static final 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) {
61 public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
62 InstanceIdentifier<Node> srcPath,
63 InstanceIdentifier<Node> dstPath,
64 LogicalDatastoreType logicalDatastoreType,
65 ReadWriteTransaction tx) throws ReadFailedException {
66 if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
67 Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
69 public void onSuccess(Optional<Node> nodeOptional) {
70 HAJobScheduler.getInstance().submitJob(() -> {
72 ReadWriteTransaction tx1 = new BatchedTransaction();
73 if (nodeOptional.isPresent()) {
74 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
77 * In case the Parent HA Global Node is not present and Child HA node is present
78 * It means that both the child are disconnected/removed hence the parent is deleted.
79 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
80 * OnGLobalNode() delete function
81 * So we should delete the existing config child node as cleanup
83 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
85 } catch (ReadFailedException e) {
86 LOG.error("Failed to read source node {}",srcPath);
92 public void onFailure(Throwable throwable) {
97 HwvtepGlobalAugmentation srcGlobalAugmentation =
98 srcGlobalNodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class);
99 if (srcGlobalAugmentation == null) {
101 * If Source HA Global Node is not present
102 * It means that both the child are disconnected/removed hence the parent is deleted.
103 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
104 * So we should delete the existing config child node as cleanup
106 HwvtepHAUtil.deleteNodeIfPresent(tx, logicalDatastoreType, dstPath);
109 NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
110 HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
112 Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
113 Node existingDstGlobalNode =
114 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
115 HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
117 globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
118 globalNodeMerger.mergeOperationalData(haNodeBuilder,
119 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
122 if (OPERATIONAL == logicalDatastoreType) {
123 haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
124 existingDstGlobalNodeOptional));
125 //Also update the manager section in config which helps in cluster reboot scenarios
126 haBuilder.getManagers().forEach((manager) -> {
127 InstanceIdentifier<Managers> managerIid = dstPath.augmentation(HwvtepGlobalAugmentation.class)
128 .child(Managers.class, manager.getKey());
129 tx.put(CONFIGURATION, managerIid, manager, true);
133 haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
134 haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
135 Node haNode = haNodeBuilder.build();
136 if (OPERATIONAL == logicalDatastoreType) {
137 tx.merge(logicalDatastoreType, dstPath, haNode, true);
139 tx.put(logicalDatastoreType, dstPath, haNode, true);
144 public void copyPSNode(Optional<Node> srcPsNodeOptional,
145 InstanceIdentifier<Node> srcPsPath,
146 InstanceIdentifier<Node> dstPsPath,
147 InstanceIdentifier<Node> dstGlobalPath,
148 LogicalDatastoreType logicalDatastoreType,
149 ReadWriteTransaction tx) throws ReadFailedException {
150 if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
151 Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
153 public void onSuccess(Optional<Node> nodeOptional) {
154 HAJobScheduler.getInstance().submitJob(() -> {
156 ReadWriteTransaction tx1 = new BatchedTransaction();
157 if (nodeOptional.isPresent()) {
158 copyPSNode(nodeOptional,
159 srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
162 * Deleting node please refer @see #copyGlobalNode for explanation
164 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
166 } catch (ReadFailedException e) {
167 LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
173 public void onFailure(Throwable throwable) {
178 NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
179 PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
181 PhysicalSwitchAugmentation srcPsAugmenatation =
182 srcPsNodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class);
184 Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
185 PhysicalSwitchAugmentation existingDstPsAugmentation =
186 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
187 if (OPERATIONAL == logicalDatastoreType) {
188 psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
189 srcPsAugmenatation, dstPsPath);
190 psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
192 psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
193 psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
195 mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
197 dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
198 Node dstPsNode = dstPsNodeBuilder.build();
199 tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
200 LOG.debug("Copied {} physical switch node from {} to {}", logicalDatastoreType, srcPsPath, dstPsPath);
203 public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
204 PhysicalSwitchAugmentationBuilder builder,
205 InstanceIdentifier<Node> haNodePath) {
206 builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
207 if (psAugmentation != null) {
208 builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
209 builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
210 builder.setTunnelIps(psAugmentation.getTunnelIps());
211 if (psAugmentation.getHwvtepNodeName() != null) {
212 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));