b046f8a4b5bc819dd37df4dd4a82494167041b31
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UnMetDependencyGetter.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 com.google.common.base.Optional;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.Identifiable;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 /**
31  * Utility class to retrieve the unmet dependencies (config/operational) of the given object.
32  */
33 public abstract class UnMetDependencyGetter<T extends Identifiable> {
34
35     private final ConfigDependencyGetter configDependencyGetter = new ConfigDependencyGetter();
36     private final InTransitDependencyGetter inTransitDependencyGetter = new InTransitDependencyGetter();
37
38     /**
39      * Returns the iids this data depends upon
40      * which are already intransit in the previous transaction if any.
41      *
42      * @param opState The operatonal state
43      * @param data The data object
44      * @return The depenencies
45      */
46     public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getInTransitDependencies(
47             HwvtepOperationalState opState, T data) {
48         return inTransitDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
49     }
50
51     /**
52      * Returns the iids this data depends upon
53      * which are not yet present in the config data store if any.
54      *
55      * @param opState The operatonal state
56      * @param data The data object
57      * @return the      depenencies
58      */
59     public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getUnMetConfigDependencies(
60             HwvtepOperationalState opState, T data) {
61         return configDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
62     }
63
64     abstract class DependencyGetter {
65
66         Map<Class<? extends Identifiable>, List<InstanceIdentifier>> retrieveUnMetDependencies(
67                 HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo, T data) {
68
69             Map<Class<? extends Identifiable>, List<InstanceIdentifier>> result = new HashMap<>();
70             Map<Class<? extends Identifiable>, List<InstanceIdentifier<?>>> allKeys = new HashMap<>();
71             allKeys.put(LogicalSwitches.class, getLogicalSwitchDependencies(data));
72             allKeys.put(TerminationPoint.class, getTerminationPointDependencies(data));
73
74             for (Entry<Class<? extends Identifiable>, List<InstanceIdentifier<?>>> entry : allKeys.entrySet()) {
75                 Class<? extends Identifiable> cls = entry.getKey();
76                 List<InstanceIdentifier<? extends DataObject>> keysToCheck = entry.getValue();
77                 for (InstanceIdentifier<? extends DataObject> key : keysToCheck) {
78                     if (!isDependencyMet(opState, deviceInfo, cls, key)) {
79                         result = addToResultMap(result, cls, key);
80                     }
81                 }
82             }
83             return result;
84         }
85
86         Map<Class<? extends Identifiable>, List<InstanceIdentifier>> addToResultMap(
87                 Map<Class<? extends Identifiable>, List<InstanceIdentifier>> result,
88                 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key) {
89             if (null == result) {
90                 result = new HashMap<>();
91             }
92             if (!result.containsKey(cls)) {
93                 result.put(cls, new ArrayList<>());
94             }
95             result.get(cls).add(key);
96             return result;
97         }
98
99         abstract boolean isDependencyMet(HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo,
100                 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key);
101     }
102
103     class ConfigDependencyGetter extends DependencyGetter {
104         @Override
105         boolean isDependencyMet(HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo,
106                                 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key) {
107             return deviceInfo.isConfigDataAvailable(cls, key) || isConfigDataAvailable(opState, cls, key);
108         }
109
110         boolean isConfigDataAvailable(HwvtepOperationalState opState,
111                                       Class<? extends Identifiable> cls,
112                                       InstanceIdentifier<? extends DataObject> key) {
113             DataBroker db = opState.getConnectionInstance().getDataBroker();
114             ReadOnlyTransaction tx = db.newReadOnlyTransaction();
115             Optional data = HwvtepSouthboundUtil.readNode(tx, LogicalDatastoreType.CONFIGURATION, key);
116             tx.close();
117             if (data.isPresent()) {
118                 opState.getDeviceInfo().updateConfigData(cls, key, data.get());
119                 return true;
120             }
121             return false;
122         }
123     }
124
125     class InTransitDependencyGetter extends DependencyGetter {
126         @Override
127         boolean isDependencyMet(HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo,
128                 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key) {
129             return opState.isKeyPartOfCurrentTx(cls, key) || !deviceInfo.isKeyInTransit(cls, key);
130         }
131     }
132
133     public abstract List<InstanceIdentifier<?>> getLogicalSwitchDependencies(T data);
134
135     public abstract List<InstanceIdentifier<?>> getTerminationPointDependencies(T data);
136 }