NETVIRT-1630 migrate to md-sal APIs
[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.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.mdsal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
12
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.genius.infra.Datastore;
21 import org.opendaylight.genius.infra.Datastore.Configuration;
22 import org.opendaylight.genius.infra.Datastore.Operational;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
25 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
26 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
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(() -> LoggingFutures.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
117         if (Operational.class.equals(datastoreType)) {
118             globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation,
119                     dstPath);
120             globalNodeMerger.mergeOperationalData(haNodeBuilder,
121                     existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
122             haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
123                     existingDstGlobalNodeOptional));
124             //Also update the manager section in config which helps in cluster reboot scenarios
125             LoggingFutures.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         } else {
134             globalAugmentationMerger.mergeConfigData(haBuilder, srcGlobalAugmentation, dstPath);
135             globalNodeMerger.mergeConfigData(haNodeBuilder, srcGlobalNodeOptional.get(), dstPath);
136         }
137
138         haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
139         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
140         Node haNode = haNodeBuilder.build();
141         if (Operational.class.equals(datastoreType)) {
142             tx.merge(dstPath, haNode, CREATE_MISSING_PARENTS);
143         } else {
144             tx.put(dstPath, haNode, CREATE_MISSING_PARENTS);
145         }
146     }
147
148     public <D extends Datastore> void copyPSNode(Optional<Node> srcPsNodeOptional,
149                            InstanceIdentifier<Node> srcPsPath,
150                            InstanceIdentifier<Node> dstPsPath,
151                            InstanceIdentifier<Node> dstGlobalPath,
152                            Class<D> datastoreType,
153                            TypedReadWriteTransaction<D> tx)
154             throws ExecutionException, InterruptedException {
155         if (!srcPsNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
156             Futures.addCallback(tx.read(srcPsPath), new FutureCallback<Optional<Node>>() {
157                 @Override
158                 public void onSuccess(Optional<Node> nodeOptional) {
159                     HAJobScheduler.getInstance().submitJob(() -> {
160                         LoggingFutures.addErrorLogging(
161                             txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
162                                 if (nodeOptional.isPresent()) {
163                                     copyPSNode(nodeOptional,
164                                         srcPsPath, dstPsPath, dstGlobalPath, datastoreType, tx);
165                                 } else {
166                                     /*
167                                      * Deleting node please refer @see #copyGlobalNode for explanation
168                                      */
169                                     HwvtepHAUtil.deleteNodeIfPresent(tx, dstPsPath);
170                                 }
171                             }), LOG, "Failed to read source node {}", srcPsPath);
172                     });
173                 }
174
175                 @Override
176                 public void onFailure(Throwable throwable) {
177                 }
178             }, MoreExecutors.directExecutor());
179             return;
180         }
181         NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
182         PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
183
184         PhysicalSwitchAugmentation srcPsAugmenatation =
185                 srcPsNodeOptional.get().augmentation(PhysicalSwitchAugmentation.class);
186
187         Node existingDstPsNode = tx.read(dstPsPath).get().orElse(null);
188         PhysicalSwitchAugmentation existingDstPsAugmentation =
189                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
190         if (Operational.class.equals(datastoreType)) {
191             psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
192                     srcPsAugmenatation, dstPsPath);
193             psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
194         } else {
195             psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
196             psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
197         }
198         mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
199
200         dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
201         Node dstPsNode = dstPsNodeBuilder.build();
202         tx.merge(dstPsPath, dstPsNode, CREATE_MISSING_PARENTS);
203         LOG.debug("Copied {} physical switch node from {} to {}", datastoreType, srcPsPath, dstPsPath);
204     }
205
206     public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
207                                            PhysicalSwitchAugmentationBuilder builder,
208                                            InstanceIdentifier<Node> haNodePath) {
209         builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
210         if (psAugmentation != null) {
211             builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
212             builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
213             builder.setTunnelIps(psAugmentation.getTunnelIps());
214             if (psAugmentation.getHwvtepNodeName() != null) {
215                 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));
216             }
217         }
218     }
219 }