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.common.log.wrappers.Logger;
16 import org.openecomp.sdc.exception.ResponseFormat;
17 import org.springframework.beans.factory.annotation.Autowired;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.stream.Collectors;
23 @org.springframework.stereotype.Component
24 public class GenericTypeBusinessLogic {
26 private final static Logger log = Logger.getLogger(GenericTypeBusinessLogic.class);
29 private ComponentsUtils componentsUtils;
32 private ToscaOperationFacade toscaOperationFacade;
35 * @param component the component of which to fetch its generic type
36 * @return the generic node type which corresponds to the given component
38 public Either<Resource, ResponseFormat> fetchDerivedFromGenericType(Component component){
39 String genericTypeToscaName = getGenericTypeToscaName(component);
40 log.debug("Fetching generic tosca name {}", genericTypeToscaName);
41 if(null == genericTypeToscaName) {
42 log.debug("Failed to fetch certified generic node type for component {}", component.getName());
43 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
46 Either<Resource, StorageOperationStatus> findLatestGeneric = toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(genericTypeToscaName);
47 if(findLatestGeneric.isRight()){
48 log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName);
49 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName));
52 Resource genericTypeResource = findLatestGeneric.left().value();
53 return Either.left(genericTypeResource);
58 * @param genericType the generic node type
59 * @return the generic type properties as inputs
61 public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
62 List<PropertyDefinition> genericTypeProps = genericType.getProperties();
63 if(null != genericTypeProps) {
64 return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
66 return new ArrayList<>();
69 public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
70 return genericTypeProps.stream()
71 .map(p -> setInputDefinitionFromProp(p, genericUniqueId))
72 .collect(Collectors.toList());
75 private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId){
76 InputDefinition input = new InputDefinition(prop);
77 input.setOwnerId(genericUniqueId);
81 private <T extends Component> String getGenericTypeToscaName(T component) {
82 if(component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()){
83 return component.getDerivedFromGenericType();
85 return isCvfcHasDerivedFrom(component) ? ((Resource)component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
88 private <T extends Component> boolean isCvfcHasDerivedFrom(T component) {
89 return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource)component).getResourceType() == ResourceTypeEnum.CVFC && CollectionUtils.isNotEmpty(((Resource)component).getDerivedFrom());