2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
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;
27 * Utility class to retrieve the unmet dependencies (config/operational) of the given object.
29 public abstract class UnMetDependencyGetter<T extends Identifiable> {
31 private final ConfigDependencyGetter configDependencyGetter = new ConfigDependencyGetter();
32 private final InTransitDependencyGetter inTransitDependencyGetter = new InTransitDependencyGetter();
35 * Returns the iids this data depends upon
36 * which are already intransit in the previous transaction if any.
38 * @param opState The operatonal state
39 * @param data The data object
40 * @return The depenencies
42 public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getInTransitDependencies(
43 HwvtepOperationalState opState, T data) {
44 return inTransitDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
48 * Returns the iids this data depends upon
49 * which are not yet present in the config data store if any.
51 * @param opState The operatonal state
52 * @param data The data object
53 * @return the depenencies
55 public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getUnMetConfigDependencies(
56 HwvtepOperationalState opState, T data) {
57 return configDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
60 abstract class DependencyGetter {
62 Map<Class<? extends Identifiable>, List<InstanceIdentifier>> retrieveUnMetDependencies(
63 HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo, T data) {
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));
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);
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) {
86 result = new HashMap<>();
88 if (!result.containsKey(cls)) {
89 result.put(cls, new ArrayList<>());
91 result.get(cls).add(key);
95 abstract boolean isDependencyMet(HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo,
96 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key);
99 class ConfigDependencyGetter extends DependencyGetter {
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);
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());
119 class InTransitDependencyGetter extends DependencyGetter {
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);
127 public abstract List<InstanceIdentifier<?>> getLogicalSwitchDependencies(T data);
129 public abstract List<InstanceIdentifier<?>> getTerminationPointDependencies(T data);