bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / AbstractTransactCommand.java
1 /*
2  * Copyright (c) 2015 China Telecom Beijing Research Institute 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 java.lang.reflect.ParameterizedType;
12 import java.lang.reflect.Type;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.Set;
23
24 import com.google.common.collect.Lists;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
28 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
33 import org.opendaylight.yangtools.yang.binding.Identifiable;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39 import org.opendaylight.yangtools.yang.binding.Identifiable;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public abstract class AbstractTransactCommand<T extends Identifiable, Aug extends Augmentation<Node>> implements TransactCommand<T> {
43
44     private HwvtepOperationalState operationalState;
45     private Collection<DataTreeModification<Node>> changes;
46
47     protected AbstractTransactCommand() {
48         // NO OP
49     }
50
51     public AbstractTransactCommand(HwvtepOperationalState state, Collection<DataTreeModification<Node>> changes) {
52         this.operationalState = state;
53         this.changes = changes;
54     }
55
56     public HwvtepOperationalState getOperationalState() {
57         return operationalState;
58     }
59
60     public Collection<DataTreeModification<Node>> getChanges() {
61         return changes;
62     }
63
64     void updateCurrentTxDeleteData(Class<? extends Identifiable> cls, InstanceIdentifier key, T data) {
65         operationalState.updateCurrentTxDeleteData(cls, key);
66         operationalState.getDeviceInfo().clearConfigData(cls, key);
67     }
68
69     void updateCurrentTxData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid, Object data) {
70         operationalState.updateCurrentTxData(cls, key, uuid);
71         operationalState.getDeviceInfo().markKeyAsInTransit(cls, key);
72         operationalState.getDeviceInfo().updateConfigData(cls, key, data);
73     }
74
75     void processDependencies(UnMetDependencyGetter<T> unMetDependencyGetter,
76                              TransactionBuilder transaction,
77                              final InstanceIdentifier<Node> nodeIid,
78                              final InstanceIdentifier key,
79                              final T data, final Object... extraData) {
80
81         HwvtepDeviceInfo deviceInfo = operationalState.getDeviceInfo();
82         Map inTransitDependencies = unMetDependencyGetter.getInTransitDependencies(operationalState, data);
83         Map confingDependencies = unMetDependencyGetter.getUnMetConfigDependencies(operationalState, data);
84         //we can skip the config termination point dependency as we can create them in device as part of this tx
85         confingDependencies.remove(TerminationPoint.class);
86
87         Type type = getClass().getGenericSuperclass();
88         Type classType = ((ParameterizedType)type).getActualTypeArguments()[0];
89
90         //If this key itself is in transit wait for the response of this key itself
91         if (deviceInfo.isKeyInTransit((Class<? extends Identifiable>) classType, key)) {
92             inTransitDependencies.put((Class<? extends Identifiable>) classType, Lists.newArrayList(key));
93         }
94
95         if (HwvtepSouthboundUtil.isEmptyMap(confingDependencies) && HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
96             doDeviceTransaction(transaction, nodeIid, data, key, extraData);
97             //TODO put proper uuid
98             updateCurrentTxData((Class<? extends Identifiable>) classType, key, new UUID("uuid"), data);
99         }
100         if (!HwvtepSouthboundUtil.isEmptyMap(confingDependencies)) {
101             DependentJob<T> configWaitingJob = new DependentJob.ConfigWaitingJob(
102                     key, data, confingDependencies) {
103
104                 @Override
105                 public void onDependencyResolved(HwvtepOperationalState operationalState,
106                                                  TransactionBuilder transactionBuilder) {
107                     AbstractTransactCommand.this.operationalState = operationalState;
108                     onConfigUpdate(transactionBuilder, nodeIid, data, key, extraData);
109                 }
110             };
111             deviceInfo.addJobToQueue(configWaitingJob);
112         }
113         if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
114
115             DependentJob<T> opWaitingJob = new DependentJob.OpWaitingJob(
116                     key, data, inTransitDependencies) {
117
118                 @Override
119                 public void onDependencyResolved(HwvtepOperationalState operationalState,
120                                                  TransactionBuilder transactionBuilder) {
121                     AbstractTransactCommand.this.operationalState = operationalState;
122                     onConfigUpdate(transactionBuilder, nodeIid, data, key, extraData);
123                 }
124             };
125             deviceInfo.addJobToQueue(opWaitingJob);
126         }
127     }
128
129     public void doDeviceTransaction(TransactionBuilder transaction, InstanceIdentifier<Node> nodeIid, T data,
130                                     InstanceIdentifier key, Object... extraData) {
131         //tobe removed as part of refactoring patch
132     }
133
134     public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier<Node> nodeIid, T data,
135                                InstanceIdentifier key, Object... extraData) {
136         //tobe removed as part of refactoring patch
137     }
138
139     protected Aug getAugmentation(Node node) {
140         if (node == null) {
141             return null;
142         }
143         ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
144         Class<? extends Augmentation<Node>> augType = (Class<? extends Augmentation<Node>>) parameterizedType.getActualTypeArguments()[1];
145         Augmentation<Node> augmentation = node.getAugmentation(augType);
146         return (Aug)augmentation;
147     }
148
149     protected List<T> getData(Aug augmentation) {
150         return Collections.EMPTY_LIST;
151     }
152
153     protected List<T> getData(Node node) {
154         Aug augmentation = getAugmentation(node);
155         if (augmentation != null) {
156             List<T> data = getData(augmentation);
157             if (data != null) {
158                 return Lists.newArrayList(data);
159             }
160         }
161         return Collections.EMPTY_LIST;
162     }
163
164     protected Map<InstanceIdentifier<Node>, List<T>> extractRemoved(
165             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
166         Map<InstanceIdentifier<Node>, List<T>> result
167                 = new HashMap<InstanceIdentifier<Node>, List<T>>();
168         List<T> removed = Collections.EMPTY_LIST;
169         if (changes != null && !changes.isEmpty()) {
170             for (DataTreeModification<Node> change : changes) {
171                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
172                 removed = getRemoved(change);
173                 removed.addAll(getCascadeDeleteData(change));
174                 result.put(key, removed);
175             }
176         }
177         return result;
178     }
179
180     protected Map<InstanceIdentifier<Node>, List<T>> extractUpdated(
181             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
182         Map<InstanceIdentifier<Node>, List<T>> result
183                 = new HashMap<InstanceIdentifier<Node>, List<T>>();
184         if (changes != null && !changes.isEmpty()) {
185             for (DataTreeModification<Node> change : changes) {
186                 InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
187                 result.put(key, getUpdated(change));
188             }
189         }
190         return result;
191     }
192
193     List<T>  getCascadeDeleteData(DataTreeModification<Node> change) {
194         if (!cascadeDelete()) {
195             return Collections.EMPTY_LIST;
196         }
197         DataObjectModification<Node> mod = change.getRootNode();
198         Node updatedNode = TransactUtils.getUpdated(mod);
199         List<T> updatedData = getData(updatedNode);
200         Set<InstanceIdentifier> deleted = getOperationalState().getDeletedKeysInCurrentTx(LogicalSwitches.class);
201         UnMetDependencyGetter dependencyGetter = getDependencyGetter();
202         if (!HwvtepSouthboundUtil.isEmpty(deleted) && !HwvtepSouthboundUtil.isEmpty(updatedData) && dependencyGetter != null) {
203             List<T> removed = new ArrayList<T>();
204             for (T ele : updatedData) {
205                 if (deleted.containsAll(dependencyGetter.getLogicalSwitchDependencies(ele))) {
206                     removed.add(ele);
207                 }
208             }
209             return removed;
210         }
211         return Collections.EMPTY_LIST;
212     }
213
214     List<T> getRemoved(DataTreeModification<Node> change) {
215         DataObjectModification<Node> mod = change.getRootNode();
216
217         Node removed = TransactUtils.getRemoved(mod);
218         Node updated = TransactUtils.getUpdated(mod);
219         Node before = mod.getDataBefore();
220         return diffOf(removed, before, updated, true);
221     }
222
223     List<T> getUpdated(DataTreeModification<Node> change) {
224         DataObjectModification<Node> mod = change.getRootNode();
225         Node created = TransactUtils.getCreated(mod);
226         Node updated = TransactUtils.getUpdated(mod);
227         Node before = mod.getDataBefore();
228         return diffOf(created, updated, before, false);
229     }
230
231     List<T> diffOf(Node include, Node a, Node b, boolean compareKeyOnly) {
232         List<T> data1 = getData(include);
233         List<T> data2 = diffOf(a, b, compareKeyOnly);
234         if (HwvtepSouthboundUtil.isEmpty(data1) && HwvtepSouthboundUtil.isEmpty(data2)) {
235             return Collections.EMPTY_LIST;
236         }
237         List<T> result = Lists.newArrayList(data1);
238         result.addAll(data2);
239         return result;
240     }
241
242     List<T> diffOf(Node a, Node b, boolean compareKeyOnly) {
243         List<T> result = new ArrayList<T>();
244
245         List<T> list1 = getData(a);
246         List<T> list2 = getData(b);
247
248         if (HwvtepSouthboundUtil.isEmpty(list1)) {
249             return Collections.EMPTY_LIST;
250         }
251         if (HwvtepSouthboundUtil.isEmpty(list2)) {
252             return HwvtepSouthboundUtil.isEmpty(list1) ? Collections.EMPTY_LIST : list1;
253         }
254
255         Iterator<T> it1 = list1.iterator();
256
257         while(it1.hasNext()) {
258             T ele = it1.next();
259             Iterator<T> it2 = list2.iterator();
260             boolean found = false;
261             while (it2.hasNext()) {
262                 T other  = it2.next();
263                 found = compareKeyOnly ? Objects.equals(ele.getKey(), other.getKey()) : areEqual(ele, other);
264                 if ( found ) {
265                     it2.remove();
266                     break;
267                 }
268             }
269             if (!found) {
270                 result.add(ele);
271             }
272         }
273         return result;
274     }
275
276     protected boolean areEqual(T a , T b) {
277         return a.getKey().equals(b.getKey());
278     }
279
280     protected UnMetDependencyGetter getDependencyGetter() {
281         return null;
282     }
283
284     /**
285      * Tells if this object needs to be deleted if its dependent object gets deleted
286      * Ex : LocalUcastMac and LocalMacstMac
287      * @return true if this object needs to be deleted if its dependent object gets deleted
288      */
289     protected boolean cascadeDelete() {
290         return false;
291     }
292 }