logging issues in elanmanager
[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     private static final 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     @Override
61     public void copyGlobalNode(Optional<Node> srcGlobalNodeOptional,
62                                InstanceIdentifier<Node> srcPath,
63                                InstanceIdentifier<Node> dstPath,
64                                LogicalDatastoreType logicalDatastoreType,
65                                ReadWriteTransaction tx) throws ReadFailedException {
66         if (!srcGlobalNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
67             Futures.addCallback(tx.read(logicalDatastoreType, srcPath), new FutureCallback<Optional<Node>>() {
68                 @Override
69                 public void onSuccess(Optional<Node> nodeOptional) {
70                     HAJobScheduler.getInstance().submitJob(() -> {
71                         try {
72                             ReadWriteTransaction tx1 = new BatchedTransaction();
73                             if (nodeOptional.isPresent()) {
74                                 copyGlobalNode(nodeOptional, srcPath, dstPath, logicalDatastoreType, tx1);
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 deleted.
79                                  * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener
80                                  * OnGLobalNode() delete function
81                                  * So we should delete the existing config child node as cleanup
82                                  */
83                                 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPath);
84                             }
85                         } catch (ReadFailedException e) {
86                             LOG.error("Failed to read source node {}",srcPath);
87                         }
88                     });
89                 }
90
91                 @Override
92                 public void onFailure(Throwable throwable) {
93                 }
94             });
95             return;
96         }
97         HwvtepGlobalAugmentation srcGlobalAugmentation =
98                 srcGlobalNodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class);
99         if (srcGlobalAugmentation == null) {
100             /**
101              * If Source HA Global Node is not present
102              * It means that both the child are disconnected/removed hence the parent is deleted.
103              * @see org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpNodeListener OnGLobalNode() delete function
104              * So we should delete the existing config child node as cleanup
105              */
106             HwvtepHAUtil.deleteNodeIfPresent(tx, logicalDatastoreType, dstPath);
107             return;
108         }
109         NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPath);
110         HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
111
112         Optional<Node> existingDstGlobalNodeOptional = tx.read(logicalDatastoreType, dstPath).checkedGet();
113         Node existingDstGlobalNode =
114                 existingDstGlobalNodeOptional.isPresent() ? existingDstGlobalNodeOptional.get() : null;
115         HwvtepGlobalAugmentation existingHAGlobalData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstGlobalNode);
116
117         globalAugmentationMerger.mergeOperationalData(haBuilder, existingHAGlobalData, srcGlobalAugmentation, dstPath);
118         globalNodeMerger.mergeOperationalData(haNodeBuilder,
119                 existingDstGlobalNode, srcGlobalNodeOptional.get(), dstPath);
120
121
122         if (OPERATIONAL == logicalDatastoreType) {
123             haBuilder.setManagers(HwvtepHAUtil.buildManagersForHANode(srcGlobalNodeOptional.get(),
124                     existingDstGlobalNodeOptional));
125             //Also update the manager section in config which helps in cluster reboot scenarios
126             haBuilder.getManagers().stream().forEach((manager) -> {
127                 InstanceIdentifier<Managers> managerIid = dstPath.augmentation(HwvtepGlobalAugmentation.class)
128                         .child(Managers.class, manager.getKey());
129                 tx.put(CONFIGURATION, managerIid, manager, true);
130             });
131
132         }
133         haBuilder.setDbVersion(srcGlobalAugmentation.getDbVersion());
134         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
135         Node haNode = haNodeBuilder.build();
136         if (OPERATIONAL == logicalDatastoreType) {
137             tx.merge(logicalDatastoreType, dstPath, haNode, true);
138         } else {
139             tx.put(logicalDatastoreType, dstPath, haNode, true);
140         }
141     }
142
143     @Override
144     public void copyPSNode(Optional<Node> srcPsNodeOptional,
145                            InstanceIdentifier<Node> srcPsPath,
146                            InstanceIdentifier<Node> dstPsPath,
147                            InstanceIdentifier<Node> dstGlobalPath,
148                            LogicalDatastoreType logicalDatastoreType,
149                            ReadWriteTransaction tx) throws ReadFailedException {
150         if (!srcPsNodeOptional.isPresent() && logicalDatastoreType == CONFIGURATION) {
151             Futures.addCallback(tx.read(logicalDatastoreType, srcPsPath), new FutureCallback<Optional<Node>>() {
152                 @Override
153                 public void onSuccess(Optional<Node> nodeOptional) {
154                     HAJobScheduler.getInstance().submitJob(() -> {
155                         try {
156                             ReadWriteTransaction tx1 = new BatchedTransaction();
157                             if (nodeOptional.isPresent()) {
158                                 copyPSNode(nodeOptional,
159                                         srcPsPath, dstPsPath, dstGlobalPath, logicalDatastoreType, tx1);
160                             } else {
161                                 /**
162                                  * Deleting node please refer @see #copyGlobalNode for explanation
163                                  */
164                                 HwvtepHAUtil.deleteNodeIfPresent(tx1, logicalDatastoreType, dstPsPath);
165                             }
166                         } catch (ReadFailedException e) {
167                             LOG.error("Failed to read src node {}", srcPsNodeOptional.get());
168                         }
169                     });
170                 }
171
172                 @Override
173                 public void onFailure(Throwable throwable) {
174                 }
175             });
176             return;
177         }
178         NodeBuilder dstPsNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(dstPsPath);
179         PhysicalSwitchAugmentationBuilder dstPsAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
180
181         PhysicalSwitchAugmentation srcPsAugmenatation =
182                 srcPsNodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class);
183
184         Node existingDstPsNode = HwvtepHAUtil.readNode(tx, logicalDatastoreType, dstPsPath);
185         PhysicalSwitchAugmentation existingDstPsAugmentation =
186                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingDstPsNode);
187         if (OPERATIONAL == logicalDatastoreType) {
188             psAugmentationMerger.mergeOperationalData(dstPsAugmentationBuilder, existingDstPsAugmentation,
189                     srcPsAugmenatation, dstPsPath);
190             psNodeMerger.mergeOperationalData(dstPsNodeBuilder, existingDstPsNode, srcPsNodeOptional.get(), dstPsPath);
191         } else {
192             psAugmentationMerger.mergeConfigData(dstPsAugmentationBuilder, srcPsAugmenatation, dstPsPath);
193             psNodeMerger.mergeConfigData(dstPsNodeBuilder, srcPsNodeOptional.get(), dstPsPath);
194         }
195         mergeOpManagedByAttributes(srcPsAugmenatation, dstPsAugmentationBuilder, dstGlobalPath);
196
197         dstPsNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstPsAugmentationBuilder.build());
198         Node dstPsNode = dstPsNodeBuilder.build();
199         tx.merge(logicalDatastoreType, dstPsPath, dstPsNode, true);
200         LOG.debug("Copied {} physical switch node from {} to {}", logicalDatastoreType, srcPsPath, dstPsPath);
201     }
202
203     public void mergeOpManagedByAttributes(PhysicalSwitchAugmentation psAugmentation,
204                                            PhysicalSwitchAugmentationBuilder builder,
205                                            InstanceIdentifier<Node> haNodePath) {
206         builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
207         if (psAugmentation != null) {
208             builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
209             builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
210             builder.setTunnelIps(psAugmentation.getTunnelIps());
211             if (psAugmentation.getHwvtepNodeName() != null) {
212                 builder.setPhysicalSwitchUuid(HwvtepHAUtil.getUUid(psAugmentation.getHwvtepNodeName().getValue()));
213             }
214         }
215     }
216 }