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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.be.components.impl;
25 import fj.data.Either;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
29 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
30 import org.openecomp.sdc.be.auditing.impl.resourceadmin.AuditImportResourceAdminEventFactory;
31 import org.openecomp.sdc.be.components.csar.CsarInfo;
32 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
33 import org.openecomp.sdc.be.components.impl.ImportUtils.Constants;
34 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
42 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
46 import org.openecomp.sdc.be.model.*;
47 import org.openecomp.sdc.be.model.category.CategoryDefinition;
48 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
49 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
50 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
51 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
52 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
53 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
54 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
55 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
56 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
57 import org.openecomp.sdc.be.utils.TypeUtils;
58 import org.openecomp.sdc.common.log.wrappers.Logger;
59 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
60 import org.openecomp.sdc.common.util.ValidationUtils;
61 import org.openecomp.sdc.exception.ResponseFormat;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Component;
64 import org.springframework.web.context.WebApplicationContext;
65 import org.yaml.snakeyaml.Yaml;
67 import javax.servlet.ServletContext;
69 import java.util.Map.Entry;
70 import java.util.function.Function;
71 import java.util.regex.Pattern;
72 import java.util.stream.Collectors;
74 @Component("resourceImportManager")
75 public class ResourceImportManager {
77 private ServletContext servletContext;
80 private AuditingManager auditingManager;
83 private ResourceBusinessLogic resourceBusinessLogic;
86 private IGraphLockOperation graphLockOperation;
89 protected ComponentsUtils componentsUtils;
91 public static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern
92 .compile("[\\w\\-\\_\\d\\:]+");
94 protected CapabilityTypeOperation capabilityTypeOperation;
96 protected ToscaOperationFacade toscaOperationFacade;
98 private ResponseFormatManager responseFormatManager;
100 private static final Logger log = Logger.getLogger(ResourceImportManager.class);
102 public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) {
103 this.toscaOperationFacade = toscaOperationFacade;
106 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importNormativeResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) {
108 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
109 lifecycleChangeInfo.setUserRemarks("certification on import");
110 Function<Resource, Either<Boolean, ResponseFormat>> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
112 return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null, null, false);
115 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) {
117 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
118 lifecycleChangeInfo.setUserRemarks("certification on import");
119 Function<Resource, Either<Boolean, ResponseFormat>> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
121 return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null, null, false);
124 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, Function<Resource, Either<Boolean, ResponseFormat>> validationFunction,
125 LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction, boolean createNewVersion, boolean needLock, Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName, boolean isNested) {
126 Resource resource = new Resource();
127 ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
128 Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
130 String latestCertifiedResourceId = null;
132 boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty();
133 setConstantMetaData(resource, shouldBeCertified);
134 setMetaDataFromJson(resourceMetaData, resource);
136 Either<Boolean, ResponseFormat> validateResourceFromYaml = populateResourceFromYaml(resourceYml, resource);
137 if (validateResourceFromYaml.isRight()) {
138 ResponseFormat validationErrorResponse = validateResourceFromYaml.right().value();
139 auditErrorImport(resourceMetaData, creator, validationErrorResponse, true);
140 return Either.right(validationErrorResponse);
144 Either<Boolean, ResponseFormat> isValidResource = validationFunction.apply(resource);
145 if (isValidResource.isLeft()) {
146 // The flag createNewVersion if false doesn't create new version
147 if (!createNewVersion) {
148 Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resource.getName());
149 if (latestByName.isLeft()) {
150 return Either.right(componentsUtils.getResponseFormatByResource(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resource));
153 resource = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left;
154 Either<Resource, ResponseFormat> changeStateResponse;
156 if (nodeTypeArtifactsToHandle != null && !nodeTypeArtifactsToHandle.isEmpty()) {
157 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes =
158 resourceBusinessLogic.handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false);
159 if (handleNodeTypeArtifactsRes.isRight()) {
160 return Either.right(handleNodeTypeArtifactsRes.right().value());
163 latestCertifiedResourceId = getLatestCertifiedResourceId(resource);
164 changeStateResponse = resourceBusinessLogic.propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed);
165 if (changeStateResponse.isRight()) {
166 response = Either.right(changeStateResponse.right().value());
169 responsePair = new ImmutablePair<>(changeStateResponse.left().value(), response.left()
171 response = Either.left(responsePair);
175 ResponseFormat validationErrorResponse = isValidResource.right().value();
176 auditErrorImport(resourceMetaData, creator, validationErrorResponse, true);
177 response = Either.right(validationErrorResponse);
181 catch (RuntimeException e) {
182 ResponseFormat exceptionResponse = handleImportResourceException(resourceMetaData, creator, true, e, null);
183 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));
212 public void populateResourceMetadata(UploadResourceInfo resourceMetaData, Resource resource) {
213 if (resource != null && resourceMetaData != null) {
214 resource.setDescription(resourceMetaData.getDescription());
215 resource.setTags(resourceMetaData.getTags());
216 resource.setCategories(resourceMetaData.getCategories());
217 resource.setContactId(resourceMetaData.getContactId());
218 resource.setName(resourceMetaData.getName());
219 resource.setIcon(resourceMetaData.getResourceIconPath());
220 resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber());
221 resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType()));
222 if (resourceMetaData.getVendorName() != null) {
223 resource.setVendorName(resourceMetaData.getVendorName());
225 if (resourceMetaData.getVendorRelease() != null) {
226 resource.setVendorRelease(resourceMetaData.getVendorRelease());
231 public Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean isInTransaction) {
233 Resource resource = new Resource();
234 ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
235 Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
238 setMetaDataFromJson(resourceMetaData, resource);
240 Either<Boolean, ResponseFormat> validateResourceFromYaml = populateResourceFromYaml(resourceYml, resource);
241 if (validateResourceFromYaml.isRight()) {
242 ResponseFormat validationErrorResponse = validateResourceFromYaml.right().value();
243 auditErrorImport(resourceMetaData, creator, validationErrorResponse, false);
244 return Either.right(validationErrorResponse);
248 // currently import VF isn't supported. In future will be supported
249 // import VF only with CSAR file!!
250 if (ResourceTypeEnum.VF.equals(resource.getResourceType())) {
251 log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only");
252 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
255 resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
256 Either<Boolean, ResponseFormat> validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource);
258 if (validatePropertiesTypes.isLeft()) {
259 response = Either.left(resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false));
262 ResponseFormat validationErrorResponse = validatePropertiesTypes.right().value();
263 auditErrorImport(resourceMetaData, creator, validationErrorResponse, false);
264 response = Either.right(validationErrorResponse);
268 catch (ComponentException e) {
269 response = Either.right(handleImportResourceException(resourceMetaData, creator, false, e,
270 e.getResponseFormat()));
272 catch (RuntimeException e) {
273 response = Either.right(handleImportResourceException(resourceMetaData, creator, false, e, null));
279 Either<Boolean, ResponseFormat> populateResourceFromYaml(String resourceYml, Resource resource) {
280 @SuppressWarnings("unchecked")
281 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
282 Map<String, Object> toscaJsonAll = (Map<String, Object>) new Yaml().load(resourceYml);
283 Map<String, Object> toscaJson = toscaJsonAll;
285 // Checks if exist and builds the node_types map
286 if (toscaJsonAll.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
287 toscaJson = new HashMap<>();
288 toscaJson.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()));
291 Either<Resource, ResponseFormat> setDerivedFrom = setDerivedFrom(toscaJson, resource);
292 if (setDerivedFrom.isRight()) {
293 return Either.right(setDerivedFrom.right().value());
295 Resource parentResource = setDerivedFrom.left().value();
296 if (StringUtils.isEmpty(resource.getToscaResourceName())) {
297 setToscaResourceName(toscaJson, resource);
299 setAttributes(toscaJson, resource);
300 eitherResult = setCapabilities(toscaJson, resource, parentResource);
301 if (eitherResult.isRight()) {
304 eitherResult = setProperties(toscaJson, resource);
305 if (eitherResult.isRight()) {
308 eitherResult = setRequirements(toscaJson, resource, parentResource);
309 if (eitherResult.isRight()) {
312 setInterfaceLifecycle(toscaJson, resource);
317 private void setToscaResourceName(Map<String, Object> toscaJson, Resource resource) {
318 Either<Map<String, Object>, ResultStatusEnum> toscaElement = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
319 if (toscaElement.isLeft() || toscaElement.left().value().size() == 1) {
320 String toscaResourceName = toscaElement.left().value().keySet().iterator().next();
321 resource.setToscaResourceName(toscaResourceName);
325 private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource) {
326 Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.INTERFACES);
327 if (toscaInterfaces.isLeft()) {
328 Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
329 Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
330 Iterator<Entry<String, Object>> interfacesNameValue = jsonInterfaces.entrySet().iterator();
331 while (interfacesNameValue.hasNext()) {
332 Entry<String, Object> interfaceNameValue = interfacesNameValue.next();
333 Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue
335 if (eitherInterface.isRight()) {
336 log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
339 moduleInterfaces.put(interfaceNameValue.getKey(), eitherInterface.left().value());
343 if (moduleInterfaces.size() > 0) {
344 resource.setInterfaces(moduleInterfaces);
349 private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(Object interfaceJson) {
350 InterfaceDefinition interf = new InterfaceDefinition();
351 Either<InterfaceDefinition, ResultStatusEnum> result = Either.left(interf);
354 if (interfaceJson instanceof String) {
355 String requirementJsonString = (String) interfaceJson;
356 interf.setType(requirementJsonString);
358 else if (interfaceJson instanceof Map) {
359 Map<String, Object> requirementJsonMap = (Map<String, Object>) interfaceJson;
360 if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) {
361 String type = (String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName());
362 interf.setType(type);
363 interf.setUniqueId(type.toLowerCase());
367 result = Either.right(ResultStatusEnum.GENERAL_ERROR);
371 catch (Exception e) {
372 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface");
373 log.debug("error when creating interface, message:{}", e.getMessage(), e);
374 result = Either.right(ResultStatusEnum.GENERAL_ERROR);
380 private Either<Boolean, ResponseFormat> setRequirements(Map<String, Object> toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null
381 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
382 Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils.findFirstToscaListElement(toscaJson, TypeUtils.ToscaTagNamesEnum.REQUIREMENTS);
383 if (toscaRequirements.isLeft()) {
384 List<Object> jsonRequirements = toscaRequirements.left().value();
385 Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<>();
386 // Checking for name duplication
387 Set<String> reqNames = new HashSet<>();
388 // Getting flattened list of capabilities of parent node - cap name
390 Either<Map<String, String>, ResponseFormat> reqName2Type = getReqName2Type(parentResource);
391 if (reqName2Type.isRight()) {
392 ResponseFormat responseFormat = reqName2Type.right().value();
393 log.debug("Error during setting requirements of imported resource: {}", responseFormat);
394 return Either.right(responseFormat);
396 Map<String, String> reqName2TypeMap = reqName2Type.left().value();
397 for (Object jsonRequirementObj : jsonRequirements) {
399 Map<String, Object> requirementJsonWrapper = (Map<String, Object>) jsonRequirementObj;
400 String requirementName = requirementJsonWrapper.keySet().iterator().next();
401 String reqNameLowerCase = requirementName.toLowerCase();
402 if (reqNames.contains(reqNameLowerCase)) {
403 log.debug("More than one requirement with same name {} (case-insensitive) in imported TOSCA file is invalid", reqNameLowerCase);
404 return Either.right(componentsUtils.getResponseFormat(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase));
406 reqNames.add(reqNameLowerCase);
407 Either<RequirementDefinition, ResponseFormat> eitherRequirement = createRequirementFromImportFile(requirementJsonWrapper
408 .get(requirementName));
409 if (eitherRequirement.isRight()) {
410 log.info("error when creating Requirement:{}, for resource:{}", requirementName, resource.getName());
411 return Either.right(eitherRequirement.right().value());
413 RequirementDefinition requirementDef = eitherRequirement.left().value();
414 requirementDef.setName(requirementName);
415 if (moduleRequirements.containsKey(requirementDef.getCapability())) {
416 moduleRequirements.get(requirementDef.getCapability()).add(requirementDef);
419 List<RequirementDefinition> list = new ArrayList<>();
420 list.add(requirementDef);
421 moduleRequirements.put(requirementDef.getCapability(), list);
424 // Validating against req/cap of "derived from" node
425 Either<Boolean, ResponseFormat> validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef
426 .getCapability(), requirementDef.getName());
427 if (validateVsParentCap.isRight()) {
428 return Either.right(validateVsParentCap.right().value());
430 if (!validateVsParentCap.left().value()) {
431 log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResource
433 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement", requirementDef
435 .toLowerCase(), parentResource.getName());
436 return Either.right(responseFormat);
439 if (moduleRequirements.size() > 0) {
440 resource.setRequirements(moduleRequirements);
448 private Either<RequirementDefinition, ResponseFormat> createRequirementFromImportFile(Object requirementJson) {
449 RequirementDefinition requirement = new RequirementDefinition();
450 Either<RequirementDefinition, ResponseFormat> result = Either.left(requirement);
453 if (requirementJson instanceof String) {
454 String requirementJsonString = (String) requirementJson;
455 requirement.setCapability(requirementJsonString);
457 else if (requirementJson instanceof Map) {
458 Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
459 if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName())) {
460 requirement.setCapability((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName()));
463 if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE.getElementName())) {
464 requirement.setNode((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE.getElementName()));
467 if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
468 requirement.setRelationship((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
470 if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
471 List<Object> occurrencesList = (List) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
472 Either<Boolean, ResponseFormat> validateAndSetOccurrencesStatus = validateOccurrences(occurrencesList);
473 if (validateAndSetOccurrencesStatus.isRight()) {
474 result = Either.right(validateAndSetOccurrencesStatus.right().value());
477 if (validateAndSetOccurrencesStatus.left().value()) {
478 requirement.setMinOccurrences(occurrencesList.get(0).toString());
479 requirement.setMaxOccurrences(occurrencesList.get(1).toString());
485 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
489 catch (Exception e) {
490 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create Requirement");
491 log.debug("error when creating requirement, message:{}", e.getMessage(), e);
492 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
498 private Either<Boolean, ResponseFormat> setProperties(Map<String, Object> toscaJson, Resource resource) {
499 Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
500 ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
501 Either<Boolean, ResponseFormat> result = Either.left(true);
502 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
503 if (properties.isLeft()) {
504 List<PropertyDefinition> propertiesList = new ArrayList<>();
505 Map<String, PropertyDefinition> value = properties.left().value();
507 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
508 String name = entry.getKey();
509 if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
510 log.debug("The property with invalid name {} occured upon import resource {}. ", name, resource.getName());
511 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)));
513 PropertyDefinition propertyDefinition = entry.getValue();
514 propertyDefinition.setName(name);
515 propertiesList.add(propertyDefinition);
518 resource.setProperties(propertiesList);
520 else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
521 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties
523 .value(), JsonPresentationFields.PROPERTY)));
528 private ResultStatusEnum setAttributes(Map<String, Object> toscaJson, Resource resource) {
529 ResultStatusEnum result = ResultStatusEnum.OK;
530 Either<Map<String, PropertyDefinition>, ResultStatusEnum> attributes = ImportUtils.getAttributes(toscaJson);
531 if (attributes.isLeft()) {
532 List<PropertyDefinition> attributeList = new ArrayList<>();
533 Map<String, PropertyDefinition> value = attributes.left().value();
535 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
536 String name = entry.getKey();
537 PropertyDefinition attributeDef = entry.getValue();
538 attributeDef.setName(name);
539 attributeList.add(attributeDef);
542 resource.setAttributes(attributeList);
545 result = attributes.right().value();
550 private Either<Resource, ResponseFormat> setDerivedFrom(Map<String, Object> toscaJson, Resource resource) {
551 Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils.findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM);
552 Resource derivedFromResource = null;
553 if (toscaDerivedFromElement.isLeft()) {
554 String derivedFrom = toscaDerivedFromElement.left().value();
555 log.debug("Derived from TOSCA name is {}", derivedFrom);
556 resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom}));
557 Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom);
559 if (latestByToscaResourceName.isRight()) {
560 StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
561 if (operationStatus.equals(StorageOperationStatus.NOT_FOUND)) {
562 operationStatus = StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND;
564 log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus);
565 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
566 BeEcompErrorManager.getInstance()
567 .logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom);
568 return Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, derivedFrom));
570 derivedFromResource = latestByToscaResourceName.left().value();
572 return Either.left(derivedFromResource);
575 private Either<Boolean, ResponseFormat> setCapabilities(Map<String, Object> toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null
576 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
577 Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES);
578 if (toscaCapabilities.isLeft()) {
579 Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
580 Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<>();
581 Iterator<Entry<String, Object>> capabilitiesNameValue = jsonCapabilities.entrySet().iterator();
582 Set<String> capNames = new HashSet<>();
583 // Getting flattened list of capabilities of parent node - cap name
585 Either<Map<String, String>, ResponseFormat> capName2Type = getCapName2Type(parentResource);
586 if (capName2Type.isRight()) {
587 ResponseFormat responseFormat = capName2Type.right().value();
588 log.debug("Error during setting capabilities of imported resource: {}", responseFormat);
589 return Either.right(responseFormat);
591 Map<String, String> capName2TypeMap = capName2Type.left().value();
592 while (capabilitiesNameValue.hasNext()) {
593 Entry<String, Object> capabilityNameValue = capabilitiesNameValue.next();
595 // Validating that no req/cap duplicates exist in imported YAML
596 String capNameLowerCase = capabilityNameValue.getKey().toLowerCase();
597 if (capNames.contains(capNameLowerCase)) {
598 log.debug("More than one capability with same name {} (case-insensitive) in imported TOSCA file is invalid", capNameLowerCase);
599 return Either.right(componentsUtils.getResponseFormat(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase));
601 capNames.add(capNameLowerCase);
603 Either<CapabilityDefinition, ResponseFormat> eitherCapability = createCapabilityFromImportFile(capabilityNameValue
605 if (eitherCapability.isRight()) {
606 log.debug("error when creating capability:{}, for resource:{}", capabilityNameValue.getKey(), resource
608 return Either.right(eitherCapability.right().value());
611 CapabilityDefinition capabilityDef = eitherCapability.left().value();
612 capabilityDef.setName(capabilityNameValue.getKey());
613 if (moduleCapabilities.containsKey(capabilityDef.getType())) {
614 moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef);
617 List<CapabilityDefinition> list = new ArrayList<>();
618 list.add(capabilityDef);
619 moduleCapabilities.put(capabilityDef.getType(), list);
622 // Validating against req/cap of "derived from" node
623 Either<Boolean, ResponseFormat> validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef
624 .getType(), capabilityDef.getName());
625 if (validateVsParentCap.isRight()) {
626 return Either.right(validateVsParentCap.right().value());
628 if (!validateVsParentCap.left().value()) {
629 // Here parentResource is for sure not null, so it's
631 log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResource
633 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability", capabilityDef
635 .toLowerCase(), parentResource.getName());
636 return Either.right(responseFormat);
639 if (moduleCapabilities.size() > 0) {
640 resource.setCapabilities(moduleCapabilities);
648 private Either<Map<String, String>, ResponseFormat> getCapName2Type(Resource parentResource) {
649 Map<String, String> capName2type = new HashMap<>();
650 if (parentResource != null) {
651 Map<String, List<CapabilityDefinition>> capabilities = parentResource.getCapabilities();
652 if (capabilities != null) {
653 for (List<CapabilityDefinition> capDefinitions : capabilities.values()) {
654 for (CapabilityDefinition capDefinition : capDefinitions) {
655 String nameLowerCase = capDefinition.getName().toLowerCase();
656 if (capName2type.get(nameLowerCase) != null) {
657 String parentResourceName = parentResource.getName();
658 log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName, nameLowerCase);
659 BeEcompErrorManager.getInstance()
660 .logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name " + nameLowerCase, ErrorSeverity.ERROR);
661 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
663 capName2type.put(nameLowerCase, capDefinition.getType());
668 return Either.left(capName2type);
671 private Either<Map<String, String>, ResponseFormat> getReqName2Type(Resource parentResource) {
672 Map<String, String> reqName2type = new HashMap<>();
673 if (parentResource != null) {
674 Map<String, List<RequirementDefinition>> requirements = parentResource.getRequirements();
675 if (requirements != null) {
676 for (List<RequirementDefinition> reqDefinitions : requirements.values()) {
677 for (RequirementDefinition reqDefinition : reqDefinitions) {
678 String nameLowerCase = reqDefinition.getName().toLowerCase();
679 if (reqName2type.get(nameLowerCase) != null) {
680 String parentResourceName = parentResource.getName();
681 log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName, nameLowerCase);
682 BeEcompErrorManager.getInstance()
683 .logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name " + nameLowerCase, ErrorSeverity.ERROR);
684 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
686 reqName2type.put(nameLowerCase, reqDefinition.getCapability());
691 return Either.left(reqName2type);
694 private Either<Boolean, ResponseFormat> validateCapNameVsDerived(Map<String, String> parentCapName2Type, String childCapabilityType, String reqCapName) {
695 String capNameLowerCase = reqCapName.toLowerCase();
696 log.trace("Validating capability {} vs parent resource", capNameLowerCase);
697 String parentCapType = parentCapName2Type.get(capNameLowerCase);
698 if (parentCapType != null) {
699 if (childCapabilityType.equals(parentCapType)) {
700 log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase, childCapabilityType);
701 return Either.left(true);
703 Either<Boolean, StorageOperationStatus> capabilityTypeDerivedFrom = capabilityTypeOperation.isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType);
704 if (capabilityTypeDerivedFrom.isRight()) {
705 log.debug("Couldn't check whether imported resource capability derives from its parent's capability");
706 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom
709 return Either.right(responseFormat);
711 return Either.left(capabilityTypeDerivedFrom.left().value());
713 return Either.left(true);
716 private Either<CapabilityDefinition, ResponseFormat> createCapabilityFromImportFile(Object capabilityJson) {
718 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
719 Either<CapabilityDefinition, ResponseFormat> result = Either.left(capabilityDefinition);
722 if (capabilityJson instanceof String) {
723 String capabilityJsonString = (String) capabilityJson;
724 capabilityDefinition.setType(capabilityJsonString);
726 else if (capabilityJson instanceof Map) {
727 Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
729 if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) {
730 capabilityDefinition.setType((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()));
733 if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
734 capabilityDefinition.setValidSourceTypes((List<String>) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES
738 if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
739 capabilityDefinition.setDescription((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName()));
741 if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
742 List<Object> occurrencesList = (List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
743 Either<Boolean, ResponseFormat> validateAndSetOccurrencesStatus = validateOccurrences(occurrencesList);
744 if (validateAndSetOccurrencesStatus.isRight()) {
745 result = Either.right(validateAndSetOccurrencesStatus.right().value());
748 if (validateAndSetOccurrencesStatus.left().value()) {
749 capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
750 capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
753 if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName())) {
755 Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
756 if (propertiesRes.isRight()) {
757 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
765 .forEach(e -> e.getValue().setName(e.getKey().toLowerCase()));
766 List<ComponentInstanceProperty> capabilityProperties = propertiesRes.left()
770 .map(p -> new ComponentInstanceProperty(p, p
771 .getDefaultValue(), null))
772 .collect(Collectors.toList());
773 capabilityDefinition.setProperties(capabilityProperties);
777 else if (!(capabilityJson instanceof List)) {
779 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
783 catch (Exception e) {
784 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
785 log.debug("error when creating capability, message:{}", e.getMessage(), e);
786 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML));
792 private ResponseFormat handleImportResourceException(UploadResourceInfo resourceMetaData, User user, boolean isNormative, RuntimeException e, ResponseFormat responseFormat) {
793 if(responseFormat == null ){
794 responseFormat = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR);
796 String payloadName = (resourceMetaData != null) ? resourceMetaData.getPayloadName() : "";
797 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource " + payloadName);
798 log.debug("Error when importing resource from payload:{} Exception text: {}", payloadName, e.getMessage(), e);
799 auditErrorImport(resourceMetaData, user, responseFormat, isNormative);
800 return responseFormat;
803 private void auditErrorImport(UploadResourceInfo resourceMetaData, User user, ResponseFormat errorResponseWrapper, boolean isNormative) {
804 String version, lifeCycleState;
806 version = TypeUtils.FIRST_CERTIFIED_VERSION_VERSION;
807 lifeCycleState = LifecycleStateEnum.CERTIFIED.name();
811 lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
816 if (errorResponseWrapper.getMessageId() != null) {
817 message = errorResponseWrapper.getMessageId() + ": ";
819 message += errorResponseWrapper.getFormattedMessage();
822 AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
823 CommonAuditData.newBuilder()
824 .status(errorResponseWrapper.getStatus())
825 .description(message)
826 .requestId(ThreadLocalsHolder.getUuid())
828 new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()),
829 ResourceVersionInfo.newBuilder()
830 .state(lifeCycleState)
833 ResourceVersionInfo.newBuilder()
838 getAuditingManager().auditEvent(factory);
842 private void setMetaDataFromJson(UploadResourceInfo resourceMetaData, Resource resource) {
843 this.populateResourceMetadata(resourceMetaData, resource);
844 resource.setCreatorUserId(resourceMetaData.getContactId());
845 List<CategoryDefinition> categories = resourceMetaData.getCategories();
846 calculateResourceIsAbstract(resource, categories);
849 private void calculateResourceIsAbstract(Resource resource, List<CategoryDefinition> categories) {
850 if (categories != null && !categories.isEmpty()) {
851 CategoryDefinition categoryDef = categories.get(0);
852 resource.setAbstract(false);
853 if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName()
854 .equals(Constants.ABSTRACT_CATEGORY_NAME)) {
855 SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0);
856 if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) {
857 resource.setAbstract(true);
863 private void setConstantMetaData(Resource resource, boolean shouldBeCertified) {
865 LifecycleStateEnum state;
866 if (shouldBeCertified) {
867 version = TypeUtils.FIRST_CERTIFIED_VERSION_VERSION;
868 state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE;
871 version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION;
872 state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT;
874 resource.setVersion(version);
875 resource.setLifecycleState(state);
876 resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
877 resource.setVendorName(ImportUtils.Constants.VENDOR_NAME);
878 resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE);
882 private Either<Boolean, ResponseFormat> validateOccurrences(List<Object> occurrensesList) {
884 if (!ValidationUtils.validateListNotEmpty(occurrensesList)) {
885 log.debug("Occurrenses list empty");
886 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
887 return Either.right(responseFormat);
890 if (occurrensesList.size() < 2) {
891 log.debug("Occurrenses list size not 2");
892 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
893 return Either.right(responseFormat);
895 Object minObj = occurrensesList.get(0);
896 Object maxObj = occurrensesList.get(1);
897 Integer minOccurrences;
898 Integer maxOccurrences = null;
899 if (minObj instanceof Integer) {
900 minOccurrences = (Integer) minObj;
903 log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj);
904 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
905 return Either.right(responseFormat);
907 if (minOccurrences < 0) {
908 log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences);
909 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
910 return Either.right(responseFormat);
913 if (maxObj instanceof String) {
914 if ("UNBOUNDED".equals(maxObj)) {
915 return Either.left(true);
918 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
919 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
920 return Either.right(responseFormat);
924 if (maxObj instanceof Integer) {
925 maxOccurrences = (Integer) maxObj;
928 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
929 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
930 return Either.right(responseFormat);
933 if (maxOccurrences <= 0 || maxOccurrences < minOccurrences) {
934 log.debug("Invalid occurrenses format. min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
935 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_OCCURRENCES);
936 return Either.right(responseFormat);
940 return Either.left(true);
944 public synchronized void init(ServletContext servletContext) {
945 if (this.servletContext == null) {
946 this.servletContext = servletContext;
947 responseFormatManager = ResponseFormatManager.getInstance();
948 resourceBusinessLogic = getResourceBL(servletContext);
952 public boolean isResourceExist(String resourceName) {
953 return resourceBusinessLogic.isResourceExist(resourceName);
956 private ResourceBusinessLogic getResourceBL(ServletContext context) {
957 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
958 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
959 return webApplicationContext.getBean(ResourceBusinessLogic.class);
962 public ServletContext getServletContext() {
963 return servletContext;
966 public AuditingManager getAuditingManager() {
967 return auditingManager;
970 public ResponseFormatManager getResponseFormatManager() {
971 return responseFormatManager;
974 public void setResponseFormatManager(ResponseFormatManager responseFormatManager) {
975 this.responseFormatManager = responseFormatManager;
978 public ResourceBusinessLogic getResourceBusinessLogic() {
979 return resourceBusinessLogic;
982 public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) {
983 this.resourceBusinessLogic = resourceBusinessLogic;
986 public IGraphLockOperation getGraphLockOperation() {
987 return graphLockOperation;
990 public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
991 this.graphLockOperation = graphLockOperation;
994 public void setServletContext(ServletContext servletContext) {
995 this.servletContext = servletContext;
998 public void setAuditingManager(AuditingManager auditingManager) {
999 this.auditingManager = auditingManager;