Merge "Semicolon at the end of the Statement and Remove trailing whitespaces at the...
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / CacheConfig.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.aai.util
22
23 import com.fasterxml.jackson.core.type.TypeReference
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import com.fasterxml.jackson.module.kotlin.KotlinModule
26 import com.google.common.collect.ImmutableMap
27 import org.springframework.stereotype.Component
28
29 //I use a regular kotlin class because I want that when jackson read
30 //a json with null values (or missing fields) they would get default values.
31 //for other cases it's better to use data class for POJO class
32 //for more information you can read here :
33 //https://github.com/FasterXML/jackson-module-kotlin/issues/130
34 class CacheConfig constructor(
35         isActive: Boolean?,
36         expireAfterWriteHours: Long?,
37         refreshAfterWriteSeconds: Long?) {
38     val isActive: Boolean = isActive ?: true
39     val expireAfterWriteHours: Long = expireAfterWriteHours ?: 24L
40     val refreshAfterWriteSeconds: Long = refreshAfterWriteSeconds ?: 10L
41
42     companion object {
43         val defaultCacheConfig = CacheConfig(null, null, null)
44     }
45
46 }
47
48
49 interface CacheConfigProvider {
50     fun getCacheConfig(cacheName:String): CacheConfig
51 }
52
53 @Component
54 class CacheConfigProviderImpl() : CacheConfigProvider {
55     private val mapper = ObjectMapper().apply { registerModule(KotlinModule()) }
56
57     private fun readMapOfCacheConfig(): Map<String, CacheConfig> {
58         val configInputStream = CacheConfigProviderImpl::class.java.classLoader.getResourceAsStream("cacheConfig.json")
59
60         return if (configInputStream == null) {
61             ImmutableMap.of()
62         } else {
63             mapper.readValue(configInputStream, object : TypeReference<Map<String, CacheConfig>>() {})
64         }
65     }
66
67     override fun getCacheConfig(cacheName: String): CacheConfig {
68         return readMapOfCacheConfig()[cacheName] ?: CacheConfig.defaultCacheConfig
69     }
70 }
71
72