[AAI-2175] Change aai champ container processes to run as non-root on the host
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / service / ChampTransactionCache.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Modifications Copyright (C) 2019 IBM.
9  * ===================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  */
23 package org.onap.champ.service;
24
25 import java.util.concurrent.TimeUnit;
26
27 import org.onap.aai.champcore.ChampGraph;
28 import org.onap.aai.champcore.ChampTransaction;
29 import org.onap.aai.champcore.exceptions.ChampTransactionException;
30 import org.onap.champ.service.logging.ChampMsgs;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33
34 import com.google.common.cache.Cache;
35 import com.google.common.cache.CacheBuilder;
36 import com.google.common.cache.RemovalCause;
37 import com.google.common.cache.RemovalListener;
38 import com.google.common.cache.RemovalNotification;
39
40 /**
41  * Self expiring Cache to hold request transactionIds . Events are expired
42  * automatically after configured interval
43  */
44 public class ChampTransactionCache {
45   private static Logger logger = LoggerFactory.getInstance().getLogger(ChampTransactionCache
46       .class.getName());
47
48   
49   private ChampGraph graphImpl;
50   private Cache<String, ChampTransaction> cache;
51
52
53
54   public ChampTransactionCache(long txTimeOutInSec,ChampGraph graphImpl) {
55     CacheBuilder builder = CacheBuilder.newBuilder().expireAfterWrite(txTimeOutInSec, TimeUnit.SECONDS)
56         .removalListener(new RemovalListener() {
57
58           public void onRemoval(RemovalNotification notification) {
59             if(notification.getCause()==RemovalCause.EXPIRED){
60             logger.info(ChampMsgs.CHAMP_TX_CACHE, "Following transaction: "+notification.getKey()+" is being evicted from cache due to timeout of " + txTimeOutInSec+" seconds");
61             try {
62               graphImpl.rollbackTransaction((ChampTransaction) notification.getValue());
63               logger.info(ChampMsgs.CHAMP_TX_CACHE, "Transaction rolledback successfully :" + notification.getKey());
64             } catch (ChampTransactionException e) {
65               logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, e, "Transaction rolledback failed :");
66             }
67             }
68           }
69         });
70     cache = builder.build();
71
72     this.graphImpl = graphImpl;
73     
74   }
75
76   public  void put(String txId, ChampTransaction tx) {
77     cache.put(txId, tx);
78
79   }
80
81   public  ChampTransaction get(String txId) {
82     if (txId==null)
83       return null;
84     if(cache.getIfPresent(txId)==null){
85       //cleanup cache so that removalListener is called
86       cache.cleanUp();
87     }
88     return cache.getIfPresent(txId);
89   }
90
91   public  void invalidate(String txId) {
92     cache.invalidate(txId);
93   }
94   
95
96
97 }