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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
11 import com.google.common.base.Optional;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
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;
31 * Utility class to retrieve the unmet dependencies (config/operational) of the given object.
33 public abstract class UnMetDependencyGetter<T extends Identifiable> {
35 private final ConfigDependencyGetter configDependencyGetter = new ConfigDependencyGetter();
36 private final InTransitDependencyGetter inTransitDependencyGetter = new InTransitDependencyGetter();
39 * Returns the iids this data depends upon
40 * which are already intransit in the previous transaction if any.
42 * @param opState The operatonal state
43 * @param data The data object
44 * @return The depenencies
46 public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getInTransitDependencies(
47 HwvtepOperationalState opState, T data) {
48 return inTransitDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
52 * Returns the iids this data depends upon
53 * which are not yet present in the config data store if any.
55 * @param opState The operatonal state
56 * @param data The data object
57 * @return the depenencies
59 public Map<Class<? extends Identifiable>, List<InstanceIdentifier>> getUnMetConfigDependencies(
60 HwvtepOperationalState opState, T data) {
61 return configDependencyGetter.retrieveUnMetDependencies(opState, opState.getDeviceInfo(), data);
64 abstract class DependencyGetter {
66 Map<Class<? extends Identifiable>, List<InstanceIdentifier>> retrieveUnMetDependencies(
67 HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo, T data) {
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));
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);
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) {
90 result = new HashMap<>();
92 if (!result.containsKey(cls)) {
93 result.put(cls, new ArrayList<>());
95 result.get(cls).add(key);
99 abstract boolean isDependencyMet(HwvtepOperationalState opState, HwvtepDeviceInfo deviceInfo,
100 Class<? extends Identifiable> cls, InstanceIdentifier<? extends DataObject> key);
103 class ConfigDependencyGetter extends DependencyGetter {
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);
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);
117 if (data.isPresent()) {
118 opState.getDeviceInfo().updateConfigData(cls, key, data.get());
125 class InTransitDependencyGetter extends DependencyGetter {
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);
133 public abstract List<InstanceIdentifier<?>> getLogicalSwitchDependencies(T data);
135 public abstract List<InstanceIdentifier<?>> getTerminationPointDependencies(T data);