1 package org.openecomp.sdc.be.components.impl.generic;
4 import org.apache.commons.collections.CollectionUtils;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
7 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.model.Component;
10 import org.openecomp.sdc.be.model.InputDefinition;
11 import org.openecomp.sdc.be.model.PropertyDefinition;
12 import org.openecomp.sdc.be.model.Resource;
13 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
14 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
15 import org.openecomp.sdc.exception.ResponseFormat;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.springframework.beans.factory.annotation.Autowired;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.stream.Collectors;
24 @org.springframework.stereotype.Component
25 public class GenericTypeBusinessLogic {
27 private final static Logger log = LoggerFactory.getLogger(GenericTypeBusinessLogic.class);
30 private ComponentsUtils componentsUtils;
33 private ToscaOperationFacade toscaOperationFacade;
36 * @param component the component of which to fetch its generic type
37 * @return the generic node type which corresponds to the given component
39 public Either<Resource, ResponseFormat> fetchDerivedFromGenericType(Component component){
40 String genericTypeToscaName = getGenericTypeToscaName(component);
41 log.debug("Fetching generic tosca name {}", genericTypeToscaName);
42 if(null == genericTypeToscaName) {
43 log.debug("Failed to fetch certified generic node type for component {}", component.getName());
44 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
47 Either<Resource, StorageOperationStatus> findLatestGeneric = toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(genericTypeToscaName);
48 if(findLatestGeneric.isRight()){
49 log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName);
50 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName));
53 Resource genericTypeResource = findLatestGeneric.left().value();
54 return Either.left(genericTypeResource);
59 * @param genericType the generic node type
60 * @return the generic type properties as inputs
62 public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
63 List<PropertyDefinition> genericTypeProps = genericType.getProperties();
64 if(null != genericTypeProps) {
65 return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
67 return new ArrayList<>();
70 public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
71 return genericTypeProps.stream()
72 .map(p -> setInputDefinitionFromProp(p, genericUniqueId))
73 .collect(Collectors.toList());
76 private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId){
77 InputDefinition input = new InputDefinition(prop);
78 input.setOwnerId(genericUniqueId);
82 private <T extends Component> String getGenericTypeToscaName(T component) {
83 if(component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()){
84 return component.getDerivedFromGenericType();
86 return isCvfcHasDerivedFrom(component) ? ((Resource)component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
89 private <T extends Component> boolean isCvfcHasDerivedFrom(T component) {
90 return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource)component).getResourceType() == ResourceTypeEnum.CVFC && CollectionUtils.isNotEmpty(((Resource)component).getDerivedFrom());