aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/capabilities/IpAssignResolutionCapability.kt
blob: 71cf6ceea90f6a551e90ac9cb0ac2f11ec0efad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright © 2019 AT&T.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  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.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.capabilities

import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
import org.onap.ccsdk.cds.blueprintsprocessor.rest.restClientService
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.logger
import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.KeyIdentifier
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
import org.springframework.http.HttpMethod

/**
 * @author saurav.paira
 */

open class IpAssignResolutionCapability : ResourceAssignmentProcessor() {

    val log = logger(IpAssignResolutionCapability::class)

    override fun getName(): String {
        return "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}ipassignment-capability"
    }

    override suspend fun processNB(resourceAssignment: ResourceAssignment) {
        try {
            if (!setFromInput(resourceAssignment) && isTemplateKeyValueNull(resourceAssignment)) {
                val dName = resourceAssignment.dictionaryName!!
                val dSource = resourceAssignment.dictionarySource!!
                val resourceDefinition = resourceDefinition(dName)

                /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/
                val resourceSource = resourceAssignment.dictionarySourceDefinition
                    ?: resourceDefinition?.sources?.get(dSource)
                    ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")

                val resourceSourceProperties =
                    checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }

                // Get all matching resources assignments to process
                val groupResourceAssignments =
                    resourceAssignments.filter {
                        it.dictionarySource == dSource
                    }.toMutableList()

                // inputKeyMapping is dynamic based on dependencies
                val inputKeyMapping: MutableMap<String, String> =
                    resourceAssignment.dependencies?.map { it to it }?.toMap()
                        as MutableMap<String, String>

                // Get the values from runtime store
                val resolvedKeyValues = resolveInputKeyMappingVariables(inputKeyMapping)
                log.info("\nResolved Input Key mappings: \n{}", resolvedKeyValues)

                resolvedKeyValues?.map { KeyIdentifier(it.key, it.value) }
                    ?.let { resourceAssignment.keyIdentifiers.addAll(it) }

                // Generate the payload using already resolved value
                val generatedPayload = generatePayload(resolvedKeyValues, groupResourceAssignments)
                log.info("\nIP Assign mS Request Payload: \n{}", generatedPayload.asJsonType().toPrettyString())

                resourceSourceProperties["resolved-payload"] = JacksonUtils.jsonNode(generatedPayload)

                // Get the Rest Client service, selector will be included in application.properties
                val restClientService = BluePrintDependencyService.restClientService(
                    "ipassign-ms"
                )

                // Get the Rest Response
                val response = restClientService.exchangeResource(
                    HttpMethod.POST.name,
                    "/web/service/v1/assign", generatedPayload
                )
                val responseStatusCode = response.status
                val responseBody = response.body
                log.info("\nIP Assign mS Response : \n{}", responseBody.asJsonType().toPrettyString())
                if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
                    populateResource(groupResourceAssignments, responseBody)
                } else {
                    val errMsg =
                        "Failed to dictionary name ($dName), dictionary source($($dName) " +
                            "response_code: ($responseStatusCode)"
                    log.warn(errMsg)
                    throw BluePrintProcessorException(errMsg)
                }
                // Parse the error Body and assign the property value
            }
            // Check the value has populated for mandatory case
            ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
        } catch (e: Exception) {
            ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
            throw BluePrintProcessorException(
                "Failed in template key ($resourceAssignment) assignments with: ${e.message}",
                e
            )
        }
    }

    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
        addError(runtimeException.message!!)
    }

    /** Generates aggregated request payload for Ip Assign mS. Parses the resourceassignments of
     * sourceCapability "ipassign-ms". It generates below sample payload
     * {
     "requests": [{
     "name": "fixed_ipv4_Address_01",
     "property": {
     "CloudRegionId": "abcd123",
     "IpServiceName": "MobilityPlan",
     }
     }, {
     "name": "fixed_ipv4_Address_02",
     "property": {
     "CloudRegionId": "abcd123",
     "IpServiceName": "MobilityPlan",
     }
     }
     ]
     } */
    private fun generatePayload(
        input: Map<String, Any>,
        groupResourceAssignments: MutableList<ResourceAssignment>
    ): String {
        data class IpRequest(val name: String = "", val property: Map<String, String> = mutableMapOf<String, String>())
        data class IpAssignRequest(val requests: MutableList<IpRequest> = mutableListOf())

        val ipAssignRequests = IpAssignRequest()
        groupResourceAssignments.forEach {
            val ipRequest = IpRequest(it.name, input.mapValues { it.value.toString().removeSurrounding("\"") })
            ipAssignRequests.requests.add(ipRequest)
        }
        return ipAssignRequests.asJsonType().toString()
    }

    private fun populateResource(
        resourceAssignments: MutableList<ResourceAssignment>,
        restResponse: String
    ) {
        /** Parse all the resource assignment fields and set the corresponding value */
        resourceAssignments.forEach { resourceAssignment ->
            // Set the List of Complex Values
            val parsedResourceAssignmentValue = checkNotNull(
                JacksonUtils.jsonNode(restResponse).path(resourceAssignment.name).textValue()
            ) {
                "Failed to find path ($resourceAssignment.name) in response ($restResponse)"
            }

            ResourceAssignmentUtils.setResourceDataValue(
                resourceAssignment,
                raRuntimeService,
                parsedResourceAssignmentValue
            )
        }
    }
}