2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl.generic;
23 import fj.data.Either;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.InputDefinition;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.openecomp.sdc.exception.ResponseFormat;
37 import org.springframework.beans.factory.annotation.Autowired;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.stream.Collectors;
43 @org.springframework.stereotype.Component
44 public class GenericTypeBusinessLogic {
46 private final static Logger log = Logger.getLogger(GenericTypeBusinessLogic.class);
49 private ComponentsUtils componentsUtils;
52 private ToscaOperationFacade toscaOperationFacade;
55 * @param component the component of which to fetch its generic type
56 * @return the generic node type which corresponds to the given component
58 public Either<Resource, ResponseFormat> fetchDerivedFromGenericType(Component component){
59 String genericTypeToscaName = getGenericTypeToscaName(component);
60 log.debug("Fetching generic tosca name {}", genericTypeToscaName);
61 if(null == genericTypeToscaName) {
62 log.debug("Failed to fetch certified generic node type for component {}", component.getName());
63 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
66 Either<Resource, StorageOperationStatus> findLatestGeneric = toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(genericTypeToscaName);
67 if(findLatestGeneric.isRight()){
68 log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName);
69 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName));
72 Resource genericTypeResource = findLatestGeneric.left().value();
73 return Either.left(genericTypeResource);
78 * @param genericType the generic node type
79 * @return the generic type properties as inputs
81 public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
82 List<PropertyDefinition> genericTypeProps = genericType.getProperties();
83 if(null != genericTypeProps) {
84 return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
86 return new ArrayList<>();
89 public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
90 return genericTypeProps.stream()
91 .map(p -> setInputDefinitionFromProp(p, genericUniqueId))
92 .collect(Collectors.toList());
95 private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId){
96 InputDefinition input = new InputDefinition(prop);
97 input.setOwnerId(genericUniqueId);
101 private <T extends Component> String getGenericTypeToscaName(T component) {
102 if(component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()){
103 return component.getDerivedFromGenericType();
105 return isCvfcHasDerivedFrom(component) ? ((Resource)component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
108 private <T extends Component> boolean isCvfcHasDerivedFrom(T component) {
109 return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource)component).getResourceType() == ResourceTypeEnum.CVFC && CollectionUtils.isNotEmpty(((Resource)component).getDerivedFrom());