2 * Copyright © 2019 AT&T.
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.cds.blueprintsprocessor.functions.resource.resolution.capabilities
19 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.restClientService
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
25 import org.onap.ccsdk.cds.controllerblueprints.core.logger
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.KeyIdentifier
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.springframework.http.HttpMethod
33 * @author saurav.paira
36 open class IpAssignResolutionCapability : ResourceAssignmentProcessor() {
38 val log = logger(IpAssignResolutionCapability::class)
40 override fun getName(): String {
41 return "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}ipassignment-capability"
44 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
46 if (!setFromInput(resourceAssignment) && isTemplateKeyValueNull(resourceAssignment)) {
47 val dName = resourceAssignment.dictionaryName!!
48 val dSource = resourceAssignment.dictionarySource!!
49 val resourceDefinition = resourceDefinition(dName)
51 /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/
52 val resourceSource = resourceAssignment.dictionarySourceDefinition
53 ?: resourceDefinition?.sources?.get(dSource)
54 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
56 val resourceSourceProperties =
57 checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
59 // Get all matching resources assignments to process
60 val groupResourceAssignments =
61 resourceAssignments.filter {
62 it.dictionarySource == dSource
65 // inputKeyMapping is dynamic based on dependencies
66 val inputKeyMapping: MutableMap<String, String> =
67 resourceAssignment.dependencies?.map { it to it }?.toMap()
68 as MutableMap<String, String>
70 // Get the values from runtime store
71 val resolvedKeyValues = resolveInputKeyMappingVariables(inputKeyMapping)
72 log.info("\nResolved Input Key mappings: \n{}", resolvedKeyValues)
74 resolvedKeyValues?.map { KeyIdentifier(it.key, it.value) }
75 ?.let { resourceAssignment.keyIdentifiers.addAll(it) }
77 // Generate the payload using already resolved value
78 val generatedPayload = generatePayload(resolvedKeyValues, groupResourceAssignments)
79 log.info("\nIP Assign mS Request Payload: \n{}", generatedPayload.asJsonType().toPrettyString())
81 resourceSourceProperties["resolved-payload"] = JacksonUtils.jsonNode(generatedPayload)
83 // Get the Rest Client service, selector will be included in application.properties
84 val restClientService = BluePrintDependencyService.restClientService(
88 // Get the Rest Response
89 val response = restClientService.exchangeResource(
91 "/web/service/v1/assign", generatedPayload
93 val responseStatusCode = response.status
94 val responseBody = response.body
95 log.info("\nIP Assign mS Response : \n{}", responseBody.asJsonType().toPrettyString())
96 if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
97 populateResource(groupResourceAssignments, responseBody)
100 "Failed to dictionary name ($dName), dictionary source($($dName) " +
101 "response_code: ($responseStatusCode)"
103 throw BluePrintProcessorException(errMsg)
105 // Parse the error Body and assign the property value
107 // Check the value has populated for mandatory case
108 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
109 } catch (e: Exception) {
110 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
111 throw BluePrintProcessorException(
112 "Failed in template key ($resourceAssignment) assignments with: ${e.message}",
118 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
119 addError(runtimeException.message!!)
122 /** Generates aggregated request payload for Ip Assign mS. Parses the resourceassignments of
123 * sourceCapability "ipassign-ms". It generates below sample payload
126 "name": "fixed_ipv4_Address_01",
128 "CloudRegionId": "abcd123",
129 "IpServiceName": "MobilityPlan",
132 "name": "fixed_ipv4_Address_02",
134 "CloudRegionId": "abcd123",
135 "IpServiceName": "MobilityPlan",
140 private fun generatePayload(
141 input: Map<String, Any>,
142 groupResourceAssignments: MutableList<ResourceAssignment>
144 data class IpRequest(val name: String = "", val property: Map<String, String> = mutableMapOf<String, String>())
145 data class IpAssignRequest(val requests: MutableList<IpRequest> = mutableListOf())
147 val ipAssignRequests = IpAssignRequest()
148 groupResourceAssignments.forEach {
149 val ipRequest = IpRequest(it.name, input.mapValues { it.value.toString().removeSurrounding("\"") })
150 ipAssignRequests.requests.add(ipRequest)
152 return ipAssignRequests.asJsonType().toString()
155 private fun populateResource(
156 resourceAssignments: MutableList<ResourceAssignment>,
159 /** Parse all the resource assignment fields and set the corresponding value */
160 resourceAssignments.forEach { resourceAssignment ->
161 // Set the List of Complex Values
162 val parsedResourceAssignmentValue = checkNotNull(
163 JacksonUtils.jsonNode(restResponse).path(resourceAssignment.name).textValue()
165 "Failed to find path ($resourceAssignment.name) in response ($restResponse)"
168 ResourceAssignmentUtils.setResourceDataValue(
171 parsedResourceAssignmentValue