6cf4327a9957bd1d020904932b0b9e08029c3327
[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     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));
88             }
89             return Either.left(genericType.left().value());
90         }
91         return fetchDerivedFromGenericType(component);
92     }
93
94     /**
95      * @param genericType the generic node type
96      * @return the generic type properties as inputs
97      */
98     public List<InputDefinition> generateInputsFromGenericTypeProperties(Resource genericType) {
99         List<PropertyDefinition> genericTypeProps = genericType.getProperties();
100         if (null != genericTypeProps) {
101             return convertGenericTypePropertiesToInputsDefintion(genericTypeProps, genericType.getUniqueId());
102         }
103         return new ArrayList<>();
104     }
105
106     public List<InputDefinition> convertGenericTypePropertiesToInputsDefintion(List<PropertyDefinition> genericTypeProps, String genericUniqueId) {
107         return genericTypeProps.stream().map(p -> setInputDefinitionFromProp(p, genericUniqueId)).collect(Collectors.toList());
108     }
109
110     private InputDefinition setInputDefinitionFromProp(PropertyDefinition prop, String genericUniqueId) {
111         InputDefinition input = new InputDefinition(prop);
112         input.setOwnerId(genericUniqueId);
113         return input;
114     }
115
116     private <T extends Component> String getGenericTypeToscaName(T component) {
117         if (component.getDerivedFromGenericType() != null && !component.getDerivedFromGenericType().isEmpty()) {
118             return component.getDerivedFromGenericType();
119         }
120         return isCvfcHasDerivedFrom(component) ? ((Resource) component).getDerivedFrom().get(0) : component.fetchGenericTypeToscaNameFromConfig();
121     }
122
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());
126     }
127 }