1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.cambria.service.impl;
25 import java.util.TreeSet;
27 import com.att.nsa.cambria.transaction.DMaaPTransactionFactory;
28 import com.att.nsa.cambria.transaction.DMaaPTransactionObj;
29 import com.att.nsa.cambria.transaction.DMaaPTransactionObjDB;
30 import com.att.nsa.cambria.transaction.TransactionObj;
31 import com.att.nsa.configs.ConfigDb;
32 import com.att.nsa.configs.ConfigDbException;
33 import com.att.nsa.configs.ConfigPath;
36 * Persistent storage for Transaction objects built over an abstract config db.
42 public class BaseTransactionDbImpl<K extends DMaaPTransactionObj> implements DMaaPTransactionObjDB<K> {
44 private final ConfigDb fDb;
45 private final ConfigPath fBasePath;
46 private final DMaaPTransactionFactory<K> fKeyFactory;
48 private static final String kStdRootPath = "/transaction";
50 private ConfigPath makePath(String transactionId) {
51 return fBasePath.getChild(transactionId);
55 * Construct an Transaction db over the given config db at the standard
60 * @throws ConfigDbException
62 public BaseTransactionDbImpl(ConfigDb db, DMaaPTransactionFactory<K> keyFactory) throws ConfigDbException {
63 this(db, kStdRootPath, keyFactory);
67 * Construct an Transaction db over the given config db using the given root
73 * @throws ConfigDbException
75 public BaseTransactionDbImpl(ConfigDb db, String rootPath, DMaaPTransactionFactory<K> keyFactory)
76 throws ConfigDbException {
78 fBasePath = db.parse(rootPath);
79 fKeyFactory = keyFactory;
81 if (!db.exists(fBasePath)) {
82 db.store(fBasePath, "");
87 * Create a new Transaction Obj. If one exists,
90 * @return the new Transaction record
91 * @throws ConfigDbException
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);
99 // make one, store it, return it
100 final K newKey = fKeyFactory.makeNewTransactionId(id);
101 fDb.store(path, newKey.serialize());
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).
112 * @throws ConfigDbException
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");
120 fDb.store(path, ((TransactionObj) trnObj).serialize());
124 * Load an Transaction record based on the Transaction Id value
126 * @param transactionId
127 * @return an Transaction Object record or null
128 * @throws ConfigDbException
131 public synchronized K loadTransactionObj(String transactionId) throws ConfigDbException {
132 final String data = fDb.load(makePath(transactionId));
134 return fKeyFactory.makeNewTransactionObj(data);
140 * Load all transactions known to this database. (This could be expensive.)
142 * @return a set of all Transaction objects
143 * @throws ConfigDbException
145 public synchronized Set<String> loadAllTransactionObjs() throws ConfigDbException {
146 final TreeSet<String> result = new TreeSet<String>();
147 for (ConfigPath cp : fDb.loadChildrenNames(fBasePath)) {
148 result.add(cp.getName());