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=========================================================
20 package org.openecomp.sdc.be.components.impl.generic;
22 import fj.data.Either;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.InputDefinition;
34 import org.openecomp.sdc.be.model.PropertyDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.openecomp.sdc.exception.ResponseFormat;
40 import org.springframework.beans.factory.annotation.Autowired;
42 @org.springframework.stereotype.Component
43 public class GenericTypeBusinessLogic {
45 private final static Logger log = Logger.getLogger(GenericTypeBusinessLogic.class);
46 private final ComponentsUtils componentsUtils;
47 private final ToscaOperationFacade toscaOperationFacade;
50 public GenericTypeBusinessLogic(ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade) {
51 this.componentsUtils = componentsUtils;
52 this.toscaOperationFacade = toscaOperationFacade;
56 * @param component the component of which to fetch its generic type
57 * @return the generic node type which corresponds to the given component
59 public Either<Resource, ResponseFormat> fetchDerivedFromGenericType(Component component) {
60 String genericTypeToscaName = getGenericTypeToscaName(component);
61 log.debug("Fetching generic tosca name {}", genericTypeToscaName);
62 if (null == genericTypeToscaName) {
63 log.debug("Failed to fetch certified generic node type for component {}", component.getName());
64 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
66 Either<Resource, StorageOperationStatus> genericType;
67 if (StringUtils.isEmpty(component.getDerivedFromGenericVersion())){
68 genericType = toscaOperationFacade
69 .getLatestCertifiedNodeTypeByToscaResourceName(genericTypeToscaName);
70 if (genericType.isRight()) {
71 log.debug("Failed to fetch certified node type by tosca resource name {}", genericTypeToscaName);
72 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), genericTypeToscaName));
75 genericType = toscaOperationFacade.getByToscaResourceNameAndVersion(genericTypeToscaName, component.getDerivedFromGenericVersion());
78 Resource genericTypeResource = genericType.left().value();
79 return Either.left(genericTypeResource);
82 public Either<Resource, ResponseFormat> fetchDerivedFromGenericType(final Component component, final String toscaType) {
83 if (StringUtils.isNotEmpty(toscaType)) {
84 final Either<Resource, StorageOperationStatus> genericType = toscaOperationFacade.getLatestByToscaResourceNameAndModel(toscaType, component.getModel());
85 if (genericType.isRight()) {
86 log.debug("Failed to fetch certified node type by tosca resource name {}", toscaType);
87 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType(), toscaType));
89 return Either.left(genericType.left().value());
91 return fetchDerivedFromGenericType(component);
95 * @param genericType the generic node type
96 * @return the generic type properties as inputs
98 public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
99 List<PropertyDefinition> genericTypeProps = genericType.getProperties();
100 if (null != genericTypeProps) {
101 return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
103 return new ArrayList<>();
106 public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
107 return genericTypeProps.stream().map(p -> setInputDefinitionFromProp(p, genericUniqueId)).collect(Collectors.toList());
110 private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId) {
111 InputDefinition input = new InputDefinition(prop);
112 input.setOwnerId(genericUniqueId);
116 private <T extends Component> String getGenericTypeToscaName(T component) {
117 if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()) {
118 return component.getDerivedFromGenericType();
120 return isCvfcHasDerivedFrom(component) ? ((Resource) component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
123 private <T extends Component> boolean isCvfcHasDerivedFrom(T component) {
124 return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC
125 && CollectionUtils.isNotEmpty(((Resource) component).getDerivedFrom());