Remove redundant names in paths
[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.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
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 javax.inject.Inject;
17 import javax.inject.Singleton;
18
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 @Singleton
43 public class NodeCopier implements INodeCopier {
44
45     static Logger LOG = LoggerFactory.getLogger(NodeCopier.class);
46
47     GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
48     PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
49     GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
50     PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
51     DataBroker db;
52     HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
53
54     @Inject
55     public NodeCopier(DataBroker db) {
56         this.db = db;
57     }
58
59     public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
60                         InstanceIdentifier<Node> srcPath,
61                         InstanceIdentifier<Node> dstPath,
62                         LogicalDatastoreType logicalDatastoreType,
63                         ReadWriteTransaction tx) throws ReadFailedException {
64         if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
65             Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
66                 @Override
67                 public void onSuccess(Optional<Node> nodeOptional) {
68                     HAJobScheduler.getInstance().submitJob(() -> {
69                         try {
70                             ReadWriteTransaction tx1 = new BatchedTransaction();
71                             if (nodeOptional.isPresent()) {
72                                 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
73                             } else {
74                                 /**
75                                  * In case the Parent HA Global Node is not present and Child HA node is present
76                                  * It means that both the child are disconnected/removed hence the parent is deleted.
77                                  * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
78                                  * OnGLobalNode() delete function
79                                  * So we should delete the existing config child node as cleanup
80                                  */
81                                 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
82                             }
83                         } catch (ReadFailedException e) {
84                             LOG.error("Failed to read source node {}",srcPath);
85                         }
86                     });
87                 }
88
89                 @Override
90                 public void onFailure(Throwable throwable) {
91                 }
92             });
93             return;
94         }
95         HwvtepGlobalAugmentation srcGlobalAugmentation =
96                 srcGlobalNodeOptional.get().getAugmentation(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, logicalDatastoreType, dstPath);
105             return;
106         }
107         NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
108         HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
109
110         Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
111         Node existingDstGlobalNode =
112                 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
113         HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
114
115         globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
116         globalNodeMerger.mergeOperationalData(haNodeBuilder,
117                 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
118
119
120         if (OPERATIONAL == logicalDatastoreType) {
121             haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
122                     existingDstGlobalNodeOptional));
123         }
124         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
125         Node haNode = haNodeBuilder.build();
126         tx.merge(logicalDatastoreType, dstPath, haNode, true);
127     }
128
129     public void copyPSNode(Optional<Node> srcPsNodeOptional,
130                            InstanceIdentifier<Node> srcPsPath,
131                            InstanceIdentifier<Node> dstPsPath,
132                            InstanceIdentifier<Node> dstGlobalPath,
133                            LogicalDatastoreType logicalDatastoreType,
134                            ReadWriteTransaction tx) throws ReadFailedException {
135         if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
136             Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
137                 @Override
138                 public void onSuccess(Optional<Node> nodeOptional) {
139                     HAJobScheduler.getInstance().submitJob(() -> {
140                         try {
141                             ReadWriteTransaction tx1 = new BatchedTransaction();
142                             if (nodeOptional.isPresent()) {
143                                 copyPSNode(nodeOptional,
144                                         srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
145                             } else {
146                                 /**
147                                  * Deleting node please refer @see #copyGlobalNode for explanation
148                                  */
149                                 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
150                             }
151                         } catch (ReadFailedException e) {
152                             LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
153                         }
154                     });
155                 }
156
157                 @Override
158                 public void onFailure(Throwable throwable) {
159                 }
160             });
161             return;
162         }
163         NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
164         PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
165
166         PhysicalSwitchAugmentation srcPsAugmenatation =
167                 srcPsNodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class);
168
169         Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
170         PhysicalSwitchAugmentation existingDstPsAugmentation =
171                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
172         if (OPERATIONAL == logicalDatastoreType) {
173             psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
174                     srcPsAugmenatation, dstPsPath);
175             psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
176         } else {
177             psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
178             psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
179         }
180         mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
181
182         dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
183         Node dstPsNode = dstPsNodeBuilder.build();
184         tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
185     }
186
187     public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
188                                            PhysicalSwitchAugmentationBuilder builder,
189                                            InstanceIdentifier<Node> haNodePath) {
190         builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
191         builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
192         builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
193         builder.setTunnelIps(psAugmentation.getTunnelIps());
194         builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));
195     }
196 }