2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.service.enhancer
\r
19 import com.att.eelf.configuration.EELFLogger
\r
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
\r
22 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
\r
23 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
\r
24 import com.att.eelf.configuration.EELFManager
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.format
\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
\r
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
\r
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceAssignmentValidationDefaultService
\r
30 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceDefinitionRepoService
\r
31 import org.springframework.stereotype.Service
\r
34 * ResourceAssignmentEnhancerService.
\r
36 * @author Brinda Santh
\r
38 interface ResourceAssignmentEnhancerService {
\r
40 @Throws(BluePrintException::class)
\r
41 fun enhanceBluePrint(bluePrintEnhancerService: BluePrintEnhancerService,
\r
42 resourceAssignments: List<ResourceAssignment>)
\r
44 @Throws(BluePrintException::class)
\r
45 fun enhanceBluePrint(resourceAssignments: List<ResourceAssignment>): ServiceTemplate
\r
49 * ResourceAssignmentEnhancerDefaultService.
\r
51 * @author Brinda Santh
\r
54 open class ResourceAssignmentEnhancerDefaultService(private val resourceDefinitionRepoService: ResourceDefinitionRepoService)
\r
55 : ResourceAssignmentEnhancerService {
\r
56 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentValidationDefaultService::class.java)
\r
59 * Get the defined source instance from the ResourceAssignment,
\r
60 * then get the NodeType of the Sources assigned
\r
62 override fun enhanceBluePrint(bluePrintEnhancerService: BluePrintEnhancerService,
\r
63 resourceAssignments: List<ResourceAssignment>) {
\r
65 val uniqueSourceNodeTypeNames = hashSetOf<String>()
\r
67 // Iterate the Resource Assignment and
\r
68 resourceAssignments.map { resourceAssignment ->
\r
69 val dictionaryName = resourceAssignment.dictionaryName!!
\r
70 val dictionarySource = resourceAssignment.dictionarySource!!
\r
71 log.debug("Enriching Assignment name({}), dictionary name({}), source({})", resourceAssignment.name,
\r
72 dictionaryName, dictionarySource)
\r
73 val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)
\r
75 // Add Unique Node Types
\r
76 uniqueSourceNodeTypeNames.add(sourceNodeTypeName)
\r
78 // TODO("Candidate for Optimisation")
\r
79 if (checkResourceDefinitionNeeded(resourceAssignment)) {
\r
81 bluePrintEnhancerService.enrichPropertyDefinition(resourceAssignment.name, resourceAssignment.property!!);
\r
83 // Get the Resource Definition from Repo
\r
84 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)
\r
86 val sourceNodeTemplate = resourceDefinition.sources.get(dictionarySource)
\r
87 ?: throw BluePrintException(format("failed to get assigned dictionarySource({}) from resourceDefinition({})", dictionarySource, dictionaryName))
\r
89 // Enrich as NodeTemplate
\r
90 bluePrintEnhancerService.enrichNodeTemplate(dictionarySource, sourceNodeTemplate)
\r
93 // Enrich the ResourceSource NodeTypes
\r
94 uniqueSourceNodeTypeNames.map { nodeTypeName ->
\r
95 resourceDefinitionRepoService.getNodeType(nodeTypeName).subscribe { nodeType ->
\r
96 bluePrintEnhancerService.enrichNodeType(nodeTypeName, nodeType)
\r
102 override fun enhanceBluePrint(resourceAssignments: List<ResourceAssignment>): ServiceTemplate {
\r
103 val bluePrintEnhancerService = BluePrintEnhancerDefaultService(resourceDefinitionRepoService)
\r
104 bluePrintEnhancerService.serviceTemplate = ServiceTemplate()
\r
105 bluePrintEnhancerService.initialCleanUp()
\r
106 enhanceBluePrint(bluePrintEnhancerService, resourceAssignments)
\r
107 return bluePrintEnhancerService.serviceTemplate
\r
110 private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {
\r
111 return !((resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT)
\r
112 || resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT))
\r
113 && BluePrintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!))
\r
116 private fun getResourceDefinition(name: String): ResourceDefinition {
\r
117 return resourceDefinitionRepoService.getResourceDefinition(name).block()
\r
118 ?: throw BluePrintException(format("failed to get dictionary definition({})", name))
\r