Sonar major issues
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / 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 com.att.dmf.mr.service.impl;
23
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 import com.att.dmf.mr.transaction.DMaaPTransactionFactory;
28 import com.att.dmf.mr.transaction.DMaaPTransactionObj;
29 import com.att.dmf.mr.transaction.DMaaPTransactionObjDB;
30 import com.att.dmf.mr.transaction.TransactionObj;
31 import com.att.nsa.configs.ConfigDb;
32 import com.att.nsa.configs.ConfigDbException;
33 import com.att.nsa.configs.ConfigPath;
34
35 /**
36  * Persistent storage for Transaction objects built over an abstract config db.
37  * 
38  * @author anowarul.islam
39  *
40  * @param <K>
41  */
42 public class BaseTransactionDbImpl<K extends DMaaPTransactionObj> implements DMaaPTransactionObjDB<K> {
43
44         private final ConfigDb fDb;
45         private final ConfigPath fBasePath;
46         private final DMaaPTransactionFactory<K> fKeyFactory;
47
48         private static final String kStdRootPath = "/transaction";
49
50         private ConfigPath makePath(String transactionId) {
51                 return fBasePath.getChild(transactionId);
52         }
53
54         /**
55          * Construct an Transaction db over the given config db at the standard
56          * location
57          * 
58          * @param db
59          * @param keyFactory
60          * @throws ConfigDbException
61          */
62         public BaseTransactionDbImpl(ConfigDb db, DMaaPTransactionFactory<K> keyFactory) throws ConfigDbException {
63                 this(db, kStdRootPath, keyFactory);
64         }
65
66         /**
67          * Construct an Transaction db over the given config db using the given root
68          * location
69          * 
70          * @param db
71          * @param rootPath
72          * @param keyFactory
73          * @throws ConfigDbException
74          */
75         public BaseTransactionDbImpl(ConfigDb db, String rootPath, DMaaPTransactionFactory<K> keyFactory)
76                         throws ConfigDbException {
77                 fDb = db;
78                 fBasePath = db.parse(rootPath);
79                 fKeyFactory = keyFactory;
80
81                 if (!db.exists(fBasePath)) {
82                         db.store(fBasePath, "");
83                 }
84         }
85
86         /**
87          * Create a new Transaction Obj. If one exists,
88          * 
89          * @param id
90          * @return the new Transaction record
91          * @throws ConfigDbException
92          */
93         public synchronized K createTransactionObj(String id) throws KeyExistsException, ConfigDbException {
94                 final ConfigPath path = makePath(id);
95                 if (fDb.exists(path)) {
96                         throw new KeyExistsException(id);
97                 }
98
99                 // make one, store it, return it
100                 final K newKey = fKeyFactory.makeNewTransactionId(id);
101                 fDb.store(path, newKey.serialize());
102                 return newKey;
103         }
104
105         /**
106          * Save an Transaction record. This must be used after changing auxiliary
107          * data on the record. Note that the transaction object must exist (via
108          * createTransactionObj).
109          * 
110          * @param transaction
111          *            object
112          * @throws ConfigDbException
113          */
114         @Override
115         public synchronized void saveTransactionObj(K trnObj) throws ConfigDbException {
116                 final ConfigPath path = makePath(trnObj.getId());
117                 if (!fDb.exists(path) || !(trnObj instanceof TransactionObj)) {
118                         throw new IllegalStateException(trnObj.getId() + " is not known to this database");
119                 }
120                 fDb.store(path, ((TransactionObj) trnObj).serialize());
121         }
122
123         /**
124          * Load an Transaction record based on the Transaction Id value
125          * 
126          * @param transactionId
127          * @return an Transaction Object record or null
128          * @throws ConfigDbException
129          */
130         @Override
131         public synchronized K loadTransactionObj(String transactionId) throws ConfigDbException {
132                 final String data = fDb.load(makePath(transactionId));
133                 if (data != null) {
134                         return fKeyFactory.makeNewTransactionObj(data);
135                 }
136                 return null;
137         }
138
139         /**
140          * Load all transactions known to this database. (This could be expensive.)
141          * 
142          * @return a set of all Transaction objects
143          * @throws ConfigDbException
144          */
145         public synchronized Set<String> loadAllTransactionObjs() throws ConfigDbException {
146                 final TreeSet<String> result = new TreeSet<>();
147                 for (ConfigPath cp : fDb.loadChildrenNames(fBasePath)) {
148                         result.add(cp.getName());
149                 }
150                 return result;
151         }
152
153 }