MRI version bumpup for Aluminium
[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
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.genius.infra.Datastore;
20 import org.opendaylight.genius.infra.Datastore.Configuration;
21 import org.opendaylight.genius.infra.Datastore.Operational;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
24 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
25 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
28 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAJobScheduler;
29 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
30 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalNodeMerger;
31 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSAugmentationMerger;
32 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSNodeMerger;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 @Singleton
46 public class NodeCopier {
47
48     private static final Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
49
50     private final GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
51     private final PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
52     private final GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
53     private final PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
54     private final ManagedNewTransactionRunner txRunner;
55
56     @Inject
57     public NodeCopier(DataBroker db) {
58         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
59     }
60
61     public <D extends Datastore> void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
62                                InstanceIdentifier<Node> srcPath,
63                                InstanceIdentifier<Node> dstPath,
64                                Class<D> datastoreType,
65                                TypedReadWriteTransaction<D> tx)
66             throws ExecutionException, InterruptedException {
67         if (!srcGlobalNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
68             Futures.addCallback(tx.read(srcPath), new FutureCallback<Optional<Node>>() {
69                 @Override
70                 public void onSuccess(Optional<Node> nodeOptional) {
71                     HAJobScheduler.getInstance().submitJob(() -> LoggingFutures.addErrorLogging(
72                         txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
73                             if (nodeOptional.isPresent()) {
74                                 copyGlobalNode(nodeOptional, srcPath, dstPath, datastoreType, tx);
75                             } else {
76                                 /*
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
79                                  * deleted.
80                                  * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
81                                  * OnGLobalNode() delete function
82                                  * So we should delete the existing config child node as cleanup
83                                  */
84                                 HwvtepHAUtil.deleteNodeIfPresent(tx, dstPath);
85                             }
86                         }), LOG, "Failed to read source node {}", srcPath));
87                 }
88
89                 @Override
90                 public void onFailure(Throwable throwable) {
91                 }
92             }, MoreExecutors.directExecutor());
93             return;
94         }
95         HwvtepGlobalAugmentation srcGlobalAugmentation =
96                 srcGlobalNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
97         if (srcGlobalAugmentation == null) {
98             /*
99              * If Source HA Global Node is not present
100              * It means that both the child are disconnected/removed hence the parent is deleted.
101              * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
102              * So we should delete the existing config child node as cleanup
103              */
104             HwvtepHAUtil.deleteNodeIfPresent(tx, dstPath);
105             return;
106         }
107         NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
108         HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
109
110         Optional<Node> existingDstGlobalNodeOptional = tx.read(dstPath).get();
111         Node existingDstGlobalNode =
112                 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
113         HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
114
115
116         if (Operational.class.equals(datastoreType)) {
117             globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation,
118                     dstPath);
119             globalNodeMerger.mergeOperationalData(haNodeBuilder,
120                     existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
121             haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
122                     existingDstGlobalNodeOptional));
123             //Also update the manager section in config which helps in cluster reboot scenarios
124             LoggingFutures.addErrorLogging(
125                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
126                     confTx -> haBuilder.getManagers().values().forEach(manager -> {
127                         InstanceIdentifier<Managers> managerIid =
128                             dstPath.augmentation(HwvtepGlobalAugmentation.class).child(Managers.class, manager.key());
129                         confTx.mergeParentStructurePut(managerIid, manager);
130                     })), LOG, "Error updating the manager section in config");
131
132         } else {
133             globalAugmentationMerger.mergeConfigData(haBuilder, srcGlobalAugmentation, dstPath);
134             globalNodeMerger.mergeConfigData(haNodeBuilder, srcGlobalNodeOptional.get(), dstPath);
135         }
136
137         haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
138         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
139         Node haNode = haNodeBuilder.build();
140         if (Operational.class.equals(datastoreType)) {
141             tx.mergeParentStructureMerge(dstPath, haNode);
142         } else {
143             tx.mergeParentStructurePut(dstPath, haNode);
144         }
145     }
146
147     public <D extends Datastore> void copyPSNode(Optional<Node> srcPsNodeOptional,
148                            InstanceIdentifier<Node> srcPsPath,
149                            InstanceIdentifier<Node> dstPsPath,
150                            InstanceIdentifier<Node> dstGlobalPath,
151                            Class<D> datastoreType,
152                            TypedReadWriteTransaction<D> tx)
153             throws ExecutionException, InterruptedException {
154         if (!srcPsNodeOptional.isPresent() && Configuration.class.equals(datastoreType)) {
155             Futures.addCallback(tx.read(srcPsPath), new FutureCallback<Optional<Node>>() {
156                 @Override
157                 public void onSuccess(Optional<Node> nodeOptional) {
158                     HAJobScheduler.getInstance().submitJob(() -> {
159                         LoggingFutures.addErrorLogging(
160                             txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
161                                 if (nodeOptional.isPresent()) {
162                                     copyPSNode(nodeOptional,
163                                         srcPsPath, dstPsPath, dstGlobalPath, datastoreType, tx);
164                                 } else {
165                                     /*
166                                      * Deleting node please refer @see #copyGlobalNode for explanation
167                                      */
168                                     HwvtepHAUtil.deleteNodeIfPresent(tx, dstPsPath);
169                                 }
170                             }), LOG, "Failed to read source node {}", srcPsPath);
171                     });
172                 }
173
174                 @Override
175                 public void onFailure(Throwable throwable) {
176                 }
177             }, MoreExecutors.directExecutor());
178             return;
179         }
180         NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
181         PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
182
183         PhysicalSwitchAugmentation srcPsAugmenatation =
184                 srcPsNodeOptional.get().augmentation(PhysicalSwitchAugmentation.class);
185
186         Node existingDstPsNode = tx.read(dstPsPath).get().orElse(null);
187         PhysicalSwitchAugmentation existingDstPsAugmentation =
188                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
189         if (Operational.class.equals(datastoreType)) {
190             psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
191                     srcPsAugmenatation, dstPsPath);
192             psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
193         } else {
194             psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
195             psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
196         }
197         mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
198
199         dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
200         Node dstPsNode = dstPsNodeBuilder.build();
201         tx.mergeParentStructureMerge(dstPsPath, dstPsNode);
202         LOG.debug("Copied {} physical switch node from {} to {}", datastoreType, srcPsPath, dstPsPath);
203     }
204
205     public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
206                                            PhysicalSwitchAugmentationBuilder builder,
207                                            InstanceIdentifier<Node> haNodePath) {
208         builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
209         if (psAugmentation != null) {
210             builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
211             builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
212             builder.setTunnelIps(psAugmentation.getTunnelIps());
213             if (psAugmentation.getHwvtepNodeName() != null) {
214                 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));
215             }
216         }
217     }
218 }