155b3b03ab64fcd429dd347271af628ace127cea
[ccsdk/cds.git] /
1 /*\r
2  *  Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *  Modifications Copyright © 2018 IBM.\r
4  *\r
5  *  Licensed under the Apache License, Version 2.0 (the "License");\r
6  *  you may not use this file except in compliance with the License.\r
7  *  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  *  Unless required by applicable law or agreed to in writing, software\r
12  *  distributed under the License is distributed on an "AS IS" BASIS,\r
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  *  See the License for the specific language governing permissions and\r
15  *  limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.blueprintsprocessor.services.resolution\r
19 \r
20 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ResourceResolutionInput\r
21 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ResourceResolutionOutput\r
22 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException\r
24 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment\r
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignmentProcessor\r
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils\r
27 import org.springframework.beans.factory.annotation.Autowired\r
28 import org.springframework.context.ApplicationContext\r
29 import org.springframework.stereotype.Service\r
30 \r
31 /**\r
32  * ResourceResolutionService\r
33  * @author Brinda Santh\r
34  * 8/14/2018\r
35  */\r
36 \r
37 @Service\r
38 class ResourceResolutionService {\r
39 \r
40 \r
41     @Autowired\r
42     private lateinit var applicationContext: ApplicationContext\r
43 \r
44     fun resolveResource(resourceResolutionInput: ResourceResolutionInput): ResourceResolutionOutput {\r
45         val resourceResolutionOutput = ResourceResolutionOutput()\r
46         resourceResolutionOutput.actionIdentifiers = resourceResolutionInput.actionIdentifiers\r
47         resourceResolutionOutput.commonHeader = resourceResolutionInput.commonHeader\r
48         resourceResolutionOutput.resourceAssignments = resourceResolutionInput.resourceAssignments\r
49 \r
50         val context = hashMapOf<String, Any>()\r
51 \r
52         process(resourceResolutionOutput.resourceAssignments)\r
53 \r
54         val status = Status()\r
55         status.code = 200\r
56         status.message = "Success"\r
57         resourceResolutionOutput.status = status\r
58 \r
59         return resourceResolutionOutput\r
60     }\r
61 \r
62     fun registeredResourceSources(): List<String> {\r
63         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)\r
64                 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
65                 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
66     }\r
67 \r
68     fun process(resourceAssignments: MutableList<ResourceAssignment>) {\r
69 \r
70         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)\r
71 \r
72         bulkSequenced.map { batchResourceAssignments ->\r
73             batchResourceAssignments.filter { it.name != "*" && it.name != "start" }\r
74                     .map { resourceAssignment ->\r
75                         val dictionarySource = resourceAssignment.dictionarySource\r
76                         val processorInstanceName = ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR.plus(dictionarySource)\r
77 \r
78                         val resourceAssignmentProcessor = applicationContext.getBean(processorInstanceName) as? ResourceAssignmentProcessor\r
79                                 ?: throw BluePrintProcessorException("failed to get resource processor for instance name($processorInstanceName) " +\r
80                                         "for resource assignment(${resourceAssignment.name})")\r
81                         try {\r
82                             // Invoke Apply Method\r
83                             resourceAssignmentProcessor.apply(resourceAssignment)\r
84                         } catch (e: RuntimeException) {\r
85                             resourceAssignmentProcessor.recover(e, resourceAssignment)\r
86                             throw BluePrintProcessorException(e)\r
87                         }\r
88                     }\r
89         }\r
90     }\r
91 }\r