bug 6579 added dependency queue
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / DependentJob.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
12 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
13 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.Identifiable;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17
18 import java.util.List;
19 import java.util.Map;
20
21 public abstract class DependentJob<T extends Identifiable> {
22
23     private final long expiryTime;
24     private final InstanceIdentifier key;
25     private final T data;
26     private final Map<Class<? extends DataObject>, List<InstanceIdentifier>> dependencies;
27
28     DependentJob(InstanceIdentifier key,
29                            T data, Map<Class<? extends DataObject>, List<InstanceIdentifier>> dependencies) {
30         this.expiryTime = System.currentTimeMillis() + HwvtepSouthboundConstants.WAITING_JOB_EXPIRY_TIME_MILLIS;
31         this.key = key;
32         this.data = data;
33         this.dependencies = dependencies;
34     }
35
36     /**
37      * This call back method gets called when all its dependencies are resolved
38      * @param operationalState   new current operational state
39      * @param transactionBuilder transaction builder to create device transaction
40      */
41     protected abstract void onDependencyResolved(HwvtepOperationalState operationalState,
42                                                  TransactionBuilder transactionBuilder);
43
44     /**
45      * This method is to check if all the given dependency of this job or not
46      * @param deviceInfo   The device info of tis job
47      * @param cls          dependency type to be checked for
48      * @param iid          instance identifier to be checked for
49      * @return true if the dependency is met
50      */
51     protected abstract boolean isDependencyMet(HwvtepDeviceInfo deviceInfo, Class<? extends DataObject> cls,
52                                                InstanceIdentifier iid);
53
54     boolean isExpired(long currentTime) {
55         return currentTime > expiryTime;
56     }
57
58     /**
59      * This method checks if all the dependencies of this job or met or not
60      * @param deviceInfo The device info of this job
61      * @return true if all the dependencies are met
62      */
63     boolean areDependenciesMet(HwvtepDeviceInfo deviceInfo) {
64         for (Class<? extends DataObject> cls : dependencies.keySet()) {
65             for (InstanceIdentifier iid : dependencies.get(cls)) {
66                 if (!isDependencyMet(deviceInfo, cls, iid)) {
67                     return false;
68                 }
69             }
70         }
71         return true;
72     }
73
74     public InstanceIdentifier getKey() {
75         return key;
76     }
77
78     public T getData() {
79         return data;
80     }
81
82     public abstract static class ConfigWaitingJob<T extends Identifiable> extends DependentJob {
83
84         public ConfigWaitingJob(InstanceIdentifier key, T data, Map dependencies) {
85             super(key, data, dependencies);
86         }
87
88         @Override
89         protected boolean isDependencyMet(HwvtepDeviceInfo deviceInfo, Class cls, InstanceIdentifier iid) {
90             return deviceInfo.isConfigDataAvailable(cls, iid);
91         }
92     }
93
94     public abstract static class OpWaitingJob<T extends Identifiable> extends DependentJob {
95
96         public OpWaitingJob(InstanceIdentifier key, T data, Map dependencies) {
97             super(key, data, dependencies);
98         }
99
100         @Override
101         protected boolean isDependencyMet(HwvtepDeviceInfo deviceInfo, Class cls, InstanceIdentifier iid) {
102             HwvtepDeviceInfo.DeviceData deviceData = deviceInfo.getDeviceOpData(cls, iid);
103             return deviceData == null || deviceData.getStatus() != HwvtepDeviceInfo.DeviceDataStatus.IN_TRANSIT;
104         }
105     }
106 }