Resource Resolution Service : 2
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / functions / resource / resolution / processor / CapabilityResourceAssignmentProcessor.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.blueprintsprocessor.functions.resource.resolution.processor
18
19 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
21 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
22 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceAssignmentUtils
23 import org.springframework.beans.factory.annotation.Autowired
24 import org.springframework.context.ApplicationContext
25 import org.springframework.stereotype.Service
26
27 @Service("resource-assignment-processor-capability")
28 open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() {
29
30     companion object {
31         const val CAPABILITY_TYPE_JAVA_COMPONENT = "JAVA-COMPONENT"
32         const val CAPABILITY_TYPE_JYTHON_COMPONENT = "JYTHON-COMPONENT"
33     }
34
35     @Autowired
36     private lateinit var applicationContext: ApplicationContext
37
38     override fun getName(): String {
39         return "resource-assignment-processor-capability"
40     }
41
42     override fun process(executionRequest: ResourceAssignment) {
43
44         val resourceDefinition = resourceDictionaries[executionRequest.dictionaryName]
45                 ?: throw BluePrintProcessorException("couldn't get resource definition for ${executionRequest.dictionaryName}")
46
47         val resourceSource = resourceDefinition.sources[executionRequest.dictionarySource]
48                 ?: throw BluePrintProcessorException("couldn't get resource definition ${executionRequest.dictionaryName} source(${executionRequest.dictionarySource})")
49
50         checkNotNull(resourceSource.properties) { "failed to get ${executionRequest.dictionarySource} properties" }
51
52         val capabilityResourceSourceProperty = ResourceAssignmentUtils.transformResourceSource(resourceSource.properties!!, CapabilityResourceSource::class.java)
53
54         val instanceType = capabilityResourceSourceProperty.type
55         val instanceName = capabilityResourceSourceProperty.instanceName
56
57
58         var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null
59
60         when (instanceType) {
61             CAPABILITY_TYPE_JAVA_COMPONENT -> {
62                 // Initialize Capability Resource Assignment Processor
63                 componentResourceAssignmentProcessor = applicationContext.getBean(instanceName, ResourceAssignmentProcessor::class.java)
64             }
65             CAPABILITY_TYPE_JYTHON_COMPONENT -> {
66                 TODO(" No implementation")
67             }
68         }
69
70         checkNotNull(componentResourceAssignmentProcessor) { "failed to get capability resource assignment processor($instanceName)" }
71
72         // Assign Current Blueprint runtime and ResourceDictionaries
73         componentResourceAssignmentProcessor.bluePrintRuntimeService = bluePrintRuntimeService
74         componentResourceAssignmentProcessor.resourceDictionaries = resourceDictionaries
75
76         // Invoke componentResourceAssignmentProcessor
77         componentResourceAssignmentProcessor.apply(executionRequest)
78     }
79
80     override fun recover(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
81
82         TODO("To Implement")
83     }
84 }