c1ed5ffd9a70b4ebb08d7fd9397fb2835476908d
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudAsyncGraphEventCache.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.service;
25
26 import com.google.common.cache.Cache;
27 import com.google.common.cache.CacheBuilder;
28
29 import java.util.concurrent.TimeUnit;
30
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.crud.logging.CrudServiceMsgs;
34 import org.onap.crud.service.CrudAsyncGraphDataService.CollectGraphResponse;
35 import org.onap.crud.util.CrudProperties;
36 import org.onap.crud.util.CrudServiceConstants;
37
38 /**
39  * Self expiring Cache to hold request transactionIds . Events are expired
40  * automatically after 2 seconds of request time out
41  */
42 public class CrudAsyncGraphEventCache {
43   private static Logger logger = LoggerFactory.getInstance().getLogger(CrudAsyncGraphEventCache
44       .class.getName());
45
46   private static Integer interval;
47
48   static {
49     // Set the cache eviction timeout = request timeout + 2 sec for the
50     // buffer
51     interval = CrudAsyncGraphDataService.DEFAULT_REQUEST_TIMEOUT + 2000;
52     try {
53       interval = Integer
54           .parseInt(CrudProperties.get(CrudServiceConstants.CRD_ASYNC_REQUEST_TIMEOUT) + 2000);
55     } catch (Exception ex) {
56       logger.error(CrudServiceMsgs.ASYNC_DATA_CACHE_ERROR, "Unable to parse "
57           + CrudServiceConstants.CRD_ASYNC_REQUEST_TIMEOUT + " error: "
58           + ex.getMessage());
59     }
60   }
61
62   private final static Cache<String, CollectGraphResponse> cache = CacheBuilder.newBuilder()
63       .expireAfterWrite(interval, TimeUnit.MILLISECONDS).build();
64
65
66   public static void put(String uuid, CollectGraphResponse collector) {
67     cache.put(uuid, collector);
68
69   }
70
71   public static CollectGraphResponse get(String uuid) {
72     return cache.getIfPresent(uuid);
73   }
74
75   public static void invalidate(String uuid) {
76     cache.invalidate(uuid);
77   }
78
79 }