Blueprint Processor Python Script Components
[ccsdk/apps.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / CustomFunctions.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.BooleanNode
21 import com.fasterxml.jackson.databind.node.DoubleNode
22 import com.fasterxml.jackson.databind.node.IntNode
23 import com.fasterxml.jackson.databind.node.TextNode
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
25 import org.slf4j.helpers.MessageFormatter
26 import java.io.File
27 import java.io.InputStream
28 import kotlin.reflect.KClass
29
30 /**
31  *
32  *
33  * @author Brinda Santh
34  */
35
36 fun String.asJsonPrimitive(): TextNode {
37     return TextNode(this)
38 }
39
40 fun Boolean.asJsonPrimitive(): BooleanNode {
41     return BooleanNode.valueOf(this)
42 }
43
44 fun Int.asJsonPrimitive(): IntNode {
45     return IntNode.valueOf(this)
46 }
47
48 fun Double.asJsonPrimitive(): DoubleNode {
49     return DoubleNode.valueOf(this)
50 }
51
52 fun MutableMap<String, *>.asJsonNode(): JsonNode {
53     return JacksonUtils.jsonNodeFromObject(this)
54 }
55
56 fun format(message: String, vararg args: Any?): String {
57     if (args != null && args.isNotEmpty()) {
58         return MessageFormatter.arrayFormat(message, args).message
59     }
60     return message
61 }
62
63 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
64     if (containsKey(key)) {
65         return get(key) as? T
66     } else {
67         return null
68     }
69 }
70
71 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
72     if (containsKey(key)) {
73         return get(key) as T
74     } else {
75         throw BluePrintException("couldn't find the key $key")
76     }
77 }
78
79 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
80     when (value) {
81         is JsonNode ->
82             this[key] = value
83         is String ->
84             this[key] = TextNode(value)
85         is Boolean ->
86             this[key] = BooleanNode.valueOf(value)
87         is Int ->
88             this[key] = IntNode.valueOf(value.toInt())
89         is Double ->
90             this[key] = DoubleNode.valueOf(value.toDouble())
91         else ->
92             this[key] = JacksonUtils.jsonNodeFromObject(value)
93     }
94 }
95
96 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
97     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
98 }
99
100 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
101     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
102 }
103
104 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
105     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
106 }
107
108 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
109     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
110 }
111
112 // Checks
113
114 fun checkNotEmpty(value: String?): Boolean {
115     return value != null && value.isNotBlank()
116 }
117
118 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
119     val notEmpty = checkNotEmpty(value)
120     if (!notEmpty) {
121         throw BluePrintException(message!!)
122     }
123     return notEmpty
124 }
125
126 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
127     if (value1.equals(value2, ignoreCase = true)) {
128         return true
129     } else {
130         throw BluePrintException(lazyMessage().toString())
131     }
132 }
133
134 fun nullToEmpty(value: String?): String {
135     return if (checkNotEmpty(value)) value!! else ""
136 }
137
138 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
139     if (checkNotEmpty(value)) {
140         return value!!
141     } else {
142         throw IllegalStateException(lazyMessage().toString())
143     }
144 }
145
146 fun InputStream.toFile(path: String): File {
147     val file = File(path)
148     file.outputStream().use { this.copyTo(it) }
149     return file
150 }
151