be779fa74a1807795cde2ff136adb6e758798488
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / messagerouter / msgrtr / nsa / cambria / service / impl / BaseTransactionDbImpl.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.service.impl;
23
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.transaction.DMaaPTransactionFactory;
28 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.transaction.DMaaPTransactionObj;
29 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.transaction.DMaaPTransactionObjDB;
30 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.transaction.TransactionObj;
31
32 import com.att.nsa.configs.ConfigDb;
33 import com.att.nsa.configs.ConfigDbException;
34 import com.att.nsa.configs.ConfigPath;
35
36 /**
37  * Persistent storage for Transaction objects built over an abstract config db.
38  * 
39  * @author author
40  *
41  * @param <K>
42  */
43 public class BaseTransactionDbImpl<K extends DMaaPTransactionObj> implements DMaaPTransactionObjDB<K> {
44
45         private final ConfigDb fDb;
46         private final ConfigPath fBasePath;
47         private final DMaaPTransactionFactory<K> fKeyFactory;
48
49         private static final String kStdRootPath = "/transaction";
50
51         private ConfigPath makePath(String transactionId) {
52                 return fBasePath.getChild(transactionId);
53         }
54
55         /**
56          * Construct an Transaction db over the given config db at the standard
57          * location
58          * 
59          * @param db
60          * @param keyFactory
61          * @throws ConfigDbException
62          */
63         public BaseTransactionDbImpl(ConfigDb db, DMaaPTransactionFactory<K> keyFactory) throws ConfigDbException {
64                 this(db, kStdRootPath, keyFactory);
65         }
66
67         /**
68          * Construct an Transaction db over the given config db using the given root
69          * location
70          * 
71          * @param db
72          * @param rootPath
73          * @param keyFactory
74          * @throws ConfigDbException
75          */
76         public BaseTransactionDbImpl(ConfigDb db, String rootPath, DMaaPTransactionFactory<K> keyFactory)
77                         throws ConfigDbException {
78                 fDb = db;
79                 fBasePath = db.parse(rootPath);
80                 fKeyFactory = keyFactory;
81
82                 if (!db.exists(fBasePath)) {
83                         db.store(fBasePath, "");
84                 }
85         }
86
87         /**
88          * Create a new Transaction Obj. If one exists,
89          * 
90          * @param id
91          * @return the new Transaction record
92          * @throws ConfigDbException
93          */
94         public synchronized K createTransactionObj(String id) throws KeyExistsException, ConfigDbException {
95                 final ConfigPath path = makePath(id);
96                 if (fDb.exists(path)) {
97                         throw new KeyExistsException(id);
98                 }
99
100                 // make one, store it, return it
101                 final K newKey = fKeyFactory.makeNewTransactionId(id);
102                 fDb.store(path, newKey.serialize());
103                 return newKey;
104         }
105
106         /**
107          * Save an Transaction record. This must be used after changing auxiliary
108          * data on the record. Note that the transaction object must exist (via
109          * createTransactionObj).
110          * 
111          * @param transaction
112          *            object
113          * @throws ConfigDbException
114          */
115         @Override
116         public synchronized void saveTransactionObj(K trnObj) throws ConfigDbException {
117                 final ConfigPath path = makePath(trnObj.getId());
118                 if (!fDb.exists(path) || !(trnObj instanceof TransactionObj)) {
119                         throw new IllegalStateException(trnObj.getId() + " is not known to this database");
120                 }
121                 fDb.store(path, ((TransactionObj) trnObj).serialize());
122         }
123
124         /**
125          * Load an Transaction record based on the Transaction Id value
126          * 
127          * @param transactionId
128          * @return an Transaction Object record or null
129          * @throws ConfigDbException
130          */
131         @Override
132         public synchronized K loadTransactionObj(String transactionId) throws ConfigDbException {
133                 final String data = fDb.load(makePath(transactionId));
134                 if (data != null) {
135                         return fKeyFactory.makeNewTransactionObj(data);
136                 }
137                 return null;
138         }
139
140         /**
141          * Load all transactions known to this database. (This could be expensive.)
142          * 
143          * @return a set of all Transaction objects
144          * @throws ConfigDbException
145          */
146         public synchronized Set<String> loadAllTransactionObjs() throws ConfigDbException {
147                 final TreeSet<String> result = new TreeSet<String>();
148                 for (ConfigPath cp : fDb.loadChildrenNames(fBasePath)) {
149                         result.add(cp.getName());
150                 }
151                 return result;
152         }
153
154 }