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