re base code
[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.log.wrappers.Logger;
28 import org.openecomp.sdc.common.transaction.api.ITransactionSdnc;
29 import org.openecomp.sdc.common.transaction.api.TransactionUtils;
30 import org.openecomp.sdc.common.transaction.api.TransactionUtils.ActionTypeEnum;
31 import org.springframework.stereotype.Component;
32
33 import javax.annotation.Resource;
34 import java.util.Queue;
35 import java.util.concurrent.atomic.AtomicInteger;
36
37 @Component("transactionManager")
38 public class TransactionManager {
39
40     private static final Logger log = Logger.getLogger(TransactionManager.class.getName());
41
42     private AtomicInteger transactionIDCounter = new AtomicInteger(0);
43
44     private Queue<ITransactionSdnc> transactions;
45     @Resource
46     private ESCatalogDAO esCatalogDao;
47     @Resource
48     private TitanGenericDao titanGenericDao;
49
50     /**
51      * userId and actionType parameters are used only for logging purposes.
52      */
53     public ITransactionSdnc getTransaction(String userId, ActionTypeEnum actionType) {
54         if (transactions == null) {
55             init();
56         }
57         log.debug("TransactionManager creating new SdncTransaction");
58         ITransactionSdnc tx = new TransactionSdncImpl(generateTransactionID(), userId, actionType, esCatalogDao, titanGenericDao);
59         transactions.add(tx);
60
61         return tx;
62
63     }
64
65     private Integer generateTransactionID() {
66         boolean generatedSuccessfully = false;
67         int nextId = 0;
68
69         while (!generatedSuccessfully) {
70             int prevId = transactionIDCounter.get();
71             if (prevId > TransactionUtils.TRANSACTION_ID_RESET_LIMIT) {
72                 resetTransactionId();
73             }
74             nextId = prevId + 1;
75             generatedSuccessfully = transactionIDCounter.compareAndSet(prevId, nextId);
76         }
77         return nextId;
78     }
79
80     private void resetTransactionId() {
81
82         boolean resetSuccessfully = false;
83         while (!resetSuccessfully) {
84             int prevId = transactionIDCounter.get();
85             resetSuccessfully = transactionIDCounter.compareAndSet(prevId, 0);
86         }
87
88     }
89
90     private synchronized void init() {
91         if (transactions == null) {
92             log.info("TransactionManager Initialized");
93             EvictingQueue<ITransactionSdnc> queue = EvictingQueue
94                     .<ITransactionSdnc>create(TransactionUtils.MAX_SIZE_TRANSACTION_LIST);
95             // make thread-safe
96             transactions = Queues.synchronizedQueue(queue);
97         }
98     }
99
100 }