Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / common / transaction / mngr / TransactionManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.transaction.mngr;
22
23 import com.google.common.collect.EvictingQueue;
24 import com.google.common.collect.Queues;
25 import org.openecomp.sdc.be.dao.impl.ESCatalogDAO;
26 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
27 import org.openecomp.sdc.common.transaction.api.ITransactionSdnc;
28 import org.openecomp.sdc.common.transaction.api.TransactionUtils;
29 import org.openecomp.sdc.common.transaction.api.TransactionUtils.ActionTypeEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34 import javax.annotation.Resource;
35 import java.util.Queue;
36 import java.util.concurrent.atomic.AtomicInteger;
37
38 @Component("transactionManager")
39 public class TransactionManager {
40
41     private static final Logger log = LoggerFactory.getLogger(TransactionManager.class);
42
43     private AtomicInteger transactionIDCounter = new AtomicInteger(0);
44
45     private Queue<ITransactionSdnc> transactions;
46     @Resource
47     private ESCatalogDAO esCatalogDao;
48     @Resource
49     private TitanGenericDao titanGenericDao;
50
51     /**
52      * userId and actionType parameters are used only for logging purposes.
53      */
54     public ITransactionSdnc getTransaction(String userId, ActionTypeEnum actionType) {
55         if (transactions == null) {
56             init();
57         }
58         log.debug("TransactionManager creating new SdncTransaction");
59         ITransactionSdnc tx = new TransactionSdncImpl(generateTransactionID(), userId, actionType, esCatalogDao, titanGenericDao);
60         transactions.add(tx);
61
62         return tx;
63
64     }
65
66     private Integer generateTransactionID() {
67         boolean generatedSuccessfully = false;
68         int nextId = 0;
69
70         while (!generatedSuccessfully) {
71             int prevId = transactionIDCounter.get();
72             if (prevId > TransactionUtils.TRANSACTION_ID_RESET_LIMIT) {
73                 resetTransactionId();
74             }
75             nextId = prevId + 1;
76             generatedSuccessfully = transactionIDCounter.compareAndSet(prevId, nextId);
77         }
78         return nextId;
79     }
80
81     private void resetTransactionId() {
82
83         boolean resetSuccessfully = false;
84         while (!resetSuccessfully) {
85             int prevId = transactionIDCounter.get();
86             resetSuccessfully = transactionIDCounter.compareAndSet(prevId, 0);
87         }
88
89     }
90
91     private synchronized void init() {
92         if (transactions == null) {
93             log.info("TransactionManager Initialized");
94             EvictingQueue<ITransactionSdnc> queue = EvictingQueue
95                     .<ITransactionSdnc>create(TransactionUtils.MAX_SIZE_TRANSACTION_LIST);
96             // make thread-safe
97             transactions = Queues.synchronizedQueue(queue);
98         }
99     }
100
101 }