2fedcc3b28c2e34058bb474b532c11450a15478b
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / info / ArtifactTemplateInfo.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.info;
21
22 import com.google.gson.Gson;
23 import com.google.gson.JsonObject;
24 import fj.data.Either;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import lombok.Getter;
32 import lombok.NoArgsConstructor;
33 import lombok.Setter;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.openecomp.sdc.be.config.ArtifactConfiguration;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.config.ComponentType;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.ArtifactType;
43 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
44 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
46 import org.openecomp.sdc.exception.ResponseFormat;
47
48 @Getter
49 @Setter
50 @NoArgsConstructor
51 public class ArtifactTemplateInfo {
52
53     public static final String CSAR_ARTIFACT = "artifacts";
54     private static final Logger log = Logger.getLogger(ArtifactTemplateInfo.class);
55     private static final Gson gson = new Gson();
56     private static final String ARTIFACT_TEMPLATE_TYPE = "type";
57     private static final String FILE_NAME = "fileName";
58     private static final String ARTIFACT_TEMPLATE_ENV = "env";
59     private static final String IS_BASE = "isBase";
60     private static final String CSAR_HEAT = "HEAT";
61     private static final String CSAR_HELM = "HELM";
62     private static final String CSAR_NETWORK = "network";
63     private static final String CSAR_VOLUME = "volume";
64     private static final String CSAR_NESTED = "nested";
65     private static final String DESC = "description";
66     private String type;
67     private String fileName;
68     private String env;
69     private boolean base;
70     private String groupName;
71     private String description;
72     private List<ArtifactTemplateInfo> relatedArtifactsInfo;
73
74     public ArtifactTemplateInfo(String type, String fileName, String env, List<ArtifactTemplateInfo> relatedArtifactsInfo) {
75         this.type = type;
76         this.fileName = fileName;
77         this.env = env;
78         this.relatedArtifactsInfo = relatedArtifactsInfo;
79     }
80
81     public static Either<ArtifactTemplateInfo, ResponseFormat> createArtifactTemplateInfoFromJson(ComponentsUtils componentsUtils, String type,
82                                                                                                   Map<String, Object> o,
83                                                                                                   List<ArtifactTemplateInfo> createdArtifactTemplateInfoList,
84                                                                                                   ArtifactTemplateInfo parentArtifact) {
85         String content = gson.toJson(o);
86         JsonObject jsonElement = new JsonObject();
87         ArtifactTemplateInfo resourceInfo = new ArtifactTemplateInfo();
88         jsonElement = gson.fromJson(content, jsonElement.getClass());
89         Map<String, Object> artifactTemplateMap = ComponentsUtils.parseJsonToObject(jsonElement.toString(), HashMap.class);
90         if (artifactTemplateMap.containsKey(ARTIFACT_TEMPLATE_TYPE)) {
91             resourceInfo.setType((String) artifactTemplateMap.get(ARTIFACT_TEMPLATE_TYPE));
92         }
93         if (artifactTemplateMap.containsKey(FILE_NAME)) {
94             resourceInfo.setFileName((String) artifactTemplateMap.get(FILE_NAME));
95         }
96         if (artifactTemplateMap.containsKey(IS_BASE)) {
97             resourceInfo.setBase((Boolean) artifactTemplateMap.get(IS_BASE));
98         }
99         if (artifactTemplateMap.containsKey(ARTIFACT_TEMPLATE_ENV)) {
100             Object envObj = artifactTemplateMap.get(ARTIFACT_TEMPLATE_ENV);
101             String envStr = "";
102             if (envObj instanceof String) {
103                 envStr = (String) envObj;
104             } else if (envObj instanceof Map) {
105                 Map envMap = (Map) envObj;
106                 if (envMap.containsKey(FILE_NAME)) {
107                     envStr = (String) envMap.get(FILE_NAME);
108                 }
109             }
110             resourceInfo.setEnv(envStr);
111         }
112         if (artifactTemplateMap.containsKey(DESC)) {
113             resourceInfo.setDescription((String) artifactTemplateMap.get(DESC));
114         } else {
115             resourceInfo.setDescription((String) artifactTemplateMap.get(FILE_NAME));
116         }
117         boolean artifactTypeExist = false;
118         String correctType;
119         if (type.equalsIgnoreCase(CSAR_NESTED)) {
120             correctType = ArtifactTypeEnum.HEAT_NESTED.getType();
121         } else if (type.equalsIgnoreCase(CSAR_VOLUME)) {
122             correctType = ArtifactTypeEnum.HEAT_VOL.getType();
123         } else if (type.equalsIgnoreCase(CSAR_NETWORK)) {
124             correctType = ArtifactTypeEnum.HEAT_NET.getType();
125         } else if (type.equalsIgnoreCase(CSAR_ARTIFACT)) {
126             if (parentArtifact != null) {
127                 correctType = ArtifactTypeEnum.HEAT_ARTIFACT.getType();
128             } else {
129                 correctType = resourceInfo.type;
130             }
131         } else if (type.equalsIgnoreCase(CSAR_HEAT)) {
132             correctType = ArtifactTypeEnum.HEAT.getType();
133         } else if (type.equalsIgnoreCase(CSAR_HELM)) {
134             correctType = ArtifactTypeEnum.HELM.getType();
135         } else {
136             correctType = ArtifactTypeEnum.OTHER.getType();
137         }
138         Either<List<ArtifactType>, ActionStatus> allArtifactTypes = getDeploymentArtifactTypes(NodeTypeEnum.Resource);
139         if (allArtifactTypes.isRight()) {
140             BeEcompErrorManager.getInstance()
141                 .logBeInvalidConfigurationError("Artifact Upload / Update", "artifactTypes", allArtifactTypes.right().value().name());
142             return Either.right(componentsUtils.getResponseFormat(ActionStatus.FAILED_RETRIVE_ARTIFACTS_TYPES));
143         }
144         for (ArtifactType artType : allArtifactTypes.left().value()) {
145             if (artType.getName().contains(correctType)) {
146                 resourceInfo.type = artType.getName();
147                 artifactTypeExist = true;
148                 break;
149             }
150         }
151         if (!artifactTypeExist) {
152             BeEcompErrorManager.getInstance().logBeInvalidTypeError("Artifact", "-Not supported artifact type ", correctType);
153             log.debug("Not supported artifact type = {}", correctType);
154             return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, correctType));
155         }
156         Either<Boolean, ResponseFormat> eitherNeedToCreate = validateEnv(componentsUtils, createdArtifactTemplateInfoList, resourceInfo);
157         if (eitherNeedToCreate.isRight()) {
158             return Either.right(eitherNeedToCreate.right().value());
159         }
160         eitherNeedToCreate = validateParentType(componentsUtils, resourceInfo, parentArtifact);
161         if (eitherNeedToCreate.isRight()) {
162             return Either.right(eitherNeedToCreate.right().value());
163         }
164         eitherNeedToCreate = validateIsAlreadyExist(componentsUtils, resourceInfo, createdArtifactTemplateInfoList, parentArtifact);
165         if (eitherNeedToCreate.isRight()) {
166             return Either.right(eitherNeedToCreate.right().value());
167         }
168         Set<String> keys = o.keySet();
169         for (String key : keys) {
170             if (o.get(key) instanceof List) {
171                 List<Map<String, Object>> artifList = (List<Map<String, Object>>) o.get(key);
172                 for (Map<String, Object> relatedArtifactsMap : artifList) {
173                     Either<ArtifactTemplateInfo, ResponseFormat> relatedArtifact = ArtifactTemplateInfo
174                         .createArtifactTemplateInfoFromJson(componentsUtils, key, relatedArtifactsMap, createdArtifactTemplateInfoList, resourceInfo);
175                     if (relatedArtifact.isRight()) {
176                         return relatedArtifact;
177                     }
178                     if (resourceInfo.relatedArtifactsInfo == null) {
179                         resourceInfo.relatedArtifactsInfo = new ArrayList<>();
180                     }
181                     resourceInfo.relatedArtifactsInfo.add(relatedArtifact.left().value());
182                 }
183             }
184         }
185         return Either.left(resourceInfo);
186     }
187
188     private static Either<Boolean, ResponseFormat> validateIsAlreadyExist(ComponentsUtils componentsUtils, ArtifactTemplateInfo resourceInfo,
189                                                                           List<ArtifactTemplateInfo> createdArtifactTemplateInfoList,
190                                                                           ArtifactTemplateInfo parentArtifact) {
191         if (parentArtifact == null) {
192             if (createdArtifactTemplateInfoList == null || createdArtifactTemplateInfoList.isEmpty()) {
193                 return Either.left(true);
194             }
195             for (ArtifactTemplateInfo createdArtifact : createdArtifactTemplateInfoList) {
196                 if (createdArtifact.getType().equalsIgnoreCase(resourceInfo.getType()) && createdArtifact.getFileName()
197                     .equalsIgnoreCase(resourceInfo.getFileName())) {
198                     return Either.right(componentsUtils
199                         .getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_MASTER_IN_CSAR, resourceInfo.getFileName(), createdArtifact.type));
200                 }
201             }
202             return Either.left(true);
203         } else {
204             List<ArtifactTemplateInfo> relatedArtifacts = parentArtifact.getRelatedArtifactsInfo();
205             if (relatedArtifacts == null || relatedArtifacts.isEmpty()) {
206                 return Either.left(true);
207             }
208             for (ArtifactTemplateInfo relatedArtifact : relatedArtifacts) {
209                 if (relatedArtifact.getType().equalsIgnoreCase(resourceInfo.getType()) && relatedArtifact.getFileName()
210                     .equalsIgnoreCase(resourceInfo.getFileName())) {
211                     return Either.right(componentsUtils
212                         .getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_MASTER_IN_CSAR, resourceInfo.getFileName(),
213                             parentArtifact.getFileName()));
214                 }
215             }
216             return Either.left(true);
217         }
218     }
219
220     private static Either<Boolean, ResponseFormat> validateParentType(ComponentsUtils componentsUtils, ArtifactTemplateInfo resourceInfo,
221                                                                       ArtifactTemplateInfo parentArtifact) {
222         if (parentArtifact == null) {
223             return Either.left(true);
224         }
225         if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ARTIFACT.getType())) {
226             return Either.left(true);
227         }
228         if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
229             return Either.right(componentsUtils
230                 .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_IN_MASTER, resourceInfo.getFileName(), resourceInfo.getType(),
231                     parentArtifact.getFileName(), parentArtifact.getType()));
232         }
233         if ((resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType()) || resourceInfo.getType()
234             .equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())) && !parentArtifact.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
235             return Either.right(componentsUtils
236                 .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_IN_MASTER, resourceInfo.getFileName(), resourceInfo.getType(),
237                     parentArtifact.getFileName(), parentArtifact.getType()));
238         }
239         if (parentArtifact.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_NESTED.getType())) {
240             if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || resourceInfo.getType()
241                 .equalsIgnoreCase(ArtifactTypeEnum.HEAT_NESTED.getType())) {
242                 return Either.left(true);
243             }
244             return Either.right(componentsUtils
245                 .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_IN_MASTER, resourceInfo.getFileName(), resourceInfo.getType(),
246                     parentArtifact.getFileName(), parentArtifact.getType()));
247         }
248         if (parentArtifact.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType()) && resourceInfo.getType()
249             .equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
250             return Either.right(componentsUtils
251                 .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_IN_MASTER, resourceInfo.getFileName(), resourceInfo.getType(),
252                     parentArtifact.getFileName(), parentArtifact.getType()));
253         }
254         if (parentArtifact.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
255             return Either.left(true);
256         }
257         return Either.left(true);
258     }
259
260     private static Either<Boolean, ResponseFormat> validateEnv(ComponentsUtils componentsUtils,
261                                                                List<ArtifactTemplateInfo> createdArtifactTemplateInfoList,
262                                                                ArtifactTemplateInfo resourceInfo) {
263         if (createdArtifactTemplateInfoList == null || createdArtifactTemplateInfoList.isEmpty()) {
264             return Either.left(true);
265         }
266         if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_NESTED.getType()) || resourceInfo.getType()
267             .equalsIgnoreCase(ArtifactTypeEnum.HEAT_ARTIFACT.getType())) {
268             return Either.left(true);
269         }
270         for (ArtifactTemplateInfo createdArtifactTemplateInfo : createdArtifactTemplateInfoList) {
271             // check if artifact with this name already parsed. If parsed check
272
273             // env name. it must be the same.
274             if (resourceInfo.getFileName().equalsIgnoreCase(createdArtifactTemplateInfo.getFileName())) {
275                 if ((resourceInfo.getEnv() == null || resourceInfo.getEnv().isEmpty()) && (createdArtifactTemplateInfo.getEnv() != null
276                     && !createdArtifactTemplateInfo.getEnv().isEmpty())) {
277                     log.debug("Artifact  file with name {} type{}  already parsed but with env {}", resourceInfo.getFileName(),
278                         resourceInfo.getType(), createdArtifactTemplateInfo.getEnv());
279                     return Either.right(componentsUtils
280                         .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_ENV, resourceInfo.getFileName(), resourceInfo.getType(),
281                             resourceInfo.getEnv(), createdArtifactTemplateInfo.getEnv()));
282                 }
283                 if (resourceInfo.getEnv() != null && !resourceInfo.getEnv().isEmpty() && createdArtifactTemplateInfo.getEnv() != null
284                     && !createdArtifactTemplateInfo.getEnv().isEmpty() && !createdArtifactTemplateInfo.getEnv()
285                     .equalsIgnoreCase(resourceInfo.getEnv())) {
286                     log.debug("Artifact  file with name {} type{} env {} already parsed but with env {}", resourceInfo.getFileName(),
287                         resourceInfo.getType(), resourceInfo.getEnv(), createdArtifactTemplateInfo.getEnv());
288                     return Either.right(componentsUtils
289                         .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_ENV, resourceInfo.getFileName(), resourceInfo.getType(),
290                             resourceInfo.getEnv(), createdArtifactTemplateInfo.getEnv()));
291                 }
292                 if ((resourceInfo.getEnv() != null && !resourceInfo.getEnv().isEmpty()) && (createdArtifactTemplateInfo.getEnv() == null
293                     || createdArtifactTemplateInfo.getEnv().isEmpty())) {
294                     log.debug("Artifact  file with name {} type{} env {} already parsed but with env {}", resourceInfo.getFileName(),
295                         resourceInfo.getType(), resourceInfo.getEnv(), createdArtifactTemplateInfo.getEnv());
296                     return Either.right(componentsUtils
297                         .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_ENV, resourceInfo.getFileName(), resourceInfo.getType(),
298                             resourceInfo.getEnv(), createdArtifactTemplateInfo.getEnv()));
299                 }
300             }
301             List<ArtifactTemplateInfo> relatedArtifacts = createdArtifactTemplateInfo.getRelatedArtifactsInfo();
302             Either<Boolean, ResponseFormat> status = validateEnv(componentsUtils, relatedArtifacts, resourceInfo);
303             if (status.isRight()) {
304                 return status;
305             }
306         }
307         return Either.left(true);
308     }
309
310     private static Either<List<ArtifactType>, ActionStatus> getDeploymentArtifactTypes(final NodeTypeEnum parentType) {
311         final List<ArtifactConfiguration> deploymentArtifacts;
312         final List<ArtifactConfiguration> artifactConfigurationList = ConfigurationManager.getConfigurationManager().getConfiguration()
313             .getArtifacts();
314         if (parentType == NodeTypeEnum.Service) {
315             deploymentArtifacts = artifactConfigurationList.stream().filter(
316                 artifactConfiguration -> artifactConfiguration.hasSupport(ArtifactGroupTypeEnum.DEPLOYMENT) && artifactConfiguration
317                     .hasSupport(ComponentType.SERVICE)).collect(Collectors.toList());
318         } else {
319             deploymentArtifacts = artifactConfigurationList.stream().filter(
320                 artifactConfiguration -> artifactConfiguration.hasSupport(ArtifactGroupTypeEnum.DEPLOYMENT) && artifactConfiguration
321                     .hasSupport(ComponentType.RESOURCE)).collect(Collectors.toList());
322         }
323         final List<ArtifactType> artifactTypes = new ArrayList<>();
324         if (CollectionUtils.isNotEmpty(deploymentArtifacts)) {
325             deploymentArtifacts.forEach(artifactConfiguration -> {
326                 final ArtifactType artifactType = new ArtifactType();
327                 artifactType.setName(artifactConfiguration.getType());
328                 artifactTypes.add(artifactType);
329             });
330             return Either.left(artifactTypes);
331         }
332         return Either.right(ActionStatus.GENERAL_ERROR);
333     }
334
335     public static int compareByGroupName(ArtifactTemplateInfo art1, ArtifactTemplateInfo art2) {
336         return art1.isBase() ? (art2.isBase() ? 0 : -1) : (art2.isBase() ? 1 : 0);
337     }
338 }