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=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import fj.data.Either;
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.be.auditing.api.IAuditingManager;
27 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
28 import org.openecomp.sdc.be.components.impl.ImportUtils.Constants;
29 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
30 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaTagNamesEnum;
31 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
32 import org.openecomp.sdc.be.config.BeEcompErrorManager;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
36 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
37 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.model.*;
42 import org.openecomp.sdc.be.model.category.CategoryDefinition;
43 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
44 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
48 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
49 import org.openecomp.sdc.common.config.EcompErrorName;
50 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
51 import org.openecomp.sdc.common.util.ValidationUtils;
52 import org.openecomp.sdc.exception.ResponseFormat;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Component;
57 import org.springframework.web.context.WebApplicationContext;
58 import org.yaml.snakeyaml.Yaml;
60 import javax.servlet.ServletContext;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.EnumMap;
64 import java.util.HashMap;
65 import java.util.HashSet;
66 import java.util.Iterator;
67 import java.util.List;
69 import java.util.Map.Entry;
71 import java.util.function.Function;
72 import java.util.regex.Pattern;
73 import java.util.stream.Collectors;
75 @Component("resourceImportManager")
76 public class ResourceImportManager {
78 private ServletContext servletContext;
81 private IAuditingManager auditingManager;
84 private ResourceBusinessLogic resourceBusinessLogic;
87 private IGraphLockOperation graphLockOperation;
90 protected ComponentsUtils componentsUtils;
92 public final static Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern
93 .compile("[\\w\\-\\_\\d\\:]+");
95 protected CapabilityTypeOperation capabilityTypeOperation;
97 protected ToscaOperationFacade toscaOperationFacade;
99 private ResponseFormatManager responseFormatManager;
101 private static Logger log = LoggerFactory.getLogger(ResourceImportManager.class.getName());
103 public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) {
104 this.toscaOperationFacade = toscaOperationFacade;
107 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importNormativeResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) {
109 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
110 lifecycleChangeInfo.setUserRemarks("certification on import");
111 Function<Resource, Either<Boolean, ResponseFormat>> validator = (resource) -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
113 return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null);
116 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) {
118 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
119 lifecycleChangeInfo.setUserRemarks("certification on import");
120 Function<Resource, Either<Boolean, ResponseFormat>> validator = (resource) -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
122 return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null);
125 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, Function<Resource, Either<Boolean, ResponseFormat>> validationFunction,
126 LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction, boolean createNewVersion, boolean needLock, Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo) {
127 Resource resource = new Resource();
128 ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
129 Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
131 String latestCertifiedResourceId = null;
133 boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty();
134 setConstantMetaData(resource, shouldBeCertified);
135 setMetaDataFromJson(resourceMetaData, resource);
137 Either<Boolean, ResponseFormat> validateResourceFromYaml = populateResourceFromYaml(resourceYml, resource, isInTransaction);
138 if (validateResourceFromYaml.isRight()) {
139 ResponseFormat validationErrorResponse = validateResourceFromYaml.right().value();
140 auditErrorImport(resourceMetaData, creator, validationErrorResponse, true);
141 return Either.right(validationErrorResponse);
145 Either<Boolean, ResponseFormat> isValidResource = validationFunction.apply(resource);
146 if (isValidResource.isLeft()) {
147 // The flag createNewVersion if false doesn't create new version
148 if (!createNewVersion) {
149 Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resource.getName());
150 if (latestByName.isLeft()) {
151 return Either.right(componentsUtils.getResponseFormatByResource(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resource));
155 response = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo);
156 Either<Resource, ResponseFormat> changeStateResponse;
157 if (response.isLeft()) {
158 resource = response.left().value().left;
160 if(nodeTypeArtifactsToHandle !=null && !nodeTypeArtifactsToHandle.isEmpty()){
161 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes =
162 resourceBusinessLogic.handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false);
163 if(handleNodeTypeArtifactsRes.isRight()){
164 return Either.right(handleNodeTypeArtifactsRes.right().value());
167 latestCertifiedResourceId = getLatestCertifiedResourceId(resource);
168 changeStateResponse = resourceBusinessLogic.propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed);
169 if (changeStateResponse.isRight()) {
170 response = Either.right(changeStateResponse.right().value());
172 responsePair = new ImmutablePair<>(changeStateResponse.left().value(), response.left().value().right);
173 response = Either.left(responsePair);
177 ResponseFormat validationErrorResponse = isValidResource.right().value();
178 auditErrorImport(resourceMetaData, creator, validationErrorResponse, true);
179 response = Either.right(validationErrorResponse);
182 } catch (RuntimeException e) {
183 ResponseFormat exceptionResponse = handleImportResourceExecption(resourceMetaData, creator, true, e);
184 response = Either.right(exceptionResponse);
186 if (latestCertifiedResourceId != null && needLock) {
187 log.debug("unlock resource {}", latestCertifiedResourceId);
188 graphLockOperation.unlockComponent(latestCertifiedResourceId, NodeTypeEnum.Resource);
195 private String getLatestCertifiedResourceId(Resource resource) {
196 Map<String, String> allVersions = resource.getAllVersions();
197 Double latestCertifiedVersion = 0.0;
198 if (allVersions != null) {
199 for (String version : allVersions.keySet()) {
200 Double dVersion = Double.valueOf(version);
201 if ((dVersion > latestCertifiedVersion) && (version.endsWith(".0"))) {
202 latestCertifiedVersion = dVersion;
205 return allVersions.get(String.valueOf(latestCertifiedVersion));
211 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean isReusable, boolean isInTransaction) {
213 Resource resource = new Resource();
214 ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<Resource, ActionStatus>(resource, ActionStatus.CREATED);
215 Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
218 setMetaDataFromJson(resourceMetaData, resource);
220 Either<Boolean, ResponseFormat> validateResourceFromYaml = populateResourceFromYaml(resourceYml, resource, isInTransaction);
221 if (validateResourceFromYaml.isRight()) {
222 ResponseFormat validationErrorResponse = validateResourceFromYaml.right().value();
223 auditErrorImport(resourceMetaData, creator, validationErrorResponse, false);
224 return Either.right(validationErrorResponse);
228 // currently import VF isn't supported. In future will be supported
229 // import VF only with CSER file!!
230 if (ResourceTypeEnum.VF.equals(resource.getResourceType())) {
231 log.debug("Now import VF isn't supported. It will be supported in future with CSER file only");
232 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
235 Either<Boolean, ResponseFormat> validateDerivedFromNotEmpty = resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
236 if (validateDerivedFromNotEmpty.isRight()) {
237 return Either.right(validateDerivedFromNotEmpty.right().value());
240 Either<Boolean, ResponseFormat> validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource);
242 if (validatePropertiesTypes.isLeft()) {
243 response = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null);
245 ResponseFormat validationErrorResponse = validatePropertiesTypes.right().value();
246 auditErrorImport(resourceMetaData, creator, validationErrorResponse, false);
247 response = Either.right(validationErrorResponse);
250 } catch (RuntimeException e) {
251 ResponseFormat exceptionResponse = handleImportResourceExecption(resourceMetaData, creator, false, e);
252 response = Either.right(exceptionResponse);
259 Either<Boolean, ResponseFormat> populateResourceFromYaml(String resourceYml, Resource resource, boolean inTransaction) {
260 @SuppressWarnings("unchecked")
261 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
262 Map<String, Object> toscaJsonAll = (Map<String, Object>) new Yaml().load(resourceYml);
263 Map<String, Object> toscaJson = toscaJsonAll;
265 // Checks if exist and builds the node_types map
266 if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType()!=ResourceTypeEnum.CVFC) {
267 toscaJson = new HashMap<String, Object>();
268 toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()));
271 Either<Resource, ResponseFormat> setDerivedFrom = setDerivedFrom(toscaJson, resource, inTransaction);
272 if (setDerivedFrom.isRight()) {
273 return Either.right(setDerivedFrom.right().value());
275 Resource parentResource = setDerivedFrom.left().value();
276 if(StringUtils.isEmpty(resource.getToscaResourceName()))
277 setToscaResourceName(toscaJson, resource);
278 setAttributes(toscaJson, resource);
279 eitherResult = setCapabilities(toscaJson, resource, parentResource);
280 if (eitherResult.isRight())
282 eitherResult = setProperties(toscaJson, resource);
283 if (eitherResult.isRight())
285 eitherResult = setRequirements(toscaJson, resource, parentResource);
286 if (eitherResult.isRight())
288 setInterfaceLifecycle(toscaJson, resource);
293 private void setToscaResourceName(Map<String, Object> toscaJson, Resource resource) {
294 Either<Map<String, Object>, ResultStatusEnum> toscaElement = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES);
295 if (toscaElement.isLeft() || toscaElement.left().value().size() == 1) {
296 String toscaResourceName = toscaElement.left().value().keySet().iterator().next();
297 resource.setToscaResourceName(toscaResourceName);
301 private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource) {
302 Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES);
303 if (toscaInterfaces.isLeft()) {
304 Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
305 Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<String, InterfaceDefinition>();
306 Iterator<Entry<String, Object>> interfacesNameValue = jsonInterfaces.entrySet().iterator();
307 while (interfacesNameValue.hasNext()) {
308 Entry<String, Object> interfaceNameValue = interfacesNameValue.next();
309 Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue.getValue());
310 if (eitherInterface.isRight()) {
311 log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
313 moduleInterfaces.put(interfaceNameValue.getKey(), eitherInterface.left().value());
317 if (moduleInterfaces.size() > 0) {
318 resource.setInterfaces(moduleInterfaces);
323 private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(Object interfaceJson) {
324 InterfaceDefinition interf = new InterfaceDefinition();
325 Either<InterfaceDefinition, ResultStatusEnum> result = Either.left(interf);
328 if (interfaceJson instanceof String) {
329 String requirementJsonString = (String) interfaceJson;
330 interf.setType(requirementJsonString);
331 } else if (interfaceJson instanceof Map) {
332 Map<String, Object> requirementJsonMap = (Map<String, Object>) interfaceJson;
333 if (requirementJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) {
334 String type = (String) requirementJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName());
335 interf.setType(type);
336 interf.setUniqueId(type.toLowerCase());
339 result = Either.right(ResultStatusEnum.GENERAL_ERROR);
342 } catch (Exception e) {
343 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "Import Resource- create interface");
344 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface");
345 log.debug("error when creating interface, message:{}", e.getMessage(), e);
346 result = Either.right(ResultStatusEnum.GENERAL_ERROR);
352 private Either<Boolean, ResponseFormat> setRequirements(Map<String, Object> toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null
353 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
354 Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils.findFirstToscaListElement(toscaJson, ToscaTagNamesEnum.REQUIREMENTS);
355 if (toscaRequirements.isLeft()) {
356 List<Object> jsonRequirements = toscaRequirements.left().value();
357 Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<String, List<RequirementDefinition>>();
358 // Checking for name duplication
359 Set<String> reqNames = new HashSet<>();
360 // Getting flattened list of capabilities of parent node - cap name
362 Either<Map<String, String>, ResponseFormat> reqName2Type = getReqName2Type(parentResource);
363 if (reqName2Type.isRight()) {
364 ResponseFormat responseFormat = reqName2Type.right().value();
365 log.debug("Error during setting requirements of imported resource: {}", responseFormat);
366 return Either.right(responseFormat);
368 Map<String, String> reqName2TypeMap = reqName2Type.left().value();
369 for (Object jsonRequirementObj : jsonRequirements) {
371 Map<String, Object> requirementJsonWrapper = (Map<String, Object>) jsonRequirementObj;
372 String requirementName = requirementJsonWrapper.keySet().iterator().next();
373 String reqNameLowerCase = requirementName.toLowerCase();
374 if (reqNames.contains(reqNameLowerCase)) {
375 log.debug("More than one requirement with same name {} (case-insensitive) in imported TOSCA file is invalid", reqNameLowerCase);
376 return Either.right(componentsUtils.getResponseFormat(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase));
378 reqNames.add(reqNameLowerCase);
379 Either<RequirementDefinition, ResponseFormat> eitherRequirement = createRequirementFromImportFile(requirementJsonWrapper.get(requirementName));
380 if (eitherRequirement.isRight()) {
381 log.info("error when creating Requirement:{}, for resource:{}", requirementName, resource.getName());
382 return Either.right(eitherRequirement.right().value());
384 RequirementDefinition requirementDef = eitherRequirement.left().value();
385 requirementDef.setName(requirementName);
386 if (moduleRequirements.containsKey(requirementDef.getCapability())) {
387 moduleRequirements.get(requirementDef.getCapability()).add(requirementDef);
389 List<RequirementDefinition> list = new ArrayList<RequirementDefinition>();
390 list.add(requirementDef);
391 moduleRequirements.put(requirementDef.getCapability(), list);
394 // Validating against req/cap of "derived from" node
395 Either<Boolean, ResponseFormat> validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef.getCapability(), requirementDef.getName());
396 if (validateVsParentCap.isRight()) {
397 return Either.right(validateVsParentCap.right().value());
399 if (!validateVsParentCap.left().value()) {
400 log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResource.getName());
401 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement", requirementDef.getName().toLowerCase(), parentResource.getName());
402 return Either.right(responseFormat);
405 if (moduleRequirements.size() > 0) {
406 resource.setRequirements(moduleRequirements);
414 private Either<RequirementDefinition, ResponseFormat> createRequirementFromImportFile(Object requirementJson) {
415 RequirementDefinition requirement = new RequirementDefinition();
416 Either<RequirementDefinition, ResponseFormat> result = Either.left(requirement);
419 if (requirementJson instanceof String) {
420 String requirementJsonString = (String) requirementJson;
421 requirement.setCapability(requirementJsonString);
422 } else if (requirementJson instanceof Map) {
423 Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
424 if (requirementJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) {
425 requirement.setCapability((String) requirementJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName()));
428 if (requirementJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) {
429 requirement.setNode((String) requirementJsonMap.get(ToscaTagNamesEnum.NODE.getElementName()));
432 if (requirementJsonMap.containsKey(ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
433 requirement.setRelationship((String) requirementJsonMap.get(ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
435 if (requirementJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
436 List<Object> occurrencesList = (List) requirementJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
437 Either<Boolean, ResponseFormat> validateAndSetOccurrencesStatus = validateOccurrences(occurrencesList);
438 if (validateAndSetOccurrencesStatus.isRight()) {
439 result = Either.right(validateAndSetOccurrencesStatus.right().value());
442 if (validateAndSetOccurrencesStatus.left().value() == true) {
443 requirement.setMinOccurrences(occurrencesList.get(0).toString());
444 requirement.setMaxOccurrences(occurrencesList.get(1).toString());
449 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
452 } catch (Exception e) {
453 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "Import Resource - create Requirement");
454 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create Requirement");
455 log.debug("error when creating requirement, message:{}", e.getMessage(), e);
456 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
462 private Either<Boolean, ResponseFormat> setProperties(Map<String, Object> toscaJson, Resource resource) {
463 Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
464 ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
465 Either<Boolean, ResponseFormat> result = Either.left(true);
466 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
467 if (properties.isLeft()) {
468 List<PropertyDefinition> propertiesList = new ArrayList<>();
469 Map<String, PropertyDefinition> value = properties.left().value();
471 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
472 String name = entry.getKey();
473 if(!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()){
474 log.debug("The property with invalid name {} occured upon import resource {}. ", name, resource.getName());
475 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)));
477 PropertyDefinition propertyDefinition = entry.getValue();
478 propertyDefinition.setName(name);
479 propertiesList.add(propertyDefinition);
482 resource.setProperties(propertiesList);
483 } else if(properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND){
484 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)));
489 private ResultStatusEnum setAttributes(Map<String, Object> toscaJson, Resource resource) {
490 ResultStatusEnum result = ResultStatusEnum.OK;
491 Either<Map<String, PropertyDefinition>, ResultStatusEnum> attributes = ImportUtils.getAttributes(toscaJson);
492 if (attributes.isLeft()) {
493 List<PropertyDefinition> attributeList = new ArrayList<>();
494 Map<String, PropertyDefinition> value = attributes.left().value();
496 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
497 String name = entry.getKey();
498 PropertyDefinition attributeDef = entry.getValue();
499 attributeDef.setName(name);
500 attributeList.add(attributeDef);
503 resource.setAttributes(attributeList);
505 result = attributes.right().value();
510 private Either<Resource, ResponseFormat> setDerivedFrom(Map<String, Object> toscaJson, Resource resource, boolean inTransaction) {
511 Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils.findFirstToscaStringElement(toscaJson, ToscaTagNamesEnum.DERIVED_FROM);
512 Resource derivedFromResource = null;
513 if (toscaDerivedFromElement.isLeft()) {
514 String derivedFrom = toscaDerivedFromElement.left().value();
515 log.debug("Derived from TOSCA name is {}", derivedFrom);
516 resource.setDerivedFrom(Arrays.asList(new String[] { derivedFrom }));
517 Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom);
519 if (latestByToscaResourceName.isRight()) {
520 StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
521 if (operationStatus.equals(StorageOperationStatus.NOT_FOUND)) {
522 operationStatus = StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND;
524 log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus);
525 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
526 BeEcompErrorManager.getInstance().logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom);
527 return Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, derivedFrom));
529 derivedFromResource = latestByToscaResourceName.left().value();
531 return Either.left(derivedFromResource);
534 private Either<Boolean, ResponseFormat> setCapabilities(Map<String, Object> toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null
535 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
536 Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.CAPABILITIES);
537 if (toscaCapabilities.isLeft()) {
538 Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
539 Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<String, List<CapabilityDefinition>>();
540 Iterator<Entry<String, Object>> capabilitiesNameValue = jsonCapabilities.entrySet().iterator();
541 Set<String> capNames = new HashSet<>();
542 // Getting flattened list of capabilities of parent node - cap name
544 Either<Map<String, String>, ResponseFormat> capName2Type = getCapName2Type(parentResource);
545 if (capName2Type.isRight()) {
546 ResponseFormat responseFormat = capName2Type.right().value();
547 log.debug("Error during setting capabilities of imported resource: {}", responseFormat);
548 return Either.right(responseFormat);
550 Map<String, String> capName2TypeMap = capName2Type.left().value();
551 while (capabilitiesNameValue.hasNext()) {
552 Entry<String, Object> capabilityNameValue = capabilitiesNameValue.next();
554 // Validating that no req/cap duplicates exist in imported YAML
555 String capNameLowerCase = capabilityNameValue.getKey().toLowerCase();
556 if (capNames.contains(capNameLowerCase)) {
557 log.debug("More than one capability with same name {} (case-insensitive) in imported TOSCA file is invalid", capNameLowerCase);
558 return Either.right(componentsUtils.getResponseFormat(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase));
560 capNames.add(capNameLowerCase);
562 Either<CapabilityDefinition, ResponseFormat> eitherCapability = createCapabilityFromImportFile(capabilityNameValue.getValue());
563 if (eitherCapability.isRight()) {
564 log.debug("error when creating capability:{}, for resource:{}", capabilityNameValue.getKey(), resource.getName());
565 return Either.right(eitherCapability.right().value());
568 CapabilityDefinition capabilityDef = eitherCapability.left().value();
569 capabilityDef.setName(capabilityNameValue.getKey());
570 if (moduleCapabilities.containsKey(capabilityDef.getType())) {
571 moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef);
573 List<CapabilityDefinition> list = new ArrayList<CapabilityDefinition>();
574 list.add(capabilityDef);
575 moduleCapabilities.put(capabilityDef.getType(), list);
578 // Validating against req/cap of "derived from" node
579 Either<Boolean, ResponseFormat> validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef.getType(), capabilityDef.getName());
580 if (validateVsParentCap.isRight()) {
581 return Either.right(validateVsParentCap.right().value());
583 if (!validateVsParentCap.left().value()) {
584 // Here parentResource is for sure not null, so it's
586 log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResource.getName());
587 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability", capabilityDef.getName().toLowerCase(), parentResource.getName());
588 return Either.right(responseFormat);
591 if (moduleCapabilities.size() > 0) {
592 resource.setCapabilities(moduleCapabilities);
600 private Either<Map<String, String>, ResponseFormat> getCapName2Type(Resource parentResource) {
601 Map<String, String> capName2type = new HashMap<>();
602 if (parentResource != null) {
603 Map<String, List<CapabilityDefinition>> capabilities = parentResource.getCapabilities();
604 if (capabilities != null) {
605 for (List<CapabilityDefinition> capDefinitions : capabilities.values()) {
606 for (CapabilityDefinition capDefinition : capDefinitions) {
607 String nameLowerCase = capDefinition.getName().toLowerCase();
608 if (capName2type.get(nameLowerCase) != null) {
609 String parentResourceName = parentResource.getName();
610 log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName, nameLowerCase);
611 BeEcompErrorManager.getInstance().logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name " + nameLowerCase, ErrorSeverity.ERROR);
612 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
614 capName2type.put(nameLowerCase, capDefinition.getType());
619 return Either.left(capName2type);
622 private Either<Map<String, String>, ResponseFormat> getReqName2Type(Resource parentResource) {
623 Map<String, String> reqName2type = new HashMap<>();
624 if (parentResource != null) {
625 Map<String, List<RequirementDefinition>> requirements = parentResource.getRequirements();
626 if (requirements != null) {
627 for (List<RequirementDefinition> reqDefinitions : requirements.values()) {
628 for (RequirementDefinition reqDefinition : reqDefinitions) {
629 String nameLowerCase = reqDefinition.getName().toLowerCase();
630 if (reqName2type.get(nameLowerCase) != null) {
631 String parentResourceName = parentResource.getName();
632 log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName, nameLowerCase);
633 BeEcompErrorManager.getInstance().logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name " + nameLowerCase, ErrorSeverity.ERROR);
634 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
636 reqName2type.put(nameLowerCase, reqDefinition.getCapability());
641 return Either.left(reqName2type);
644 private Either<Boolean, ResponseFormat> validateCapNameVsDerived(Map<String, String> parentCapName2Type, String childCapabilityType, String reqCapName) {
645 String capNameLowerCase = reqCapName.toLowerCase();
646 log.trace("Validating capability {} vs parent resource", capNameLowerCase);
647 String parentCapType = parentCapName2Type.get(capNameLowerCase);
648 if (parentCapType != null) {
649 if (childCapabilityType.equals(parentCapType)) {
650 log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase, childCapabilityType);
651 return Either.left(true);
653 Either<Boolean, StorageOperationStatus> capabilityTypeDerivedFrom = capabilityTypeOperation.isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType);
654 if (capabilityTypeDerivedFrom.isRight()) {
655 log.debug("Couldn't check whether imported resource capability derives from its parent's capability");
656 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom.right().value()));
657 return Either.right(responseFormat);
659 return Either.left(capabilityTypeDerivedFrom.left().value());
661 return Either.left(true);
664 private Either<CapabilityDefinition, ResponseFormat> createCapabilityFromImportFile(Object capabilityJson) {
666 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
667 Either<CapabilityDefinition, ResponseFormat> result = Either.left(capabilityDefinition);
670 if (capabilityJson instanceof String) {
671 String capabilityJsonString = (String) capabilityJson;
672 capabilityDefinition.setType(capabilityJsonString);
673 } else if (capabilityJson instanceof Map) {
674 Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
676 if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) {
677 capabilityDefinition.setType((String) capabilityJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()));
680 if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
681 capabilityDefinition.setValidSourceTypes((List<String>) capabilityJsonMap.get(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
684 if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
685 capabilityDefinition.setDescription((String) capabilityJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName()));
687 if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
688 List<Object> occurrencesList = (List) capabilityJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
689 Either<Boolean, ResponseFormat> validateAndSetOccurrencesStatus = validateOccurrences(occurrencesList);
690 if (validateAndSetOccurrencesStatus.isRight()) {
691 result = Either.right(validateAndSetOccurrencesStatus.right().value());
694 if (validateAndSetOccurrencesStatus.left().value() == true) {
695 capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
696 capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
699 if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) {
701 Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
702 if (propertiesRes.isRight()) {
703 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
706 propertiesRes.left().value().entrySet().stream().forEach(e -> e.getValue().setName(e.getKey().toLowerCase()));
707 List<ComponentInstanceProperty> capabilityProperties = propertiesRes.left().value().values().stream().map(p -> new ComponentInstanceProperty(p, p.getDefaultValue(), null)).collect(Collectors.toList());
708 capabilityDefinition.setProperties(capabilityProperties);
711 } else if (!(capabilityJson instanceof List)) {
713 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
716 } catch (Exception e) {
717 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "Import Resource - create capability");
718 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
719 log.debug("error when creating capability, message:{}", e.getMessage(), e);
720 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
726 private ResponseFormat handleImportResourceExecption(UploadResourceInfo resourceMetaData, User user, boolean isNormative, RuntimeException e) {
727 String payloadName = (resourceMetaData != null) ? resourceMetaData.getPayloadName() : "";
728 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "Import Resource " + payloadName);
729 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource " + payloadName);
731 log.debug("Error when importing resource from payload:{} Exception text: {}", payloadName, e.getMessage(), e);
732 ResponseFormat errorResponseWrapper = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR);
733 auditErrorImport(resourceMetaData, user, errorResponseWrapper, isNormative);
734 return errorResponseWrapper;
737 private void auditErrorImport(UploadResourceInfo resourceMetaData, User user, ResponseFormat errorResponseWrapper, boolean isNormative) {
738 EnumMap<AuditingFieldsKeysEnum, Object> auditingFields = new EnumMap<>(AuditingFieldsKeysEnum.class);
739 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, AuditingActionEnum.IMPORT_RESOURCE.getName());
740 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_NAME, resourceMetaData.getName());
741 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, ComponentTypeEnum.RESOURCE.getValue());
742 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_VERSION, "");
743 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, user.getUserId());
744 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_STATE, "");
745 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_INVARIANT_UUID, "");
746 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, errorResponseWrapper.getStatus());
748 if (errorResponseWrapper.getMessageId() != null) {
749 message = errorResponseWrapper.getMessageId() + ": ";
751 message += errorResponseWrapper.getFormattedMessage();
752 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, message);
753 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_NAME, user.getFirstName() + " " + user.getLastName());
755 String version, lifeCycleState;
757 version = Constants.FIRST_CERTIFIED_VERSION_VERSION;
758 lifeCycleState = LifecycleStateEnum.CERTIFIED.name();
761 lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
764 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, version);
765 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_STATE, lifeCycleState);
766 auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TOSCA_NODE_TYPE, "");
768 getAuditingManager().auditEvent(auditingFields);
771 private void setMetaDataFromJson(UploadResourceInfo resourceMetaData, Resource resource) {
772 resource.setTags(resourceMetaData.getTags());
773 List<CategoryDefinition> categories = resourceMetaData.getCategories();
774 resource.setCategories(categories);
775 resource.setDescription(resourceMetaData.getDescription());
776 resource.setIcon(resourceMetaData.getResourceIconPath());
777 resource.setName(resourceMetaData.getName());
778 if (categories != null && !categories.isEmpty()) {
779 CategoryDefinition categoryDef = categories.get(0);
780 resource.setAbstract(false);
781 if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName().equals(Constants.ABSTRACT_CATEGORY_NAME)) {
782 SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0);
783 if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) {
784 resource.setAbstract(true);
788 resource.setContactId(resourceMetaData.getContactId());
789 resource.setCreatorUserId(resourceMetaData.getContactId());
791 if (resourceMetaData.getVendorName() != null) {
792 resource.setVendorName(resourceMetaData.getVendorName());
795 if (resourceMetaData.getVendorRelease() != null) {
796 resource.setVendorRelease(resourceMetaData.getVendorRelease());
799 resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType()));
803 private void setConstantMetaData(Resource resource, boolean shouldBeCertified) {
805 LifecycleStateEnum state;
806 if(shouldBeCertified){
807 version = ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION;
808 state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE;
810 version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION;
811 state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT;
813 resource.setVersion(version);
814 resource.setLifecycleState(state);
815 resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
816 resource.setVendorName(ImportUtils.Constants.VENDOR_NAME);
817 resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE);
821 private Either<Boolean, ResponseFormat> validateOccurrences(List<Object> occurrensesList) {
823 if (!ValidationUtils.validateListNotEmpty(occurrensesList)) {
824 log.debug("Occurrenses list empty");
825 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
826 return Either.right(responseFormat);
829 if (occurrensesList.size() < 2) {
830 log.debug("Occurrenses list size not 2");
831 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
832 return Either.right(responseFormat);
834 Object minObj = occurrensesList.get(0);
835 Object maxObj = occurrensesList.get(1);
836 Integer minOccurrences = null;
837 Integer maxOccurrences = null;
838 if (minObj instanceof Integer)
839 minOccurrences = (Integer) minObj;
841 log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj);
842 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
843 return Either.right(responseFormat);
845 if (minOccurrences < 0) {
846 log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences);
847 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
848 return Either.right(responseFormat);
851 if (maxObj instanceof String) {
852 if (maxObj.equals("UNBOUNDED")) {
853 return Either.left(true);
855 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
856 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
857 return Either.right(responseFormat);
860 if (maxObj instanceof Integer)
861 maxOccurrences = (Integer) maxObj;
863 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
864 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
865 return Either.right(responseFormat);
868 if (maxOccurrences <= 0 || maxOccurrences < minOccurrences) {
869 log.debug("Invalid occurrenses format. min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
870 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
871 return Either.right(responseFormat);
875 return Either.left(true);
879 public void init(ServletContext servletContext) {
880 if (this.servletContext == null) {
881 synchronized (this) {
882 if (this.servletContext == null) {
883 this.servletContext = servletContext;
884 responseFormatManager = ResponseFormatManager.getInstance();
885 resourceBusinessLogic = getResourceBL(servletContext);
891 public boolean isResourceExist(String resourceName) {
892 return resourceBusinessLogic.isResourceExist(resourceName);
895 private ResourceBusinessLogic getResourceBL(ServletContext context) {
896 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
897 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
898 ResourceBusinessLogic resourceBl = webApplicationContext.getBean(ResourceBusinessLogic.class);
902 public ServletContext getServletContext() {
903 return servletContext;
906 public IAuditingManager getAuditingManager() {
907 return auditingManager;
910 public ResponseFormatManager getResponseFormatManager() {
911 return responseFormatManager;
914 public void setResponseFormatManager(ResponseFormatManager responseFormatManager) {
915 this.responseFormatManager = responseFormatManager;
918 public ResourceBusinessLogic getResourceBusinessLogic() {
919 return resourceBusinessLogic;
922 public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) {
923 this.resourceBusinessLogic = resourceBusinessLogic;
926 public Logger getLog() {
930 public static void setLog(Logger log) {
931 ResourceImportManager.log = log;
934 public IGraphLockOperation getGraphLockOperation() {
935 return graphLockOperation;
938 public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
939 this.graphLockOperation = graphLockOperation;
942 public void setServletContext(ServletContext servletContext) {
943 this.servletContext = servletContext;
946 public void setAuditingManager(IAuditingManager auditingManager) {
947 this.auditingManager = auditingManager;