Enhance the use of cache in HwvtepDeviceInfo
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / AbstractTransactionCommand.java
1 /*
2  * Copyright (c) 2015 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.transactions.md;
10
11 import java.util.HashSet;
12 import java.util.Set;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.Identifiable;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public abstract class AbstractTransactionCommand<T extends DataObject> implements TransactionCommand {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractTransactionCommand.class);
32     private final TableUpdates updates;
33     private final DatabaseSchema dbSchema;
34     protected final HwvtepConnectionInstance key;
35     protected Set<Pair<Class<? extends Identifiable>, InstanceIdentifier>> addedKeys = new HashSet<>();
36     protected Set<Pair<Class<? extends Identifiable>, InstanceIdentifier>> deletedKeys = new HashSet<>();
37     protected HwvtepDeviceInfo deviceInfo;
38
39
40     public TableUpdates getUpdates() {
41         return updates;
42     }
43
44     public DatabaseSchema getDbSchema() {
45         return dbSchema;
46     }
47
48     public ConnectionInfo getConnectionInfo() {
49         return key.getMDConnectionInfo();
50     }
51
52     public HwvtepConnectionInstance getOvsdbConnectionInstance() {
53         return key;
54     }
55
56     public AbstractTransactionCommand(HwvtepConnectionInstance key,TableUpdates updates, DatabaseSchema dbSchema) {
57         this.updates = updates;
58         this.dbSchema = dbSchema;
59         this.key = key;
60         if (key != null) {
61             this.deviceInfo = key.getDeviceInfo();
62         }
63     }
64
65     public HwvtepDeviceInfo getDeviceInfo() {
66         return key.getDeviceInfo();
67     }
68
69     void addToDeviceUpdate(TransactionType transactionType, Object element) {
70         deviceInfo.addToDeviceUpdate(transactionType, element);
71     }
72
73     public void clearDeviceOpUUID(Class<? extends Identifiable> cls, InstanceIdentifier iid, UUID uuid) {
74         deviceInfo.clearDeviceOperUUID(cls, iid, uuid);
75     }
76
77     public void addToDeleteTx(ReadWriteTransaction tx, Class<? extends Identifiable> cls, InstanceIdentifier iid,
78                               UUID uuid) {
79         if (deviceInfo.isAvailableInOperDs(cls, iid)) {
80             tx.delete(LogicalDatastoreType.OPERATIONAL, iid);
81         }
82         deletedKeys.add(Pair.of(cls, iid));
83         clearDeviceOpUUID(cls, iid, uuid);
84     }
85
86     public void addToUpdateTx(Class<? extends Identifiable> cls, InstanceIdentifier iid, UUID uuid,
87                               Object southboundData) {
88         addedKeys.add(Pair.of(cls, iid));
89         deviceInfo.updateDeviceOperData(cls, iid, uuid, southboundData);
90     }
91
92     public void onSuccess() {
93         addedKeys.stream().forEach(pair -> {
94             deviceInfo.markAvailableInOperDs(pair.getLeft(), pair.getRight());
95         });
96         deletedKeys.stream().forEach(pair -> {
97             deviceInfo.clearOperDsAvailability(pair.getLeft(), pair.getRight());
98             deviceInfo.clearDeviceOperData(pair.getLeft(), pair.getRight());
99         });
100     }
101
102     public void onFailure() {
103         addedKeys.stream().forEach(pair -> {
104             LOG.error("Failed to add {}", pair.getLeft().getSimpleName());
105         });
106         deletedKeys.stream().forEach(pair -> {
107             LOG.error("Failed to delete {}", pair.getLeft().getSimpleName());
108         });
109     }
110 }