b66f424b8578d6cb8f7903d9b80814e1931821fd
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onosproject.store.consistent.impl;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.function.Supplier;
22
23 import static com.google.common.base.Preconditions.*;
24
25 import org.onosproject.store.service.ConsistentMapBuilder;
26 import org.onosproject.store.service.DatabaseUpdate;
27 import org.onosproject.store.service.Serializer;
28 import org.onosproject.store.service.Transaction;
29 import org.onosproject.store.service.TransactionContext;
30 import org.onosproject.store.service.TransactionalMap;
31
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34 import com.google.common.util.concurrent.Futures;
35
36 /**
37  * Default TransactionContext implementation.
38  */
39 public class DefaultTransactionContext implements TransactionContext {
40     private static final String TX_NOT_OPEN_ERROR = "Transaction Context is not open";
41
42     @SuppressWarnings("rawtypes")
43     private final Map<String, DefaultTransactionalMap> txMaps = Maps.newConcurrentMap();
44     private boolean isOpen = false;
45     private final Database database;
46     private final long transactionId;
47     private final Supplier<ConsistentMapBuilder> mapBuilderSupplier;
48
49     public DefaultTransactionContext(long transactionId,
50             Database database,
51             Supplier<ConsistentMapBuilder> mapBuilderSupplier) {
52         this.transactionId = transactionId;
53         this.database = checkNotNull(database);
54         this.mapBuilderSupplier = checkNotNull(mapBuilderSupplier);
55     }
56
57     @Override
58     public long transactionId() {
59         return transactionId;
60     }
61
62     @Override
63     public void begin() {
64         checkState(!isOpen, "Transaction Context is already open");
65         isOpen = true;
66     }
67
68     @Override
69     public boolean isOpen() {
70         return isOpen;
71     }
72
73     @Override
74     @SuppressWarnings("unchecked")
75     public <K, V> TransactionalMap<K, V> getTransactionalMap(String mapName,
76             Serializer serializer) {
77         checkState(isOpen, TX_NOT_OPEN_ERROR);
78         checkNotNull(mapName);
79         checkNotNull(serializer);
80         return txMaps.computeIfAbsent(mapName, name -> new DefaultTransactionalMap<>(
81                                 name,
82                                 mapBuilderSupplier.get().withName(name).withSerializer(serializer).build(),
83                                 this,
84                                 serializer));
85     }
86
87     @SuppressWarnings("unchecked")
88     @Override
89     public void commit() {
90         // TODO: rework commit implementation to be more intuitive
91         checkState(isOpen, TX_NOT_OPEN_ERROR);
92         CommitResponse response = null;
93         try {
94             List<DatabaseUpdate> updates = Lists.newLinkedList();
95             txMaps.values().forEach(m -> updates.addAll(m.prepareDatabaseUpdates()));
96             Transaction transaction = new DefaultTransaction(transactionId, updates);
97             response = Futures.getUnchecked(database.prepareAndCommit(transaction));
98         } finally {
99             if (response != null && !response.success()) {
100                 abort();
101             }
102             isOpen = false;
103         }
104     }
105
106     @Override
107     public void abort() {
108         if (isOpen) {
109             try {
110                 txMaps.values().forEach(m -> m.rollback());
111             } finally {
112                 isOpen = false;
113             }
114         }
115     }
116 }