2 * ============LICENSE_START=======================================================
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
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.info;
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;
30 import java.util.stream.Collectors;
32 import lombok.NoArgsConstructor;
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;
51 public class ArtifactTemplateInfo {
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";
67 private String fileName;
70 private String groupName;
71 private String description;
72 private List<ArtifactTemplateInfo> relatedArtifactsInfo;
74 public ArtifactTemplateInfo(String type, String fileName, String env, List<ArtifactTemplateInfo> relatedArtifactsInfo) {
76 this.fileName = fileName;
78 this.relatedArtifactsInfo = relatedArtifactsInfo;
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));
93 if (artifactTemplateMap.containsKey(FILE_NAME)) {
94 resourceInfo.setFileName((String) artifactTemplateMap.get(FILE_NAME));
96 if (artifactTemplateMap.containsKey(IS_BASE)) {
97 resourceInfo.setBase((Boolean) artifactTemplateMap.get(IS_BASE));
99 if (artifactTemplateMap.containsKey(ARTIFACT_TEMPLATE_ENV)) {
100 Object envObj = artifactTemplateMap.get(ARTIFACT_TEMPLATE_ENV);
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);
110 resourceInfo.setEnv(envStr);
112 if (artifactTemplateMap.containsKey(DESC)) {
113 resourceInfo.setDescription((String) artifactTemplateMap.get(DESC));
115 resourceInfo.setDescription((String) artifactTemplateMap.get(FILE_NAME));
117 boolean artifactTypeExist = false;
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();
129 correctType = resourceInfo.type;
131 } else if (type.equalsIgnoreCase(CSAR_HEAT)) {
132 correctType = ArtifactTypeEnum.HEAT.getType();
133 } else if (type.equalsIgnoreCase(CSAR_HELM)) {
134 correctType = ArtifactTypeEnum.HELM.getType();
136 correctType = ArtifactTypeEnum.OTHER.getType();
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));
144 for (ArtifactType artType : allArtifactTypes.left().value()) {
145 if (artType.getName().contains(correctType)) {
146 resourceInfo.type = artType.getName();
147 artifactTypeExist = true;
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));
156 Either<Boolean, ResponseFormat> eitherNeedToCreate = validateEnv(componentsUtils, createdArtifactTemplateInfoList, resourceInfo);
157 if (eitherNeedToCreate.isRight()) {
158 return Either.right(eitherNeedToCreate.right().value());
160 eitherNeedToCreate = validateParentType(componentsUtils, resourceInfo, parentArtifact);
161 if (eitherNeedToCreate.isRight()) {
162 return Either.right(eitherNeedToCreate.right().value());
164 eitherNeedToCreate = validateIsAlreadyExist(componentsUtils, resourceInfo, createdArtifactTemplateInfoList, parentArtifact);
165 if (eitherNeedToCreate.isRight()) {
166 return Either.right(eitherNeedToCreate.right().value());
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;
178 if (resourceInfo.relatedArtifactsInfo == null) {
179 resourceInfo.relatedArtifactsInfo = new ArrayList<>();
181 resourceInfo.relatedArtifactsInfo.add(relatedArtifact.left().value());
185 return Either.left(resourceInfo);
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);
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));
202 return Either.left(true);
204 List<ArtifactTemplateInfo> relatedArtifacts = parentArtifact.getRelatedArtifactsInfo();
205 if (relatedArtifacts == null || relatedArtifacts.isEmpty()) {
206 return Either.left(true);
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()));
216 return Either.left(true);
220 private static Either<Boolean, ResponseFormat> validateParentType(ComponentsUtils componentsUtils, ArtifactTemplateInfo resourceInfo,
221 ArtifactTemplateInfo parentArtifact) {
222 if (parentArtifact == null) {
223 return Either.left(true);
225 if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ARTIFACT.getType())) {
226 return Either.left(true);
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()));
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()));
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);
244 return Either.right(componentsUtils
245 .getResponseFormat(ActionStatus.ARTIFACT_NOT_VALID_IN_MASTER, resourceInfo.getFileName(), resourceInfo.getType(),
246 parentArtifact.getFileName(), parentArtifact.getType()));
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()));
254 if (parentArtifact.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
255 return Either.left(true);
257 return Either.left(true);
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);
266 if (resourceInfo.getType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_NESTED.getType()) || resourceInfo.getType()
267 .equalsIgnoreCase(ArtifactTypeEnum.HEAT_ARTIFACT.getType())) {
268 return Either.left(true);
270 for (ArtifactTemplateInfo createdArtifactTemplateInfo : createdArtifactTemplateInfoList) {
271 // check if artifact with this name already parsed. If parsed check
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()));
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()));
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()));
301 List<ArtifactTemplateInfo> relatedArtifacts = createdArtifactTemplateInfo.getRelatedArtifactsInfo();
302 Either<Boolean, ResponseFormat> status = validateEnv(componentsUtils, relatedArtifacts, resourceInfo);
303 if (status.isRight()) {
307 return Either.left(true);
310 private static Either<List<ArtifactType>, ActionStatus> getDeploymentArtifactTypes(final NodeTypeEnum parentType) {
311 final List<ArtifactConfiguration> deploymentArtifacts;
312 final List<ArtifactConfiguration> artifactConfigurationList = ConfigurationManager.getConfigurationManager().getConfiguration()
314 if (parentType == NodeTypeEnum.Service) {
315 deploymentArtifacts = artifactConfigurationList.stream().filter(
316 artifactConfiguration -> artifactConfiguration.hasSupport(ArtifactGroupTypeEnum.DEPLOYMENT) && artifactConfiguration
317 .hasSupport(ComponentType.SERVICE)).collect(Collectors.toList());
319 deploymentArtifacts = artifactConfigurationList.stream().filter(
320 artifactConfiguration -> artifactConfiguration.hasSupport(ArtifactGroupTypeEnum.DEPLOYMENT) && artifactConfiguration
321 .hasSupport(ComponentType.RESOURCE)).collect(Collectors.toList());
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);
330 return Either.left(artifactTypes);
332 return Either.right(ActionStatus.GENERAL_ERROR);
335 public static int compareByGroupName(ArtifactTemplateInfo art1, ArtifactTemplateInfo art2) {
336 return art1.isBase() ? (art2.isBase() ? 0 : -1) : (art2.isBase() ? 1 : 0);