diff options
Diffstat (limited to 'ms')
2 files changed, 174 insertions, 210 deletions
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.java deleted file mode 100644 index b9eff7624..000000000 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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.controllerblueprints.service; - -import com.google.common.base.Preconditions; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException; -import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition; -import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment; -import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition; -import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils; -import org.onap.ccsdk.cds.controllerblueprints.service.domain.ResourceDictionary; -import org.onap.ccsdk.cds.controllerblueprints.service.model.AutoMapResponse; -import org.onap.ccsdk.cds.controllerblueprints.service.repository.ResourceDictionaryRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * AutoResourceMappingService.java Purpose: Provide Automapping of Resource Assignments AutoResourceMappingService - * - * @author Brinda Santh - * @version 1.0 - */ - -@Service -@SuppressWarnings("unused") -public class AutoResourceMappingService { - - private static Logger log = LoggerFactory.getLogger(AutoResourceMappingService.class); - - private ResourceDictionaryRepository dataDictionaryRepository; - - /** - * This is a AutoResourceMappingService constructor - * - * @param dataDictionaryRepository dataDictionaryRepository - */ - public AutoResourceMappingService(ResourceDictionaryRepository dataDictionaryRepository) { - this.dataDictionaryRepository = dataDictionaryRepository; - } - - /** - * This is a autoMap service to map the template keys automatically to Dictionary fields. - * - * @param resourceAssignments resourceAssignments - * @return AutoMapResponse - */ - public AutoMapResponse autoMap(List<ResourceAssignment> resourceAssignments) throws BluePrintException { - AutoMapResponse autoMapResponse = new AutoMapResponse(); - try { - if (CollectionUtils.isNotEmpty(resourceAssignments)) { - - // Create the Dictionary definitions for the ResourceAssignment Names - Map<String, ResourceDictionary> dictionaryMap = getDictionaryDefinitions(resourceAssignments); - - for (ResourceAssignment resourceAssignment : resourceAssignments) { - if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getName()) - && StringUtils.isBlank(resourceAssignment.getDictionaryName())) { - - populateDictionaryMapping(dictionaryMap, resourceAssignment); - - log.info("Mapped Resource : {}", resourceAssignment); - - } - } - } - List<ResourceDictionary> dictionaries = getDictionaryDefinitionsList(resourceAssignments); - List<ResourceAssignment> resourceAssignmentsFinal = getAllAutomapResourceAssignments(resourceAssignments); - autoMapResponse.setDataDictionaries(dictionaries); - autoMapResponse.setResourceAssignments(resourceAssignmentsFinal); - } catch (Exception e) { - log.error(String.format("Failed in auto process %s", e.getMessage())); - throw new BluePrintException(e.getMessage(), e); - } - return autoMapResponse; - } - - private void populateDictionaryMapping(Map<String, ResourceDictionary> dictionaryMap, ResourceAssignment resourceAssignment) { - ResourceDictionary dbDataDictionary = dictionaryMap.get(resourceAssignment.getName()); - if (dbDataDictionary != null && dbDataDictionary.getDefinition() != null) { - - ResourceDefinition dictionaryDefinition = dbDataDictionary.getDefinition(); - - if (dictionaryDefinition != null && StringUtils.isNotBlank(dictionaryDefinition.getName()) - && StringUtils.isBlank(resourceAssignment.getDictionaryName())) { - - resourceAssignment.setDictionaryName(dbDataDictionary.getName()); - ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, dictionaryDefinition); - } - } - } - - private Map<String, ResourceDictionary> getDictionaryDefinitions(List<ResourceAssignment> resourceAssignments) { - Map<String, ResourceDictionary> dictionaryMap = new HashMap<>(); - List<String> names = new ArrayList<>(); - for (ResourceAssignment resourceAssignment : resourceAssignments) { - if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getName())) { - names.add(resourceAssignment.getName()); - } - } - if (CollectionUtils.isNotEmpty(names)) { - - List<ResourceDictionary> dictionaries = dataDictionaryRepository.findByNameIn(names); - if (CollectionUtils.isNotEmpty(dictionaries)) { - for (ResourceDictionary dataDictionary : dictionaries) { - if (dataDictionary != null && StringUtils.isNotBlank(dataDictionary.getName())) { - dictionaryMap.put(dataDictionary.getName(), dataDictionary); - } - } - } - } - return dictionaryMap; - - } - - private List<ResourceDictionary> getDictionaryDefinitionsList(List<ResourceAssignment> resourceAssignments) { - List<ResourceDictionary> dictionaries = null; - List<String> names = new ArrayList<>(); - for (ResourceAssignment resourceAssignment : resourceAssignments) { - if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getDictionaryName())) { - - if (!names.contains(resourceAssignment.getDictionaryName())) { - names.add(resourceAssignment.getDictionaryName()); - } - - if (resourceAssignment.getDependencies() != null && !resourceAssignment.getDependencies().isEmpty()) { - List<String> dependencyNames = resourceAssignment.getDependencies(); - for (String dependencyName : dependencyNames) { - if (StringUtils.isNotBlank(dependencyName) && !names.contains(dependencyName)) { - names.add(dependencyName); - } - } - } - } - } - if (CollectionUtils.isNotEmpty(names)) { - dictionaries = dataDictionaryRepository.findByNameIn(names); - } - return dictionaries; - - } - - private List<ResourceAssignment> getAllAutomapResourceAssignments(List<ResourceAssignment> resourceAssignments) { - List<ResourceDictionary> dictionaries = null; - List<String> names = new ArrayList<>(); - for (ResourceAssignment resourceAssignment : resourceAssignments) { - if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getDictionaryName())) { - if (resourceAssignment.getDependencies() != null && !resourceAssignment.getDependencies().isEmpty()) { - List<String> dependencieNames = resourceAssignment.getDependencies(); - for (String dependencieName : dependencieNames) { - if (StringUtils.isNotBlank(dependencieName) && !names.contains(dependencieName) - && !checkAssignmentsExists(resourceAssignments, dependencieName)) { - names.add(dependencieName); - } - } - } - } - } - - if (!names.isEmpty()) { - dictionaries = dataDictionaryRepository.findByNameIn(names); - } - if (dictionaries != null) { - for (ResourceDictionary resourcedictionary : dictionaries) { - ResourceDefinition dictionaryDefinition = resourcedictionary.getDefinition(); - Preconditions.checkNotNull(dictionaryDefinition, "failed to get Resource Definition from dictionary definition"); - PropertyDefinition property = new PropertyDefinition(); - property.setRequired(true); - ResourceAssignment resourceAssignment = new ResourceAssignment(); - resourceAssignment.setName(resourcedictionary.getName()); - resourceAssignment.setDictionaryName(resourcedictionary - .getName()); - resourceAssignment.setVersion(0); - resourceAssignment.setProperty(property); - ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, dictionaryDefinition); - resourceAssignments.add(resourceAssignment); - } - } - return resourceAssignments; - - } - - - private boolean checkAssignmentsExists(List<ResourceAssignment> resourceAssignmentsWithDepencies, String resourceName) { - return resourceAssignmentsWithDepencies.stream().anyMatch(names -> names.getName().equalsIgnoreCase(resourceName)); - } - -} diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.kt new file mode 100644 index 000000000..3ab9fee58 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/AutoResourceMappingService.kt @@ -0,0 +1,174 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 Huawei. + * + * 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.controllerblueprints.service + +import com.google.common.base.Preconditions +import org.apache.commons.collections.CollectionUtils +import org.apache.commons.lang3.StringUtils +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils +import org.onap.ccsdk.cds.controllerblueprints.service.domain.ResourceDictionary +import org.onap.ccsdk.cds.controllerblueprints.service.model.AutoMapResponse +import org.onap.ccsdk.cds.controllerblueprints.service.repository.ResourceDictionaryRepository +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Service +import java.util.* + +@Service +open class AutoResourceMappingService(private val dataDictionaryRepository: ResourceDictionaryRepository) { + + private val log = LoggerFactory.getLogger(AutoResourceMappingService::class.java) + + @Throws(BluePrintException::class) + fun autoMap(resourceAssignments: MutableList<ResourceAssignment>): + AutoMapResponse { + val autoMapResponse = AutoMapResponse() + try { + if (CollectionUtils.isNotEmpty(resourceAssignments)) { + // Create the Dictionary definitions for the ResourceAssignment Names + val dictionaryMap = getDictionaryDefinitions(resourceAssignments) + + for (resourceAssignment in resourceAssignments) { + if (StringUtils.isNotBlank(resourceAssignment.name) + && StringUtils.isBlank(resourceAssignment.dictionaryName)) { + populateDictionaryMapping(dictionaryMap, resourceAssignment) + log.info("Mapped Resource : {}", resourceAssignment) + } + } + } + val dictionaries = getDictionaryDefinitionsList(resourceAssignments) + val resourceAssignmentsFinal = getAllAutoMapResourceAssignments(resourceAssignments) + autoMapResponse.dataDictionaries = dictionaries + autoMapResponse.resourceAssignments = resourceAssignmentsFinal + } catch (e: Exception) { + log.error(String.format("Failed in auto process %s", e.message)) + throw BluePrintException(e, e.message!!) + } + + return autoMapResponse + } + + private fun populateDictionaryMapping(dictionaryMap: Map<String, ResourceDictionary>, resourceAssignment: ResourceAssignment) { + val dbDataDictionary = dictionaryMap[resourceAssignment.name] + if (dbDataDictionary != null && dbDataDictionary.definition != null) { + + val dictionaryDefinition = dbDataDictionary.definition + + if (dictionaryDefinition != null && StringUtils.isNotBlank(dictionaryDefinition.name) + && StringUtils.isBlank(resourceAssignment.dictionaryName)) { + + resourceAssignment.dictionaryName = dbDataDictionary.name + ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, dictionaryDefinition) + } + } + } + + private fun getDictionaryDefinitions(resourceAssignments: List<ResourceAssignment>): Map<String, ResourceDictionary> { + val dictionaryMap = HashMap<String, ResourceDictionary>() + val names = ArrayList<String>() + for (resourceAssignment in resourceAssignments) { + if (StringUtils.isNotBlank(resourceAssignment.name)) { + names.add(resourceAssignment.name) + } + } + if (CollectionUtils.isNotEmpty(names)) { + + val dictionaries = dataDictionaryRepository.findByNameIn(names) + if (CollectionUtils.isNotEmpty(dictionaries)) { + for (dataDictionary in dictionaries) { + if (StringUtils.isNotBlank(dataDictionary.name)) { + dictionaryMap[dataDictionary.name] = dataDictionary + } + } + } + } + return dictionaryMap + + } + private fun getDictionaryDefinitionsList(resourceAssignments: List<ResourceAssignment>): List<ResourceDictionary>? { + var dictionaries: List<ResourceDictionary>? = null + val names = ArrayList<String>() + for (resourceAssignment in resourceAssignments) { + if (StringUtils.isNotBlank(resourceAssignment.dictionaryName)) { + + if (!names.contains(resourceAssignment.dictionaryName)) { + names.add(resourceAssignment.dictionaryName!!) + } + + if (resourceAssignment.dependencies != null && !resourceAssignment.dependencies!!.isEmpty()) { + val dependencyNames = resourceAssignment.dependencies + for (dependencyName in dependencyNames!!) { + if (StringUtils.isNotBlank(dependencyName) && !names.contains(dependencyName)) { + names.add(dependencyName) + } + } + } + } + } + if (CollectionUtils.isNotEmpty(names)) { + dictionaries = dataDictionaryRepository.findByNameIn(names) + } + return dictionaries + + } + + private fun getAllAutoMapResourceAssignments(resourceAssignments: MutableList<ResourceAssignment>): List<ResourceAssignment> { + var dictionaries: List<ResourceDictionary>? = null + val names = ArrayList<String>() + for (resourceAssignment in resourceAssignments) { + if (StringUtils.isNotBlank(resourceAssignment.dictionaryName)) { + if (resourceAssignment.dependencies != null && !resourceAssignment.dependencies!!.isEmpty()) { + val dependencyNames = resourceAssignment.dependencies + for (dependencyName in dependencyNames!!) { + if (StringUtils.isNotBlank(dependencyName) && !names.contains(dependencyName) + && !checkAssignmentsExists(resourceAssignments, dependencyName)) { + names.add(dependencyName) + } + } + } + } + } + + if (!names.isEmpty()) { + dictionaries = dataDictionaryRepository.findByNameIn(names) + } + if (dictionaries != null) { + for (rscDictionary in dictionaries) { + val dictionaryDefinition = rscDictionary.definition + Preconditions.checkNotNull(dictionaryDefinition, "failed to get Resource Definition from dictionary definition") + val property = PropertyDefinition() + property.required = true + val resourceAssignment = ResourceAssignment() + resourceAssignment.name = rscDictionary.name + resourceAssignment.dictionaryName = rscDictionary.name + resourceAssignment.version = 0 + resourceAssignment.property = property + ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, dictionaryDefinition) + resourceAssignments.add(resourceAssignment) + } + } + return resourceAssignments + } + + + private fun checkAssignmentsExists(resourceAssignmentsWithDepencies: List<ResourceAssignment>, resourceName: String): Boolean { + return resourceAssignmentsWithDepencies.stream().anyMatch { names -> names.name.equals(resourceName, ignoreCase = true) } + } +}
\ No newline at end of file |