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.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
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 com.google.common.util.concurrent.MoreExecutors;
17 import java.util.concurrent.ExecutionException;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.genius.infra.Datastore;
22 import org.opendaylight.genius.infra.Datastore.Configuration;
23 import org.opendaylight.genius.infra.Datastore.Operational;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
26 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
27 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
28 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
29 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAJobScheduler;
30 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
31 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalNodeMerger;
32 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSAugmentationMerger;
33 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSNodeMerger;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 public class NodeCopier implements INodeCopier {
49 private static final Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
51 private final GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
52 private final PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
53 private final GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
54 private final PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
55 private final ManagedNewTransactionRunner txRunner;
58 public NodeCopier(DataBroker db) {
59 this.txRunner = new ManagedNewTransactionRunnerImpl(db);
63 public <D extends Datastore> void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
64 InstanceIdentifier<Node> srcPath,
65 InstanceIdentifier<Node> dstPath,
66 Class<D> datastoreType,
67 TypedReadWriteTransaction<D> tx)
68 throws ExecutionException, InterruptedException {
69 if (!srcGlobalNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
70 Futures.addCallback(tx.read(srcPath), new FutureCallback<Optional<Node>>() {
72 public void onSuccess(Optional<Node> nodeOptional) {
73 HAJobScheduler.getInstance().submitJob(() -> ListenableFutures.addErrorLogging(
74 txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
75 if (nodeOptional.isPresent()) {
76 copyGlobalNode(nodeOptional, srcPath, dstPath, datastoreType, tx);
79 * In case the Parent HA Global Node is not present and Child HA node is present
80 * It means that both the child are disconnected/removed hence the parent is
82 * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
83 * OnGLobalNode() delete function
84 * So we should delete the existing config child node as cleanup
86 HwvtepHAUtil.deleteNodeIfPresent(tx, dstPath);
88 }), LOG, "Failed to read source node {}", srcPath));
92 public void onFailure(Throwable throwable) {
94 }, MoreExecutors.directExecutor());
97 HwvtepGlobalAugmentation srcGlobalAugmentation =
98 srcGlobalNodeOptional.get().augmentation(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, dstPath);
109 NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
110 HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
112 Optional<Node> existingDstGlobalNodeOptional = tx.read(dstPath).get();
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.class.equals(datastoreType)) {
123 haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
124 existingDstGlobalNodeOptional));
125 //Also update the manager section in config which helps in cluster reboot scenarios
126 ListenableFutures.addErrorLogging(
127 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
128 confTx -> haBuilder.getManagers().forEach(manager -> {
129 InstanceIdentifier<Managers> managerIid =
130 dstPath.augmentation(HwvtepGlobalAugmentation.class).child(Managers.class, manager.key());
131 confTx.put(managerIid, manager, CREATE_MISSING_PARENTS);
132 })), LOG, "Error updating the manager section in config");
135 haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
136 haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
137 Node haNode = haNodeBuilder.build();
138 if (Operational.class.equals(datastoreType)) {
139 tx.merge(dstPath, haNode, CREATE_MISSING_PARENTS);
141 tx.put(dstPath, haNode, CREATE_MISSING_PARENTS);
146 public <D extends Datastore> void copyPSNode(Optional<Node> srcPsNodeOptional,
147 InstanceIdentifier<Node> srcPsPath,
148 InstanceIdentifier<Node> dstPsPath,
149 InstanceIdentifier<Node> dstGlobalPath,
150 Class<D> datastoreType,
151 TypedReadWriteTransaction<D> tx)
152 throws ExecutionException, InterruptedException {
153 if (!srcPsNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
154 Futures.addCallback(tx.read(srcPsPath), new FutureCallback<Optional<Node>>() {
156 public void onSuccess(Optional<Node> nodeOptional) {
157 HAJobScheduler.getInstance().submitJob(() -> {
158 ListenableFutures.addErrorLogging(
159 txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
160 if (nodeOptional.isPresent()) {
161 copyPSNode(nodeOptional,
162 srcPsPath, dstPsPath, dstGlobalPath, datastoreType, tx);
165 * Deleting node please refer @see #copyGlobalNode for explanation
167 HwvtepHAUtil.deleteNodeIfPresent(tx, dstPsPath);
169 }), LOG, "Failed to read source node {}", srcPsNodeOptional.get());
174 public void onFailure(Throwable throwable) {
179 NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
180 PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
182 PhysicalSwitchAugmentation srcPsAugmenatation =
183 srcPsNodeOptional.get().augmentation(PhysicalSwitchAugmentation.class);
185 Node existingDstPsNode = tx.read(dstPsPath).get().orNull();
186 PhysicalSwitchAugmentation existingDstPsAugmentation =
187 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
188 if (Operational.class.equals(datastoreType)) {
189 psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
190 srcPsAugmenatation, dstPsPath);
191 psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
193 psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
194 psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
196 mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
198 dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
199 Node dstPsNode = dstPsNodeBuilder.build();
200 tx.merge(dstPsPath, dstPsNode, CREATE_MISSING_PARENTS);
201 LOG.debug("Copied {} physical switch node from {} to {}", datastoreType, srcPsPath, dstPsPath);
204 public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
205 PhysicalSwitchAugmentationBuilder builder,
206 InstanceIdentifier<Node> haNodePath) {
207 builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
208 if (psAugmentation != null) {
209 builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
210 builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
211 builder.setTunnelIps(psAugmentation.getTunnelIps());
212 if (psAugmentation.getHwvtepNodeName() != null) {
213 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));