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