2  * Copyright © 2017-2018 AT&T Intellectual Property.
 
   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
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 package org.onap.ccsdk.apps.controllerblueprints.core
 
  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
 
  27 import java.io.InputStream
 
  28 import kotlin.reflect.KClass
 
  33  * @author Brinda Santh
 
  36 fun String.asJsonPrimitive(): TextNode {
 
  40 fun Boolean.asJsonPrimitive(): BooleanNode {
 
  41     return BooleanNode.valueOf(this)
 
  44 fun Int.asJsonPrimitive(): IntNode {
 
  45     return IntNode.valueOf(this)
 
  48 fun Double.asJsonPrimitive(): DoubleNode {
 
  49     return DoubleNode.valueOf(this)
 
  52 fun MutableMap<String, *>.asJsonNode(): JsonNode {
 
  53     return JacksonUtils.jsonNodeFromObject(this)
 
  56 fun format(message: String, vararg args: Any?): String {
 
  57     if (args != null && args.isNotEmpty()) {
 
  58         return MessageFormatter.arrayFormat(message, args).message
 
  63 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
 
  64     if (containsKey(key)) {
 
  71 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
 
  72     if (containsKey(key)) {
 
  75         throw BluePrintException("couldn't find the key $key")
 
  79 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
 
  84             this[key] = TextNode(value)
 
  86             this[key] = BooleanNode.valueOf(value)
 
  88             this[key] = IntNode.valueOf(value.toInt())
 
  90             this[key] = DoubleNode.valueOf(value.toDouble())
 
  92             this[key] = JacksonUtils.jsonNodeFromObject(value)
 
  96 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
 
  97     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
 
 100 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
 
 101     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
 
 104 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
 
 105     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
 
 108 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
 
 109     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
 
 114 fun checkNotEmpty(value: String?): Boolean {
 
 115     return value != null && value.isNotBlank()
 
 118 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
 
 119     val notEmpty = checkNotEmpty(value)
 
 121         throw BluePrintException(message!!)
 
 126 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
 
 127     if (value1.equals(value2, ignoreCase = true)) {
 
 130         throw BluePrintException(lazyMessage().toString())
 
 134 fun nullToEmpty(value: String?): String {
 
 135     return if (checkNotEmpty(value)) value!! else ""
 
 138 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
 
 139     if (checkNotEmpty(value)) {
 
 142         throw IllegalStateException(lazyMessage().toString())
 
 146 fun InputStream.toFile(path: String): File {
 
 147     val file = File(path)
 
 148     file.outputStream().use { this.copyTo(it) }