Added oparent to sdc main
[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
21 package org.openecomp.sdc.be.components.impl.generic;
22
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;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.stream.Collectors;
42
43 @org.springframework.stereotype.Component
44 public class GenericTypeBusinessLogic {
45
46     private final static Logger log = Logger.getLogger(GenericTypeBusinessLogic.class);
47
48     @Autowired
49     private ComponentsUtils componentsUtils;
50
51     @Autowired
52     private ToscaOperationFacade toscaOperationFacade;
53
54     /**
55      * @param component the component of which to fetch its generic type
56      * @return the generic node type which corresponds to the given component
57      */
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));
64         }
65
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));
70         }
71
72         Resource genericTypeResource = findLatestGeneric.left().value();
73         return Either.left(genericTypeResource);
74     }
75
76     /**
77      *
78      * @param genericType the generic node type
79      * @return the generic type properties as inputs
80      */
81     public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
82         List<PropertyDefinition> genericTypeProps = genericType.getProperties();
83         if(null != genericTypeProps) {
84             return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
85         }
86         return new ArrayList<>();
87     }
88
89     public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
90         return genericTypeProps.stream()
91                 .map(p -> setInputDefinitionFromProp(p, genericUniqueId))
92                 .collect(Collectors.toList());
93     }
94
95     private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId){
96         InputDefinition input = new InputDefinition(prop);
97         input.setOwnerId(genericUniqueId);
98         return input;
99     }
100
101     private <T extends Component> String getGenericTypeToscaName(T component) {
102         if(component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()){
103             return component.getDerivedFromGenericType();
104         }
105         return isCvfcHasDerivedFrom(component) ? ((Resource)component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
106     }
107
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());
110     }
111
112 }