from common import ResolutionHelper
from java.lang import Exception as JavaException
from netconfclient import NetconfClient
-from org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor import \
- NetconfComponentFunction
-
-
-class NetconfRpcExample(NetconfComponentFunction):
-
- def process(self, execution_request):
- try:
- log = globals()[netconf_constant.SERVICE_LOG]
- print(globals())
- nc = NetconfClient(log, self, "netconf-connection")
- rr = ResolutionHelper(self)
-
- payload = rr.resolve_and_generate_message_from_template_prefix("hostname")
-
- nc.connect()
- response = nc.lock()
- if not response.isSuccess():
- log.error(response.errorMessage)
-
- nc.edit_config(message_content=payload, edit_default_peration="none")
- nc.validate()
- nc.discard_change()
- nc.validate()
- nc.commit()
- nc.unlock()
- nc.disconnect()
-
- except JavaException, err:
- log.error("Java Exception in the script {}", err)
- except Exception, err:
- log.error("Python Exception in the script {}", err)
-
- def recover(self, runtime_exception, execution_request):
- print "Recovering calling.." + PROPERTY_BLUEPRINT_BASE_PATH
- return None
+from org.onap.ccsdk.cds.blueprintsprocessor.services.execution import AbstractScriptComponentFunction
+
+class NetconfRpcExample(AbstractScriptComponentFunction):
+
+ def process(self, execution_request):
+ try:
+ log = globals()[netconf_constant.SERVICE_LOG]
+ print(globals())
+ nc = NetconfClient(log, self, "netconf-connection")
+ rr = ResolutionHelper(self)
+
+ payload = rr.resolve_and_generate_message_from_template_prefix("hostname")
+
+ nc.connect()
+ response = nc.lock()
+ if not response.isSuccess():
+ log.error(response.errorMessage)
+
+ nc.edit_config(message_content=payload, edit_default_peration="none")
+ nc.validate()
+ nc.discard_change()
+ nc.validate()
+ nc.commit()
+ nc.unlock()
+ nc.disconnect()
+
+ except JavaException, err:
+ log.error("Java Exception in the script {}", err)
+ except Exception, err:
+ log.error("Python Exception in the script {}", err)
+
+ def recover(self, runtime_exception, execution_request):
+ print "Recovering calling.." + PROPERTY_BLUEPRINT_BASE_PATH
+ return None
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+from org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution import ResourceResolutionExtensionsKt
-class ResolutionHelper:
- def __init__(self, component_function):
- self.component_function = component_function
+class ResolutionHelper:
- def resolve_and_generate_message_from_template_prefix(self, artifact_prefix):
- return self.component_function.contentFromResolvedArtifact(artifact_prefix)
+ def __init__(self, component_function):
+ self.component_function = component_function
- def resolve_and_generate_message(self, artifact_mapping, artifact_template):
- return self.component_function.resolveAndGenerateMessage(artifact_mapping,
- artifact_template)
+ def resolve_and_generate_message_from_template_prefix(self, artifact_prefix):
+ return ResourceResolutionExtensionsKt.contentFromResolvedArtifact(self.component_function, artifact_prefix)
- def retrieve_resolved_template_from_database(self, key, artifact_template):
- return self.component_function.storedContentFromResolvedArtifact(key, artifact_template)
+ def retrieve_resolved_template_from_database(self, key, artifact_template):
+ return ResourceResolutionExtensionsKt.storedContentFromResolvedArtifact(self.component_function, key,
+ artifact_template)
from netconf_constant import CONFIG_TARGET_RUNNING, CONFIG_TARGET_CANDIDATE, \
- CONFIG_DEFAULT_OPERATION_REPLACE
+ CONFIG_DEFAULT_OPERATION_REPLACE
+from org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor import NetconfExecutorExtensionsKt
class NetconfClient:
- def __init__(self, log, component_function, requirement_name):
- self.log = log
- self.component_function = component_function
- netconf_device = self.component_function.initializeNetconfConnection(
- requirement_name)
- self.netconf_rpc_client = netconf_device.netconfRpcService
- self.netconf_session = netconf_device.netconfSession
-
- def disconnect(self):
- self.netconf_session.disconnect()
- return
-
- def connect(self):
- self.netconf_session.connect()
- return
-
- def lock(self, config_target=CONFIG_TARGET_CANDIDATE):
- device_response = self.netconf_rpc_client.lock(config_target)
- return device_response
-
- def get_config(self, filter="", config_target=CONFIG_TARGET_RUNNING):
- device_response = self.netconf_rpc_client.getConfig(filter, config_target)
- return device_response
-
- def edit_config(self, message_content, config_target=CONFIG_TARGET_CANDIDATE,
- edit_default_peration=CONFIG_DEFAULT_OPERATION_REPLACE):
- device_response = self.netconf_rpc_client.editConfig(message_content,
- config_target,
- edit_default_peration)
- return device_response
-
- def commit(self, confirmed=False, confirm_timeout=60, persist="",
- persist_id=""):
- device_response = self.netconf_rpc_client.commit(confirmed, confirm_timeout,
- persist, persist_id)
- return device_response
-
- def invoke_rpc(self, rpc):
- device_response = self.netconf_rpc_client.invokeRpc(rpc)
- return device_response
-
- def cancel_commit(self, persist_id=""):
- device_response = self.netconf_rpc_client.cancelCommit(persist_id)
- return device_response
-
- def unlock(self, config_target=CONFIG_TARGET_CANDIDATE):
- device_response = self.netconf_rpc_client.unLock(config_target)
- return device_response
-
- def validate(self, config_target=CONFIG_TARGET_CANDIDATE):
- device_response = self.netconf_rpc_client.validate(config_target)
- return device_response
-
- def discard_change(self):
- device_response = self.netconf_rpc_client.discardConfig()
- return device_response
+ def __init__(self, log, component_function, requirement_name):
+ self.log = log
+ self.component_function = component_function
+ netconf_device = NetconfExecutorExtensionsKt.netconfDevice(component_function, requirement_name)
+ self.netconf_rpc_client = netconf_device.netconfRpcService
+ self.netconf_session = netconf_device.netconfSession
+
+ def disconnect(self):
+ self.netconf_session.disconnect()
+ return
+
+ def connect(self):
+ self.netconf_session.connect()
+ return
+
+ def lock(self, config_target=CONFIG_TARGET_CANDIDATE):
+ device_response = self.netconf_rpc_client.lock(config_target)
+ return device_response
+
+ def get_config(self, filter="", config_target=CONFIG_TARGET_RUNNING):
+ device_response = self.netconf_rpc_client.getConfig(filter, config_target)
+ return device_response
+
+ def edit_config(self, message_content, config_target=CONFIG_TARGET_CANDIDATE,
+ edit_default_peration=CONFIG_DEFAULT_OPERATION_REPLACE):
+ device_response = self.netconf_rpc_client.editConfig(message_content,
+ config_target,
+ edit_default_peration)
+ return device_response
+
+ def commit(self, confirmed=False, confirm_timeout=60, persist="",
+ persist_id=""):
+ device_response = self.netconf_rpc_client.commit(confirmed, confirm_timeout,
+ persist, persist_id)
+ return device_response
+
+ def invoke_rpc(self, rpc):
+ device_response = self.netconf_rpc_client.invokeRpc(rpc)
+ return device_response
+
+ def cancel_commit(self, persist_id=""):
+ device_response = self.netconf_rpc_client.cancelCommit(persist_id)
+ return device_response
+
+ def unlock(self, config_target=CONFIG_TARGET_CANDIDATE):
+ device_response = self.netconf_rpc_client.unLock(config_target)
+ return device_response
+
+ def validate(self, config_target=CONFIG_TARGET_CANDIDATE):
+ device_response = self.netconf_rpc_client.validate(config_target)
+ return device_response
+
+ def discard_change(self):
+ device_response = self.netconf_rpc_client.discardConfig()
+ return device_response
/*
* Copyright © 2019 IBM.
+ * Modifications Copyright © 2018-2019 IBM, Bell Canada
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
import org.springframework.beans.factory.config.ConfigurableBeanFactory
const val INSTANCE_DEPENDENCIES = "instance-dependencies"
}
- lateinit var scriptComponent: NetconfComponentFunction
+ lateinit var scriptComponent: AbstractScriptComponentFunction
override suspend fun processNB(executionRequest: ExecutionServiceInput) {
scriptDependencies.add(instanceName.textValue())
}
- scriptComponent = componentFunctionScriptingService.scriptInstance<NetconfComponentFunction>(this, scriptType,
- scriptClassReference, scriptDependencies)
+ scriptComponent = componentFunctionScriptingService
+ .scriptInstance<AbstractScriptComponentFunction>(this, scriptType,
+ scriptClassReference, scriptDependencies)
checkNotNull(scriptComponent) { "failed to get netconf script component" }
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
"./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
- val executionContext = bluePrintRuntimeService.getExecutionContext()
+ val assignmentParams = "{\n" +
+ " \"ipAddress\": \"127.0.0.1\",\n" +
+ " \"hostName\": \"vnf-host\"\n" +
+ " }"
+
+ val json = """{
+ "hostname" : "127.0.0.1"
+ }
+ """.trimIndent()
+ bluePrintRuntimeService.assignInputs(json.asJsonType())
+ bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
+ JacksonUtils.jsonNode(assignmentParams))
+
+ val executionContext = bluePrintRuntimeService.getExecutionContext()
componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService
private var templateResolutionDBService: TemplateResolutionService,
private var blueprintTemplateService: BluePrintTemplateService,
private var resourceResolutionDBService: ResourceResolutionDBService) :
- ResourceResolutionService {
+ ResourceResolutionService {
private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
override fun registeredResourceSources(): List<String> {
return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
- .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
- .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
+ .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
+ .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
}
override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>,
artifactTemplate: String,
resolutionKey: String): String {
return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
- bluePrintRuntimeService,
- artifactTemplate,
- resolutionKey)
+ bluePrintRuntimeService,
+ artifactTemplate,
+ resolutionKey)
}
override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
artifactNames: List<String>,
properties: Map<String, Any>): MutableMap<String, JsonNode> {
-
val resourceAssignmentRuntimeService =
ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
artifactName, properties)
resolvedParams[artifactName] = resolvedContent.asJsonType()
-
}
return resolvedParams
}
log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
val resourceAssignmentContent =
- bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
+ bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
val resourceAssignments: MutableList<ResourceAssignment> =
- JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
- as? MutableList<ResourceAssignment>
- ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
+ JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
+ as? MutableList<ResourceAssignment>
+ ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
if (isToStore(properties)) {
val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
// Get the Resource Dictionary Name
val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
- .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
+ .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
// Resolve resources
resolveResourceAssignments(bluePrintRuntimeService,
- resourceDefinitions,
- resourceAssignments,
- artifactPrefix,
- properties)
+ resourceDefinitions,
+ resourceAssignments,
+ artifactPrefix,
+ properties)
val resolvedParamJsonContent =
- ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
+ ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
resolvedContent = blueprintTemplateService.generateContent(bluePrintRuntimeService, nodeTemplateName,
- artifactTemplate, resolvedParamJsonContent)
+ artifactTemplate, resolvedParamJsonContent)
if (isToStore(properties)) {
templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
properties: Map<String, Any>) {
val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
- val resourceAssignmentRuntimeService = blueprintRuntimeService as ResourceAssignmentRuntimeService
+
+ // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
+ val resourceAssignmentRuntimeService = if (!(blueprintRuntimeService is ResourceAssignmentRuntimeService)) {
+ ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
+ } else {
+ blueprintRuntimeService
+ }
+
coroutineScope {
bulkSequenced.forEach { batchResourceAssignments ->
// Execute Non Dependent Assignments in parallel ( ie asynchronously )
val deferred = batchResourceAssignments
- .filter { it.name != "*" && it.name != "start" }
- .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
- .map { resourceAssignment ->
- async {
- val dictionaryName = resourceAssignment.dictionaryName
- val dictionarySource = resourceAssignment.dictionarySource
-
- val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
-
- val resourceAssignmentProcessor =
- applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
- ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
- "for resource assignment(${resourceAssignment.name})")
- try {
- // Set BluePrint Runtime Service
- resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
- // Set Resource Dictionaries
- resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
- // Invoke Apply Method
- resourceAssignmentProcessor.applyNB(resourceAssignment)
-
- if (isToStore(properties)) {
- resourceResolutionDBService.write(properties,
- blueprintRuntimeService,
- artifactPrefix,
- resourceAssignment)
- log.info("Resource resolution saved into database successfully : ($resourceAssignment)")
+ .filter { it.name != "*" && it.name != "start" }
+ .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
+ .map { resourceAssignment ->
+ async {
+ val dictionaryName = resourceAssignment.dictionaryName
+ val dictionarySource = resourceAssignment.dictionarySource
+
+ val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
+
+ val resourceAssignmentProcessor =
+ applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
+ ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
+ "for resource assignment(${resourceAssignment.name})")
+ try {
+ // Set BluePrint Runtime Service
+ resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
+ // Set Resource Dictionaries
+ resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
+ // Invoke Apply Method
+ resourceAssignmentProcessor.applyNB(resourceAssignment)
+
+ if (isToStore(properties)) {
+ resourceResolutionDBService.write(properties,
+ blueprintRuntimeService,
+ artifactPrefix,
+ resourceAssignment)
+ log.info("Resource resolution saved into database successfully : ($resourceAssignment)")
+ }
+
+ // Set errors from RA
+ blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
+ } catch (e: RuntimeException) {
+ log.error("Fail in processing ${resourceAssignment.name}", e)
+ throw BluePrintProcessorException(e)
}
-
- // Set errors from RA
- blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
- } catch (e: RuntimeException) {
- log.error("Fail in processing ${resourceAssignment.name}", e)
- throw BluePrintProcessorException(e)
}
}
- }
log.debug("Resolving (${deferred.size})resources parallel.")
deferred.awaitAll()
}
}
else -> {
val resourceDefinition = resourceDefinitions[dictionaryName]
- ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
+ ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
val resourceSource = resourceDefinition.sources[dictionarySource]
- ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
+ ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
}
if (resolutionKey.isNotEmpty()) {
val existingResourceAssignments =
- resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
- bluePrintRuntimeService,
- resolutionKey,
- occurrence,
- artifactPrefix)
+ resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
+ bluePrintRuntimeService,
+ resolutionKey,
+ occurrence,
+ artifactPrefix)
if (existingResourceAssignments.isNotEmpty()) {
log.info("Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
- resolutionKey)
+ resolutionKey)
}
return existingResourceAssignments
} else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
val existingResourceAssignments =
- resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
- bluePrintRuntimeService,
- resourceId,
- resourceType,
+ resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
+ bluePrintRuntimeService,
+ resourceId,
+ resourceType,
- occurrence,
- artifactPrefix)
+ occurrence,
+ artifactPrefix)
if (existingResourceAssignments.isNotEmpty()) {
log.info("Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
"all resources not already resolved.")
/*
* Copyright © 2019 IBM.
+ * Modifications Copyright © 2018-2019 IBM, Bell Canada
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.apache.commons.lang3.ObjectUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils
import org.slf4j.helpers.MessageFormatter
import kotlin.reflect.KClass
}
fun String.isJson(): Boolean {
- return ((this.startsWith("{") && this.endsWith("}"))
- || (this.startsWith("[") && this.endsWith("]")))
+ return ((this.trim().startsWith("{") && this.trim().endsWith("}"))
+ || (this.trim().startsWith("[") && this.trim().endsWith("]")))
}
fun Any.asJsonString(intend: Boolean? = false): String {
return this is ObjectNode || this is ArrayNode
}
+// Json Parsing Extensions
+fun JsonNode.jsonPathParse(expression: String): JsonNode {
+ check(this.isComplexType()) { "$this is not complex or array node to apply expression" }
+ return JsonParserUtils.parse(this, expression)
+}
+
+// Json Path Extensions
+fun JsonNode.jsonPaths(expression: String): List<String> {
+ check(this.isComplexType()) { "$this is not complex or array node to apply expression" }
+ return JsonParserUtils.paths(this, expression)
+}
+
import org.onap.ccsdk.cds.controllerblueprints.core.*
import org.onap.ccsdk.cds.controllerblueprints.core.data.*
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
-import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
/**
}
if (subAttributeName != null) {
if (valueNode.isComplexType())
- valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subAttributeName)
+ valueNode = valueNode.jsonPathParse(subAttributeName)
}
return valueNode
}
if (subPropertyName != null) {
if (valueNode.isComplexType())
- valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
+ valueNode = valueNode.jsonPathParse(subPropertyName)
}
return valueNode
}
val subPropertyName: String? = operationOutputExpression.subPropertyName
if (subPropertyName != null) {
if (valueNode.isComplexType())
- valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
+ valueNode = valueNode.jsonPathParse(subPropertyName)
}
return valueNode
}
import com.jayway.jsonpath.Option
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
class JsonParserUtils {
companion object {
}
fun paths(jsonNode: JsonNode, expression: String): List<String> {
- return paths(jsonNode, expression)
+ return paths(jsonNode.asJsonString(), expression)
}
fun parse(jsonContent: String, expression: String): JsonNode {
}
fun parse(jsonNode: JsonNode, expression: String): JsonNode {
- return parse(jsonNode.toString(), expression)
+ return parse(jsonNode.asJsonString(), expression)
}
fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode {
fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode {
- return parseNSet(jsonNode, expression, valueNode)
+ return parseNSet(jsonNode.asJsonString(), expression, valueNode)
}
}
}
\ No newline at end of file
import org.junit.Test
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonPaths
import kotlin.test.assertEquals
class JsonParserUtilsTest {
fun `test parse Node`() {
val dataNode = JacksonUtils.jsonNodeFromClassPathFile("data/default-context.json")
- val parsedNode = JsonParserUtils.parse(dataNode, "$.request-id")
+ val parsedNode = dataNode.jsonPathParse("$.request-id")
assertEquals(parsedNode, "12345".asJsonPrimitive(), "failed to parse json request-id")
}
+
+ @Test
+ fun testPaths() {
+ val json: String = """
+ {
+ "data" : {
+ "prop1" : "1234"
+ },
+ "data2" : {
+ "prop1" : "12345"
+ },
+ "data3" : [{
+ "prop1" : "12345"
+ }
+ ]
+ }
+ """.trimIndent()
+ val jsonNode = json.jsonAsJsonType()
+ val parsedPath = jsonNode.jsonPaths("$..prop1")
+ println(parsedPath)
+ }
}
\ No newline at end of file