bug 6579 added dependency queue
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / AbstractTransactCommand.java
1 /*
2  * Copyright (c) 2015 China Telecom Beijing Research Institute 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
12 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
13 import org.opendaylight.ovsdb.lib.notation.UUID;
14 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
17 import org.opendaylight.yangtools.yang.binding.Identifiable;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 import java.util.Collection;
21 import java.util.Map;
22
23 public abstract class AbstractTransactCommand<T extends Identifiable> implements TransactCommand<T> {
24
25     private HwvtepOperationalState operationalState;
26     private Collection<DataTreeModification<Node>> changes;
27
28     protected AbstractTransactCommand() {
29         // NO OP
30     }
31
32     public AbstractTransactCommand(HwvtepOperationalState state, Collection<DataTreeModification<Node>> changes) {
33         this.operationalState = state;
34         this.changes = changes;
35     }
36
37     public HwvtepOperationalState getOperationalState() {
38         return operationalState;
39     }
40
41     public Collection<DataTreeModification<Node>> getChanges() {
42         return changes;
43     }
44
45     void updateCurrentTxData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid, Object data) {
46         operationalState.updateCurrentTxData(cls, key, uuid);
47         operationalState.getDeviceInfo().markKeyAsInTransit(cls, key);
48         operationalState.getDeviceInfo().updateConfigData(cls, key, data);
49     }
50
51     void processDependencies(UnMetDependencyGetter<T> unMetDependencyGetter,
52                              TransactionBuilder transaction,
53                              final InstanceIdentifier<Node> nodeIid,
54                              final InstanceIdentifier key,
55                              final T data) {
56
57         HwvtepDeviceInfo deviceInfo = operationalState.getDeviceInfo();
58         Map inTransitDependencies = unMetDependencyGetter.getInTransitDependencies(operationalState, data);
59         Map confingDependencies = unMetDependencyGetter.getUnMetConfigDependencies(operationalState, data);
60         //we can skip the config termination point dependency as we can create them in device as part of this tx
61         confingDependencies.remove(TerminationPoint.class);
62
63         if (confingDependencies.isEmpty() && inTransitDependencies.isEmpty()) {
64             doDeviceTransaction(transaction, nodeIid, data);
65         }
66         if (!confingDependencies.isEmpty()) {
67             DependentJob<T> configWaitingJob = new DependentJob.ConfigWaitingJob(
68                     key, data, confingDependencies) {
69
70                 @Override
71                 public void onDependencyResolved(HwvtepOperationalState operationalState,
72                                                  TransactionBuilder transactionBuilder) {
73                     AbstractTransactCommand.this.operationalState = operationalState;
74                     onConfigUpdate(transactionBuilder, nodeIid, data);
75                 }
76             };
77             deviceInfo.addJobToQueue(configWaitingJob);
78         }
79         if (inTransitDependencies.size() > 0) {
80             DependentJob<T> opWaitingJob = new DependentJob.OpWaitingJob(
81                     key, data, inTransitDependencies) {
82
83                 @Override
84                 public void onDependencyResolved(HwvtepOperationalState operationalState,
85                                                  TransactionBuilder transactionBuilder) {
86                     AbstractTransactCommand.this.operationalState = operationalState;
87                     onConfigUpdate(transactionBuilder, nodeIid, data);
88                 }
89             };
90             deviceInfo.addJobToQueue(opWaitingJob);
91         }
92     }
93
94     public void doDeviceTransaction(TransactionBuilder transaction, InstanceIdentifier<Node> nodeIid, T data) {
95         //tobe removed as part of refactoring patch
96     }
97
98     public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier<Node> nodeIid, T data) {
99         //tobe removed as part of refactoring patch
100     }
101 }