2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.aai.util
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
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(
36 expireAfterWriteHours: Long?,
37 refreshAfterWriteSeconds: Long?) {
38 val isActive: Boolean = isActive ?: true
39 val expireAfterWriteHours: Long = expireAfterWriteHours ?: 24L
40 val refreshAfterWriteSeconds: Long = refreshAfterWriteSeconds ?: 10L
43 val defaultCacheConfig = CacheConfig(null, null, null)
49 interface CacheConfigProvider {
50 fun getCacheConfig(cacheName:String): CacheConfig
54 class CacheConfigProviderImpl() : CacheConfigProvider {
55 private val mapper = ObjectMapper().apply { registerModule(KotlinModule()) }
57 private fun readMapOfCacheConfig(): Map<String, CacheConfig> {
58 val configInputStream = CacheConfigProviderImpl::class.java.classLoader.getResourceAsStream("cacheConfig.json")
60 return if (configInputStream == null) {
63 mapper.readValue(configInputStream, object : TypeReference<Map<String, CacheConfig>>() {})
67 override fun getCacheConfig(cacheName: String): CacheConfig {
68 return readMapOfCacheConfig()[cacheName] ?: CacheConfig.defaultCacheConfig