9e735cf0c7900d8d38b6c443c75d182400d94d7a
[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         process(resourceResolutionOutput.resourceAssignments)\r
51 \r
52         val status = Status()\r
53         status.code = 200\r
54         status.message = "Success"\r
55         resourceResolutionOutput.status = status\r
56 \r
57         return resourceResolutionOutput\r
58     }\r
59 \r
60     fun registeredResourceSources(): List<String> {\r
61         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)\r
62                 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
63                 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
64     }\r
65 \r
66     fun process(resourceAssignments: MutableList<ResourceAssignment>) {\r
67 \r
68         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)\r
69 \r
70         bulkSequenced.map { batchResourceAssignments ->\r
71             batchResourceAssignments.filter { it.name != "*" && it.name != "start" }\r
72                     .map { resourceAssignment ->\r
73                         val dictionarySource = resourceAssignment.dictionarySource\r
74                         val processorInstanceName = ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR.plus(dictionarySource)\r
75 \r
76                         val resourceAssignmentProcessor = applicationContext.getBean(processorInstanceName) as? ResourceAssignmentProcessor\r
77                                 ?: throw BluePrintProcessorException("failed to get resource processor for instance name($processorInstanceName) " +\r
78                                         "for resource assignment(${resourceAssignment.name})")\r
79                         try {\r
80                             // Invoke Apply Method\r
81                             resourceAssignmentProcessor.apply(resourceAssignment)\r
82                         } catch (e: RuntimeException) {\r
83                             resourceAssignmentProcessor.recover(e, resourceAssignment)\r
84                             throw BluePrintProcessorException(e)\r
85                         }\r
86                     }\r
87         }\r
88     }\r
89 }\r