daeb952b30748315622eb878c880f598f4b72a21
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / handlers / NodeCopier.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.l2gw.ha.handlers;
9
10 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
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;
45
46 @Singleton
47 public class NodeCopier {
48
49     private static final Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
50
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;
56
57     @Inject
58     public NodeCopier(DataBroker db) {
59         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
60     }
61
62     public <D extends Datastore> void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
63                                InstanceIdentifier<Node> srcPath,
64                                InstanceIdentifier<Node> dstPath,
65                                Class<D> datastoreType,
66                                TypedReadWriteTransaction<D> tx)
67             throws ExecutionException, InterruptedException {
68         if (!srcGlobalNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
69             Futures.addCallback(tx.read(srcPath), new FutureCallback<Optional<Node>>() {
70                 @Override
71                 public void onSuccess(Optional<Node> nodeOptional) {
72                     HAJobScheduler.getInstance().submitJob(() -> ListenableFutures.addErrorLogging(
73                         txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
74                             if (nodeOptional.isPresent()) {
75                                 copyGlobalNode(nodeOptional, srcPath, dstPath, datastoreType, tx);
76                             } else {
77                                 /*
78                                  * In case the Parent HA Global Node is not present and Child HA node is present
79                                  * It means that both the child are disconnected/removed hence the parent is
80                                  * deleted.
81                                  * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
82                                  * OnGLobalNode() delete function
83                                  * So we should delete the existing config child node as cleanup
84                                  */
85                                 HwvtepHAUtil.deleteNodeIfPresent(tx, dstPath);
86                             }
87                         }), LOG, "Failed to read source node {}", srcPath));
88                 }
89
90                 @Override
91                 public void onFailure(Throwable throwable) {
92                 }
93             }, MoreExecutors.directExecutor());
94             return;
95         }
96         HwvtepGlobalAugmentation srcGlobalAugmentation =
97                 srcGlobalNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
98         if (srcGlobalAugmentation == null) {
99             /*
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
104              */
105             HwvtepHAUtil.deleteNodeIfPresent(tx, dstPath);
106             return;
107         }
108         NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
109         HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
110
111         Optional<Node> existingDstGlobalNodeOptional = tx.read(dstPath).get();
112         Node existingDstGlobalNode =
113                 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
114         HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
115
116         globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
117         globalNodeMerger.mergeOperationalData(haNodeBuilder,
118                 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
119
120
121         if (Operational.class.equals(datastoreType)) {
122             haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
123                     existingDstGlobalNodeOptional));
124             //Also update the manager section in config which helps in cluster reboot scenarios
125             ListenableFutures.addErrorLogging(
126                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
127                     confTx -> haBuilder.getManagers().forEach(manager -> {
128                         InstanceIdentifier<Managers> managerIid =
129                             dstPath.augmentation(HwvtepGlobalAugmentation.class).child(Managers.class, manager.key());
130                         confTx.put(managerIid, manager, CREATE_MISSING_PARENTS);
131                     })), LOG, "Error updating the manager section in config");
132
133         }
134         haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
135         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
136         Node haNode = haNodeBuilder.build();
137         if (Operational.class.equals(datastoreType)) {
138             tx.merge(dstPath, haNode, CREATE_MISSING_PARENTS);
139         } else {
140             tx.put(dstPath, haNode, CREATE_MISSING_PARENTS);
141         }
142     }
143
144     public <D extends Datastore> void copyPSNode(Optional<Node> srcPsNodeOptional,
145                            InstanceIdentifier<Node> srcPsPath,
146                            InstanceIdentifier<Node> dstPsPath,
147                            InstanceIdentifier<Node> dstGlobalPath,
148                            Class<D> datastoreType,
149                            TypedReadWriteTransaction<D> tx)
150             throws ExecutionException, InterruptedException {
151         if (!srcPsNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
152             Futures.addCallback(tx.read(srcPsPath), new FutureCallback<Optional<Node>>() {
153                 @Override
154                 public void onSuccess(Optional<Node> nodeOptional) {
155                     HAJobScheduler.getInstance().submitJob(() -> {
156                         ListenableFutures.addErrorLogging(
157                             txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
158                                 if (nodeOptional.isPresent()) {
159                                     copyPSNode(nodeOptional,
160                                         srcPsPath, dstPsPath, dstGlobalPath, datastoreType, tx);
161                                 } else {
162                                     /*
163                                      * Deleting node please refer @see #copyGlobalNode for explanation
164                                      */
165                                     HwvtepHAUtil.deleteNodeIfPresent(tx, dstPsPath);
166                                 }
167                             }), LOG, "Failed to read source node {}", srcPsPath);
168                     });
169                 }
170
171                 @Override
172                 public void onFailure(Throwable throwable) {
173                 }
174             }, MoreExecutors.directExecutor());
175             return;
176         }
177         NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
178         PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
179
180         PhysicalSwitchAugmentation srcPsAugmenatation =
181                 srcPsNodeOptional.get().augmentation(PhysicalSwitchAugmentation.class);
182
183         Node existingDstPsNode = tx.read(dstPsPath).get().orNull();
184         PhysicalSwitchAugmentation existingDstPsAugmentation =
185                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
186         if (Operational.class.equals(datastoreType)) {
187             psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
188                     srcPsAugmenatation, dstPsPath);
189             psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
190         } else {
191             psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
192             psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
193         }
194         mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
195
196         dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
197         Node dstPsNode = dstPsNodeBuilder.build();
198         tx.merge(dstPsPath, dstPsNode, CREATE_MISSING_PARENTS);
199         LOG.debug("Copied {} physical switch node from {} to {}", datastoreType, srcPsPath, dstPsPath);
200     }
201
202     public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
203                                            PhysicalSwitchAugmentationBuilder builder,
204                                            InstanceIdentifier<Node> haNodePath) {
205         builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
206         if (psAugmentation != null) {
207             builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
208             builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
209             builder.setTunnelIps(psAugmentation.getTunnelIps());
210             if (psAugmentation.getHwvtepNodeName() != null) {
211                 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));
212             }
213         }
214     }
215 }