3457cff943fd40226ddbf62e796ed4f996f96eda
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudAsyncGraphEventCache.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.crud.service;
22
23 import com.google.common.cache.Cache;
24 import com.google.common.cache.CacheBuilder;
25
26 import java.util.concurrent.TimeUnit;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.crud.logging.CrudServiceMsgs;
31 import org.onap.crud.service.CrudAsyncGraphDataService.CollectGraphResponse;
32 import org.onap.crud.util.CrudProperties;
33 import org.onap.crud.util.CrudServiceConstants;
34
35 /**
36  * Self expiring Cache to hold request transactionIds . Events are expired
37  * automatically after 2 seconds of request time out
38  */
39 public class CrudAsyncGraphEventCache {
40   private static Logger logger = LoggerFactory.getInstance().getLogger(CrudAsyncGraphEventCache
41       .class.getName());
42
43   private static Integer interval;
44
45   static {
46     // Set the cache eviction timeout = request timeout + 2 sec for the
47     // buffer
48     interval = CrudAsyncGraphDataService.DEFAULT_REQUEST_TIMEOUT + 2000;
49     try {
50       interval = Integer
51           .parseInt(CrudProperties.get(CrudServiceConstants.CRD_ASYNC_REQUEST_TIMEOUT) + 2000);
52     } catch (Exception ex) {
53       logger.error(CrudServiceMsgs.ASYNC_DATA_CACHE_ERROR, "Unable to parse "
54           + CrudServiceConstants.CRD_ASYNC_REQUEST_TIMEOUT + " error: "
55           + ex.getMessage());
56     }
57   }
58
59   private final static Cache<String, CollectGraphResponse> cache = CacheBuilder.newBuilder()
60       .expireAfterWrite(interval, TimeUnit.MILLISECONDS).build();
61
62
63   public static void put(String uuid, CollectGraphResponse collector) {
64     cache.put(uuid, collector);
65
66   }
67
68   public static CollectGraphResponse get(String uuid) {
69     return cache.getIfPresent(uuid);
70   }
71
72   public static void invalidate(String uuid) {
73     cache.invalidate(uuid);
74   }
75
76 }