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