313d345da18096641975afcdcfc32cb91cb1fee8
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / generic / GenericTypeBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.components.impl.generic;
21
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;
41
42 @org.springframework.stereotype.Component
43 public class GenericTypeBusinessLogic {
44
45     private final static Logger log = Logger.getLogger(GenericTypeBusinessLogic.class);
46     private final ComponentsUtils componentsUtils;
47     private final ToscaOperationFacade toscaOperationFacade;
48
49     @Autowired
50     public GenericTypeBusinessLogic(ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade) {
51         this.componentsUtils = componentsUtils;
52         this.toscaOperationFacade = toscaOperationFacade;
53     }
54
55     /**
56      * @param component the component of which to fetch its generic type
57      * @return the generic node type which corresponds to the given component
58      */
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));
65         }
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));
73             }
74         } else {
75             genericType = toscaOperationFacade.getByToscaResourceNameAndVersion(genericTypeToscaName, component.getDerivedFromGenericVersion());
76         }
77
78         Resource genericTypeResource = genericType.left().value();
79         return Either.left(genericTypeResource);
80     }
81
82     /**
83      * @param genericType the generic node type
84      * @return the generic type properties as inputs
85      */
86     public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
87         List<PropertyDefinition> genericTypeProps = genericType.getProperties();
88         if (null != genericTypeProps) {
89             return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
90         }
91         return new ArrayList<>();
92     }
93
94     public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
95         return genericTypeProps.stream().map(p -> setInputDefinitionFromProp(p, genericUniqueId)).collect(Collectors.toList());
96     }
97
98     private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId) {
99         InputDefinition input = new InputDefinition(prop);
100         input.setOwnerId(genericUniqueId);
101         return input;
102     }
103
104     private <T extends Component> String getGenericTypeToscaName(T component) {
105         if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()) {
106             return component.getDerivedFromGenericType();
107         }
108         return isCvfcHasDerivedFrom(component) ? ((Resource) component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
109     }
110
111     private <T extends Component> boolean isCvfcHasDerivedFrom(T component) {
112         return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC
113             && CollectionUtils.isNotEmpty(((Resource) component).getDerivedFrom());
114     }
115 }