During resource resolution there occurs from time to time a ConcurrentModificationExc...
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceAssignmentRuntimeService.kt
1 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
2
3 import com.fasterxml.jackson.databind.JsonNode
4 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
5 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
6 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
7 import java.util.concurrent.ConcurrentHashMap
8
9 class ResourceAssignmentRuntimeService(private var id: String, private var bluePrintContext: BluePrintContext) :
10     DefaultBluePrintRuntimeService(id, bluePrintContext) {
11
12     private lateinit var resolutionId: String
13     private var resourceStore: MutableMap<String, JsonNode> = ConcurrentHashMap()
14
15     fun createUniqueId(key: String) {
16         resolutionId = "$id-$key"
17     }
18
19     fun cleanResourceStore() {
20         resourceStore.clear()
21     }
22
23     fun putResolutionStore(key: String, value: JsonNode) {
24         resourceStore[key] = value
25     }
26
27     fun getResolutionStore(): MutableMap<String, JsonNode> {
28         return resourceStore.mapValues { e -> e.value.deepCopy() as JsonNode }.toMutableMap()
29     }
30
31     fun getResolutionStore(key: String): JsonNode {
32         return resourceStore[key]
33             ?: throw BluePrintProcessorException("failed to get execution property ($key)")
34     }
35
36     fun checkResolutionStore(key: String): Boolean {
37         return resourceStore.containsKey(key)
38     }
39
40     fun getJsonNodeFromResolutionStore(key: String): JsonNode {
41         return getResolutionStore(key)
42     }
43
44     fun getStringFromResolutionStore(key: String): String? {
45         return getResolutionStore(key).asText()
46     }
47
48     fun getBooleanFromResolutionStore(key: String): Boolean? {
49         return getResolutionStore(key).asBoolean()
50     }
51
52     fun getIntFromResolutionStore(key: String): Int? {
53         return getResolutionStore(key).asInt()
54     }
55
56     fun getDoubleFromResolutionStore(key: String): Double? {
57         return getResolutionStore(key).asDouble()
58     }
59
60     fun putDictionaryStore(key: String, value: JsonNode) {
61         resourceStore["dictionary-$key"] = value
62     }
63
64     fun getDictionaryStore(key: String): JsonNode {
65         return resourceStore["dictionary-$key"]
66             ?: throw BluePrintProcessorException("failed to get execution property (dictionary-$key)")
67     }
68
69     fun checkDictionaryStore(key: String): Boolean {
70         return resourceStore.containsKey("dictionary-$key")
71     }
72
73     fun getJsonNodeFromDictionaryStore(key: String): JsonNode {
74         return getResolutionStore("dictionary-$key")
75     }
76
77     fun getStringFromDictionaryStore(key: String): String? {
78         return getResolutionStore("dictionary-$key").asText()
79     }
80
81     fun getBooleanFromDictionaryStore(key: String): Boolean? {
82         return getResolutionStore("dictionary-$key").asBoolean()
83     }
84
85     fun getIntFromDictionaryStore(key: String): Int? {
86         return getResolutionStore("dictionary-$key").asInt()
87     }
88
89     fun getDoubleFromDictionaryStore(key: String): Double? {
90         return getResolutionStore("dictionary-$key").asDouble()
91     }
92 }