2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.champ.service;
25 import java.util.concurrent.TimeUnit;
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;
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;
41 * Self expiring Cache to hold request transactionIds . Events are expired
42 * automatically after configured interval
44 public class ChampTransactionCache {
45 private static Logger logger = LoggerFactory.getInstance().getLogger(ChampTransactionCache
49 private ChampGraph graphImpl;
50 private Cache<String, ChampTransaction> cache;
54 public ChampTransactionCache(long txTimeOutInSec,ChampGraph graphImpl) {
55 CacheBuilder builder = CacheBuilder.newBuilder().expireAfterWrite(txTimeOutInSec, TimeUnit.SECONDS)
56 .removalListener(new RemovalListener() {
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");
62 graphImpl.rollbackTransaction((ChampTransaction) notification.getValue());
63 logger.info(ChampMsgs.CHAMP_TX_CACHE, "Transaction rolledback successfully :" + notification.getKey());
64 } catch (ChampTransactionException e) {
65 // TODO Auto-generated catch block
71 cache = builder.build();
73 this.graphImpl = graphImpl;
77 public void put(String txId, ChampTransaction tx) {
82 public ChampTransaction get(String txId) {
85 if(cache.getIfPresent(txId)==null){
86 //cleanup cache so that removalListener is called
89 return cache.getIfPresent(txId);
92 public void invalidate(String txId) {
93 cache.invalidate(txId);