make reconciliation the first transaction
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / configuration / HwvtepReconciliationTask.java
1 /*
2  * Copyright (c) 2016, 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.ovsdb.hwvtepsouthbound.reconciliation.configuration;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
12 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
13 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionManager;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationManager;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationTask;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.HwvtepOperationalState;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommandAggregator;
20 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30
31 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
32 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
33
34 public class HwvtepReconciliationTask extends ReconciliationTask {
35
36     private static final Logger LOG = LoggerFactory.getLogger(HwvtepReconciliationTask.class);
37     private HwvtepConnectionInstance connectionInstance;
38     private DataBroker db;
39     private Node psNode;
40     private MdsalUtils mdsalUtils;
41
42     public HwvtepReconciliationTask(ReconciliationManager reconciliationManager,
43                                     HwvtepConnectionManager connectionManager,
44                                     InstanceIdentifier<?> nodeId,
45                                     Node psNode,
46                                     HwvtepConnectionInstance connectionInstance,
47                                     DataBroker db) {
48         super(reconciliationManager, connectionManager, nodeId, null);
49         this.db = db;
50         this.psNode = psNode;
51         this.connectionInstance = connectionInstance;
52         this.mdsalUtils = new MdsalUtils(db);
53     }
54
55     private void transactChangesToDevice(final Collection<DataTreeModification<Node>> changes,
56                                          final Node globalOperNode,
57                                          final Node psNode) {
58         HwvtepOperationalState hwvtepOperationalState = new HwvtepOperationalState(db, connectionInstance, changes,
59                 globalOperNode, psNode);
60         hwvtepOperationalState.setInReconciliation(true);
61         boolean reconcile = true;
62         connectionInstance.transact(new TransactCommandAggregator(hwvtepOperationalState,changes), reconcile);
63     }
64
65     @Override
66     public boolean reconcileConfiguration(HwvtepConnectionManager connectionManagerOfDevice) {
67         InstanceIdentifier<Node> psNodeIid = HwvtepSouthboundMapper.createInstanceIdentifier(psNode.getNodeId());
68         InstanceIdentifier<Node> nodeId = (InstanceIdentifier<Node>)nodeIid;
69         ReadOnlyTransaction tx = reconciliationManager.getDb().newReadOnlyTransaction();
70
71         Node globalConfigNode = mdsalUtils.read(CONFIGURATION, nodeId);
72         Node globalOpNode = mdsalUtils.read(OPERATIONAL, nodeId);
73         Node psConfigNode = mdsalUtils.read(CONFIGURATION, psNodeIid);
74
75         DataTreeModification<Node> change = null;
76         Collection<DataTreeModification<Node>> changes = new ArrayList<>();
77         change = GlobalConfigOperationalChangeGetter.getModification(nodeId, globalConfigNode, globalOpNode);
78         changes.add(change);
79
80         change = SwitchConfigOperationalChangeGetter.getModification(psNodeIid, psConfigNode, psNode);
81         changes.add(change);
82
83         if (globalConfigNode != null) {
84             HwvtepGlobalAugmentation augmentation = globalConfigNode.getAugmentation(HwvtepGlobalAugmentation.class);
85             if (augmentation != null) {
86                 if (augmentation.getLogicalSwitches() != null) {
87                     for (LogicalSwitches logicalSwitches : augmentation.getLogicalSwitches()) {
88                         connectionInstance.getDeviceInfo().updateConfigData(LogicalSwitches.class,
89                                 nodeId.augmentation(HwvtepGlobalAugmentation.class).child(LogicalSwitches.class,
90                                         logicalSwitches.getKey()), logicalSwitches);
91                     }
92                 }
93             }
94         }
95         transactChangesToDevice(changes, globalOpNode, psNode);
96         return true;
97     }
98
99     @Override
100     public void doRetry(boolean wasPreviousAttemptSuccessful) {
101     }
102
103     @Override
104     public void checkReadinessAndProcess() {
105     }
106
107     @Override
108     public long retryDelayInMills() {
109         return 0;
110     }
111
112 }