summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/dmf/mr/service/impl/BaseTransactionDbImpl.java
blob: 1ad7e3a3a88700131244228f1f548fe1f214c647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
*  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *  
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
package org.onap.dmaap.dmf.mr.service.impl;

import com.att.nsa.configs.ConfigDb;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.configs.ConfigPath;
import org.onap.dmaap.dmf.mr.transaction.DMaaPTransactionFactory;
import org.onap.dmaap.dmf.mr.transaction.DMaaPTransactionObj;
import org.onap.dmaap.dmf.mr.transaction.DMaaPTransactionObjDB;
import org.onap.dmaap.dmf.mr.transaction.TransactionObj;

import java.util.Set;
import java.util.TreeSet;

/**
 * Persistent storage for Transaction objects built over an abstract config db.
 * 
 * @author anowarul.islam
 *
 * @param <K>
 */
public class BaseTransactionDbImpl<K extends DMaaPTransactionObj> implements DMaaPTransactionObjDB<K> {

	private final ConfigDb fDb;
	private final ConfigPath fBasePath;
	private final DMaaPTransactionFactory<K> fKeyFactory;

	private static final String kStdRootPath = "/transaction";

	private ConfigPath makePath(String transactionId) {
		return fBasePath.getChild(transactionId);
	}

	/**
	 * Construct an Transaction db over the given config db at the standard
	 * location
	 * 
	 * @param db
	 * @param keyFactory
	 * @throws ConfigDbException
	 */
	public BaseTransactionDbImpl(ConfigDb db, DMaaPTransactionFactory<K> keyFactory) throws ConfigDbException {
		this(db, kStdRootPath, keyFactory);
	}

	/**
	 * Construct an Transaction db over the given config db using the given root
	 * location
	 * 
	 * @param db
	 * @param rootPath
	 * @param keyFactory
	 * @throws ConfigDbException
	 */
	public BaseTransactionDbImpl(ConfigDb db, String rootPath, DMaaPTransactionFactory<K> keyFactory)
			throws ConfigDbException {
		fDb = db;
		fBasePath = db.parse(rootPath);
		fKeyFactory = keyFactory;

		if (!db.exists(fBasePath)) {
			db.store(fBasePath, "");
		}
	}

	/**
	 * Create a new Transaction Obj. If one exists,
	 * 
	 * @param id
	 * @return the new Transaction record
	 * @throws ConfigDbException
	 */
	public synchronized K createTransactionObj(String id) throws KeyExistsException, ConfigDbException {
		final ConfigPath path = makePath(id);
		if (fDb.exists(path)) {
			throw new KeyExistsException(id);
		}

		// make one, store it, return it
		final K newKey = fKeyFactory.makeNewTransactionId(id);
		fDb.store(path, newKey.serialize());
		return newKey;
	}

	/**
	 * Save an Transaction record. This must be used after changing auxiliary
	 * data on the record. Note that the transaction object must exist (via
	 * createTransactionObj).
	 * 
	 * @param transaction
	 *            object
	 * @throws ConfigDbException
	 */
	@Override
	public synchronized void saveTransactionObj(K trnObj) throws ConfigDbException {
		final ConfigPath path = makePath(trnObj.getId());
		if (!fDb.exists(path) || !(trnObj instanceof TransactionObj)) {
			throw new IllegalStateException(trnObj.getId() + " is not known to this database");
		}
		fDb.store(path, ((TransactionObj) trnObj).serialize());
	}

	/**
	 * Load an Transaction record based on the Transaction Id value
	 * 
	 * @param transactionId
	 * @return an Transaction Object record or null
	 * @throws ConfigDbException
	 */
	@Override
	public synchronized K loadTransactionObj(String transactionId) throws ConfigDbException {
		final String data = fDb.load(makePath(transactionId));
		if (data != null) {
			return fKeyFactory.makeNewTransactionObj(data);
		}
		return null;
	}

	/**
	 * Load all transactions known to this database. (This could be expensive.)
	 * 
	 * @return a set of all Transaction objects
	 * @throws ConfigDbException
	 */
	public synchronized Set<String> loadAllTransactionObjs() throws ConfigDbException {
		final TreeSet<String> result = new TreeSet<>();
		for (ConfigPath cp : fDb.loadChildrenNames(fBasePath)) {
			result.add(cp.getName());
		}
		return result;
	}

}