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