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 org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.service.impl;
25 import java.util.TreeSet;
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;
32 import com.att.nsa.configs.ConfigDb;
33 import com.att.nsa.configs.ConfigDbException;
34 import com.att.nsa.configs.ConfigPath;
37 * Persistent storage for Transaction objects built over an abstract config db.
43 public class BaseTransactionDbImpl<K extends DMaaPTransactionObj> implements DMaaPTransactionObjDB<K> {
45 private final ConfigDb fDb;
46 private final ConfigPath fBasePath;
47 private final DMaaPTransactionFactory<K> fKeyFactory;
49 private static final String kStdRootPath = "/transaction";
51 private ConfigPath makePath(String transactionId) {
52 return fBasePath.getChild(transactionId);
56 * Construct an Transaction db over the given config db at the standard
61 * @throws ConfigDbException
63 public BaseTransactionDbImpl(ConfigDb db, DMaaPTransactionFactory<K> keyFactory) throws ConfigDbException {
64 this(db, kStdRootPath, keyFactory);
68 * Construct an Transaction db over the given config db using the given root
74 * @throws ConfigDbException
76 public BaseTransactionDbImpl(ConfigDb db, String rootPath, DMaaPTransactionFactory<K> keyFactory)
77 throws ConfigDbException {
79 fBasePath = db.parse(rootPath);
80 fKeyFactory = keyFactory;
82 if (!db.exists(fBasePath)) {
83 db.store(fBasePath, "");
88 * Create a new Transaction Obj. If one exists,
91 * @return the new Transaction record
92 * @throws ConfigDbException
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);
100 // make one, store it, return it
101 final K newKey = fKeyFactory.makeNewTransactionId(id);
102 fDb.store(path, newKey.serialize());
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).
113 * @throws ConfigDbException
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");
121 fDb.store(path, ((TransactionObj) trnObj).serialize());
125 * Load an Transaction record based on the Transaction Id value
127 * @param transactionId
128 * @return an Transaction Object record or null
129 * @throws ConfigDbException
132 public synchronized K loadTransactionObj(String transactionId) throws ConfigDbException {
133 final String data = fDb.load(makePath(transactionId));
135 return fKeyFactory.makeNewTransactionObj(data);
141 * Load all transactions known to this database. (This could be expensive.)
143 * @return a set of all Transaction objects
144 * @throws ConfigDbException
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());