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