8fe742af50b6fc263b51d2ba8e7f1d5d2241d1f6
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ResourceImportManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl;
23
24 import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType;
25 import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName;
26
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Comparator;
31 import java.util.EnumMap;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Optional;
39 import java.util.Set;
40 import java.util.function.Function;
41 import java.util.regex.Pattern;
42 import java.util.stream.Collectors;
43 import javax.servlet.ServletContext;
44 import org.apache.commons.codec.binary.Base64;
45 import org.apache.commons.collections4.CollectionUtils;
46 import org.apache.commons.collections4.MapUtils;
47 import org.apache.commons.lang3.StringUtils;
48 import org.apache.commons.lang3.tuple.ImmutablePair;
49 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
50 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
51 import org.openecomp.sdc.be.auditing.impl.resourceadmin.AuditImportResourceAdminEventFactory;
52 import org.openecomp.sdc.be.components.csar.CsarInfo;
53 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
54 import org.openecomp.sdc.be.components.impl.ImportUtils.Constants;
55 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
56 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
57 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
58 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
59 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
60 import org.openecomp.sdc.be.config.BeEcompErrorManager;
61 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
62 import org.openecomp.sdc.be.dao.api.ActionStatus;
63 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
64 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
65 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
66 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
67 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
68 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
69 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
70 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
71 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
72 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
73 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
74 import org.openecomp.sdc.be.impl.ComponentsUtils;
75 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
76 import org.openecomp.sdc.be.model.ArtifactDefinition;
77 import org.openecomp.sdc.be.model.AttributeDefinition;
78 import org.openecomp.sdc.be.model.CapabilityDefinition;
79 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
80 import org.openecomp.sdc.be.model.DataTypeDefinition;
81 import org.openecomp.sdc.be.model.InterfaceDefinition;
82 import org.openecomp.sdc.be.model.LifecycleStateEnum;
83 import org.openecomp.sdc.be.model.NodeTypesMetadataList;
84 import org.openecomp.sdc.be.model.PropertyDefinition;
85 import org.openecomp.sdc.be.model.RequirementDefinition;
86 import org.openecomp.sdc.be.model.Resource;
87 import org.openecomp.sdc.be.model.UploadResourceInfo;
88 import org.openecomp.sdc.be.model.User;
89 import org.openecomp.sdc.be.model.category.CategoryDefinition;
90 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
91 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
92 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
93 import org.openecomp.sdc.be.model.mapper.NodeTypeMetadataMapper;
94 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
95 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
96 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
97 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
98 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
99 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
100 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
101 import org.openecomp.sdc.be.utils.TypeUtils;
102 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
103 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
104 import org.openecomp.sdc.common.log.wrappers.Logger;
105 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
106 import org.openecomp.sdc.common.util.ValidationUtils;
107 import org.openecomp.sdc.exception.ResponseFormat;
108 import org.springframework.beans.factory.annotation.Autowired;
109 import org.springframework.web.context.WebApplicationContext;
110 import org.yaml.snakeyaml.Yaml;
111
112 @org.springframework.stereotype.Component("resourceImportManager")
113 public class ResourceImportManager {
114
115     static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("['\\w\\s\\-\\:]+");
116     private static final Logger log = Logger.getLogger(ResourceImportManager.class);
117     private final InterfaceDefinitionHandler interfaceDefinitionHandler;
118     private final ComponentsUtils componentsUtils;
119     private final CapabilityTypeOperation capabilityTypeOperation;
120     private final JanusGraphDao janusGraphDao;
121     private ServletContext servletContext;
122     private AuditingManager auditingManager;
123     private ResourceBusinessLogic resourceBusinessLogic;
124     @Autowired
125     private ServiceBusinessLogic serviceBusinessLogic;
126     private IGraphLockOperation graphLockOperation;
127     private ToscaOperationFacade toscaOperationFacade;
128     private ResponseFormatManager responseFormatManager;
129
130     @Autowired
131     public ResourceImportManager(final ComponentsUtils componentsUtils, final CapabilityTypeOperation capabilityTypeOperation,
132                                  final InterfaceDefinitionHandler interfaceDefinitionHandler, final JanusGraphDao janusGraphDao) {
133         this.componentsUtils = componentsUtils;
134         this.capabilityTypeOperation = capabilityTypeOperation;
135         this.interfaceDefinitionHandler = interfaceDefinitionHandler;
136         this.janusGraphDao = janusGraphDao;
137     }
138
139     public ServiceBusinessLogic getServiceBusinessLogic() {
140         return serviceBusinessLogic;
141     }
142
143     public void setServiceBusinessLogic(ServiceBusinessLogic serviceBusinessLogic) {
144         this.serviceBusinessLogic = serviceBusinessLogic;
145     }
146
147     @Autowired
148     public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) {
149         this.toscaOperationFacade = toscaOperationFacade;
150     }
151
152     public ImmutablePair<Resource, ActionStatus> importNormativeResource(final String resourceYml, final UploadResourceInfo resourceMetaData,
153                                                                          final User creator, final boolean createNewVersion, final boolean needLock,
154                                                                          final boolean isInTransaction) {
155         LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
156         lifecycleChangeInfo.setUserRemarks("certification on import");
157         Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
158         return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, createNewVersion,
159             needLock, null, null, false, null, null, false);
160     }
161
162     public void importAllNormativeResource(final String resourcesYaml, final NodeTypesMetadataList nodeTypesMetadataList, final User user,
163                                            final boolean createNewVersion, final boolean needLock) {
164         final Map<String, Object> nodeTypesYamlMap;
165         try {
166             nodeTypesYamlMap = new Yaml().load(resourcesYaml);
167         } catch (final Exception e) {
168             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), "Could not parse node types YAML", e);
169             throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TYPES_YAML);
170         }
171
172         if (!nodeTypesYamlMap.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName())) {
173             return;
174         }
175         final Map<String, Object> nodeTypesMap = (Map<String, Object>) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName());
176         try {
177             nodeTypesMetadataList.getNodeMetadataList().forEach(nodeTypeMetadata -> {
178                 final String nodeTypeToscaName = nodeTypeMetadata.getToscaName();
179                 final Map<String, Object> nodeTypeMap = (Map<String, Object>) nodeTypesMap.get(nodeTypeToscaName);
180                 if (nodeTypeMap == null) {
181                     log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(),
182                         "Could not find given node type '{}'. The node will not be created.", nodeTypeToscaName);
183                 } else {
184                     final Map<String, Map<String, Map<String, Object>>> nodeTypeDefinitionMap =
185                         Map.of(ToscaTagNamesEnum.NODE_TYPES.getElementName(),
186                             Map.of(nodeTypeToscaName, nodeTypeMap)
187                         );
188                     final String nodeTypeYaml = new Yaml().dump(nodeTypeDefinitionMap);
189                     importNormativeResource(nodeTypeYaml, NodeTypeMetadataMapper.mapTo(nodeTypeMetadata), user, createNewVersion, needLock, true);
190                 }
191             });
192             janusGraphDao.commit();
193         } catch (final Exception e) {
194             janusGraphDao.rollback();
195             throw e;
196         }
197     }
198
199     public ImmutablePair<Resource, ActionStatus> importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData,
200                                                                                  User creator, boolean createNewVersion, boolean needLock) {
201         LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
202         lifecycleChangeInfo.setUserRemarks("certification on import");
203         Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
204         return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock,
205             null, null, false, null, null, false);
206     }
207
208     public ImmutablePair<Resource, ActionStatus> importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
209                                                                          Function<Resource, Boolean> validationFunction,
210                                                                          LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction,
211                                                                          boolean createNewVersion, boolean needLock,
212                                                                          Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
213                                                                          List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
214                                                                          boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
215                                                                          boolean isNested) {
216         Resource resource = new Resource();
217         ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
218         Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
219         String latestCertifiedResourceId = null;
220         try {
221             boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty();
222             setConstantMetaData(resource, shouldBeCertified);
223             setMetaDataFromJson(resourceMetaData, resource);
224             populateResourceFromYaml(resourceYml, resource);
225             validationFunction.apply(resource);
226             resource.getComponentMetadataDefinition().getMetadataDataDefinition().setNormative(resourceMetaData.isNormative());
227             checkResourceExists(createNewVersion, csarInfo, resource);
228             resource = resourceBusinessLogic
229                 .createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left;
230             Resource changeStateResponse;
231             if (nodeTypeArtifactsToHandle != null && !nodeTypeArtifactsToHandle.isEmpty()) {
232                 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = resourceBusinessLogic
233                     .handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false);
234                 if (handleNodeTypeArtifactsRes.isRight()) {
235                     //TODO: should be used more correct action
236                     throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
237                 }
238             }
239             latestCertifiedResourceId = getLatestCertifiedResourceId(resource);
240             changeStateResponse = resourceBusinessLogic
241                 .propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed);
242             responsePair = new ImmutablePair<>(changeStateResponse, response.left().value().right);
243         } catch (RuntimeException e) {
244             handleImportResourceException(resourceMetaData, creator, true, e);
245         } finally {
246             if (latestCertifiedResourceId != null && needLock) {
247                 log.debug("unlock resource {}", latestCertifiedResourceId);
248                 graphLockOperation.unlockComponent(latestCertifiedResourceId, NodeTypeEnum.Resource);
249             }
250         }
251         return responsePair;
252     }
253
254     private void checkResourceExists(final boolean isCreate, final CsarInfo csarInfo, final Resource resource) {
255         if (isCreate) {
256             checkResourceExistsOnCreate(resource, csarInfo);
257         } else {
258             checkResourceExistsOnUpdate(resource);
259         }
260     }
261
262     private void checkResourceExistsOnCreate(final Resource resource, final CsarInfo csarInfo) {
263         if (isCsarPresent(csarInfo)) {
264             return;
265         }
266         final Either<Resource, StorageOperationStatus> resourceEither =
267             toscaOperationFacade.getComponentByNameAndVendorRelease(resource.getComponentType(), resource.getName(),
268                 resource.getVendorRelease(), JsonParseFlagEnum.ParseAll, resource.getModel());
269         if (resourceEither.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(resource.getModel(), resource)) {
270             if (resource.getModel() == null) {
271                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_VENDOR_RELEASE_ALREADY_EXISTS,
272                     resource.getName(), resource.getVendorRelease());
273             }
274             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_VENDOR_RELEASE_ALREADY_EXISTS_IN_MODEL,
275                 resource.getName(), resource.getVendorRelease(), resource.getModel());
276         }
277     }
278
279     private void checkResourceExistsOnUpdate(final Resource resource) {
280         final String model = resource.getModel();
281         final Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resource.getName(), model);
282         if (latestByName.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(model, resource)) {
283             if (model == null) {
284                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
285                     resource.getResourceType().name(), resource.getName());
286             }
287             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_MODEL_ALREADY_EXIST, resource.getName(), model);
288         }
289     }
290
291     private boolean isCsarPresent(final CsarInfo csarInfo) {
292         return csarInfo != null && StringUtils.isNotEmpty(csarInfo.getCsarUUID());
293     }
294
295     private String getLatestCertifiedResourceId(Resource resource) {
296         Map<String, String> allVersions = resource.getAllVersions();
297         Double latestCertifiedVersion = 0.0;
298         if (allVersions != null) {
299             for (String version : allVersions.keySet()) {
300                 Double dVersion = Double.valueOf(version);
301                 if ((dVersion > latestCertifiedVersion) && (version.endsWith(".0"))) {
302                     latestCertifiedVersion = dVersion;
303                 }
304             }
305             return allVersions.get(String.valueOf(latestCertifiedVersion));
306         } else {
307             return null;
308         }
309     }
310
311     public void populateResourceMetadata(UploadResourceInfo resourceMetaData, Resource resource) {
312         if (resource != null && resourceMetaData != null) {
313             resource.setDescription(resourceMetaData.getDescription());
314             resource.setTags(resourceMetaData.getTags());
315             resource.setCategories(resourceMetaData.getCategories());
316             resource.setContactId(resourceMetaData.getContactId());
317             resource.setName(resourceMetaData.getName());
318             resource.setIcon(resourceMetaData.getResourceIconPath());
319             resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber());
320             resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType()));
321             if (resourceMetaData.getVendorName() != null) {
322                 resource.setVendorName(resourceMetaData.getVendorName());
323             }
324             if (resourceMetaData.getVendorRelease() != null) {
325                 resource.setVendorRelease(resourceMetaData.getVendorRelease());
326             }
327             if (resourceMetaData.getModel() != null) {
328                 resource.setModel(resourceMetaData.getModel());
329             }
330         }
331     }
332
333     public ImmutablePair<Resource, ActionStatus> importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
334                                                                            boolean isInTransaction) {
335         Resource resource = new Resource();
336         ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
337         try {
338             setMetaDataFromJson(resourceMetaData, resource);
339             populateResourceFromYaml(resourceYml, resource);
340             // currently import VF isn't supported. In future will be supported import VF only with CSAR file!!
341             if (ResourceTypeEnum.VF == resource.getResourceType()) {
342                 log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only");
343                 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
344             }
345             resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
346             resourceBusinessLogic.validatePropertiesDefaultValues(resource);
347             responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false);
348         } catch (RuntimeException e) {
349             handleImportResourceException(resourceMetaData, creator, false, e);
350         }
351         return responsePair;
352     }
353
354     private void populateResourceFromYaml(final String resourceYml, Resource resource) {
355         @SuppressWarnings("unchecked") Object ymlObj = new Yaml().load(resourceYml);
356         if (ymlObj instanceof Map) {
357             final Either<Resource, StorageOperationStatus> existingResource = getExistingResource(resource);
358             final Map<String, Object> toscaJsonAll = (Map<String, Object>) ymlObj;
359             Map<String, Object> toscaJson = toscaJsonAll;
360             // Checks if exist and builds the node_types map
361             if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
362                 toscaJson = new HashMap<>();
363                 toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()));
364             }
365             final List<Object> foundElements = new ArrayList<>();
366             final Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils
367                 .findToscaElements(toscaJsonAll, ToscaTagNamesEnum.DATA_TYPES.getElementName(), ToscaElementTypeEnum.MAP, foundElements);
368             if (toscaElements.isLeft()) {
369                 final Map<String, Object> toscaAttributes = (Map<String, Object>) foundElements.get(0);
370                 if (MapUtils.isNotEmpty(toscaAttributes)) {
371                     resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes, resource.getModel()));
372                 }
373             }
374             // Derived From
375             final Resource parentResource = setDerivedFrom(toscaJson, resource);
376             if (StringUtils.isEmpty(resource.getToscaResourceName())) {
377                 setToscaResourceName(toscaJson, resource);
378             }
379             setCapabilities(toscaJson, resource, parentResource);
380             setProperties(toscaJson, resource, existingResource);
381             setAttributes(toscaJson, resource);
382             setRequirements(toscaJson, resource, parentResource);
383             setInterfaceLifecycle(toscaJson, resource);
384         } else {
385             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
386         }
387     }
388
389     private Either<Resource, StorageOperationStatus> getExistingResource(final Resource resource) {
390         final Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(
391             getVertexTypeEnum(resource.getResourceType()), propertiesToMatch(resource), propertiesToNotMatch(),
392             JsonParseFlagEnum.ParseAll, resource.getModel(), false);
393         if (byCriteria.isLeft() && CollectionUtils.isNotEmpty(byCriteria.left().value())) {
394             final List<GraphVertex> graphVertexList = byCriteria.left().value();
395             if (graphVertexList.size() == 1) {
396                 return toscaOperationFacade.getToscaElement(graphVertexList.get(0).getUniqueId());
397             } else {
398                 final Optional<GraphVertex> vertex = graphVertexList.stream()
399                     .max(Comparator.comparing(graphVertex -> (String) graphVertex.getMetadataProperties().get(GraphPropertyEnum.VERSION)));
400                 if (vertex.isPresent()) {
401                     return toscaOperationFacade.getToscaElement(vertex.get().getUniqueId());
402                 }
403             }
404         }
405         return Either.right(StorageOperationStatus.NOT_FOUND);
406     }
407
408     private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
409         return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
410     }
411
412     private Map<GraphPropertyEnum, Object> propertiesToMatch(final Resource resource) {
413         final Map<GraphPropertyEnum, Object> graphProperties = new EnumMap<>(GraphPropertyEnum.class);
414         graphProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normaliseComponentName(resource.getName()));
415         graphProperties.put(GraphPropertyEnum.COMPONENT_TYPE, resource.getComponentType().name());
416         graphProperties.put(GraphPropertyEnum.RESOURCE_TYPE, resource.getResourceType().name());
417         graphProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
418         return graphProperties;
419     }
420
421     private Map<GraphPropertyEnum, Object> propertiesToNotMatch() {
422         final Map<GraphPropertyEnum, Object> graphProperties = new EnumMap<>(GraphPropertyEnum.class);
423         graphProperties.put(GraphPropertyEnum.IS_DELETED, true);
424         graphProperties.put(GraphPropertyEnum.IS_ARCHIVED, true);
425         return graphProperties;
426     }
427
428     private void setToscaResourceName(Map<String, Object> toscaJson, Resource resource) {
429         Either<Map<String, Object>, ResultStatusEnum> toscaElement = ImportUtils
430             .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES);
431         if (toscaElement.isLeft() && toscaElement.left().value().size() == 1) {
432             String toscaResourceName = toscaElement.left().value().keySet().iterator().next();
433             resource.setToscaResourceName(toscaResourceName);
434         }
435     }
436
437     private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource) {
438         Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils
439             .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES);
440         if (toscaInterfaces.isLeft()) {
441             Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
442             for (final Entry<String, Object> interfaceNameValue : toscaInterfaces.left().value().entrySet()) {
443                 final Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue.getValue(),
444                     resource.getModel());
445                 if (eitherInterface.isRight()) {
446                     log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
447                 } else {
448                     final InterfaceDefinition interfaceDefinition = eitherInterface.left().value();
449                     moduleInterfaces.put(interfaceDefinition.getType(), interfaceDefinition);
450                 }
451             }
452             if (!moduleInterfaces.isEmpty()) {
453                 resource.setInterfaces(moduleInterfaces);
454             }
455         }
456     }
457
458     private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(final Object interfaceJson, final String model) {
459         try {
460             if (interfaceJson instanceof String) {
461                 final InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
462                 interfaceDefinition.setType((String) interfaceJson);
463                 return Either.left(interfaceDefinition);
464             }
465             if (interfaceJson instanceof Map) {
466                 final Map<String, Object> interfaceJsonMap = (Map<String, Object>) interfaceJson;
467                 final InterfaceDefinition interfaceDefinition = interfaceDefinitionHandler.create(interfaceJsonMap, model);
468                 return Either.left(interfaceDefinition);
469             }
470             return Either.right(ResultStatusEnum.GENERAL_ERROR);
471         } catch (final Exception e) {
472             BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface");
473             log.debug("error when creating interface, message:{}", e.getMessage(), e);
474             return Either.right(ResultStatusEnum.GENERAL_ERROR);
475         }
476     }
477
478     private void setRequirements(Map<String, Object> toscaJson, Resource resource,
479                                  Resource parentResource) {// Note that parentResource can be null
480         Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils
481             .findFirstToscaListElement(toscaJson, ToscaTagNamesEnum.REQUIREMENTS);
482         if (toscaRequirements.isLeft()) {
483             List<Object> jsonRequirements = toscaRequirements.left().value();
484             Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<>();
485             // Checking for name duplication
486             Set<String> reqNames = new HashSet<>();
487             // Getting flattened list of capabilities of parent node - cap name to cap type
488             Map<String, String> reqName2TypeMap = getReqName2Type(parentResource);
489             for (Object jsonRequirementObj : jsonRequirements) {
490                 // Requirement
491                 Map<String, Object> requirementJsonWrapper = (Map<String, Object>) jsonRequirementObj;
492                 String requirementName = requirementJsonWrapper.keySet().iterator().next();
493                 String reqNameLowerCase = requirementName.toLowerCase();
494                 if (reqNames.contains(reqNameLowerCase)) {
495                     log.debug("More than one requirement with same name {} (case-insensitive) in imported TOSCA file is invalid", reqNameLowerCase);
496                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase);
497                 }
498                 reqNames.add(reqNameLowerCase);
499                 RequirementDefinition requirementDef = createRequirementFromImportFile(requirementJsonWrapper.get(requirementName));
500                 requirementDef.setName(requirementName);
501                 if (moduleRequirements.containsKey(requirementDef.getCapability())) {
502                     moduleRequirements.get(requirementDef.getCapability()).add(requirementDef);
503                 } else {
504                     List<RequirementDefinition> list = new ArrayList<>();
505                     list.add(requirementDef);
506                     moduleRequirements.put(requirementDef.getCapability(), list);
507                 }
508                 // Validating against req/cap of "derived from" node
509                 Boolean validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef.getCapability(), requirementDef.getName());
510                 if (!validateVsParentCap) {
511                     String parentResourceName = parentResource != null ? parentResource.getName() : "";
512                     log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResourceName);
513                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement",
514                         requirementDef.getName().toLowerCase(), parentResourceName);
515                 }
516             }
517             if (moduleRequirements.size() > 0) {
518                 resource.setRequirements(moduleRequirements);
519             }
520         }
521     }
522
523     private RequirementDefinition createRequirementFromImportFile(Object requirementJson) {
524         RequirementDefinition requirement = new RequirementDefinition();
525         if (requirementJson instanceof String) {
526             String requirementJsonString = (String) requirementJson;
527             requirement.setCapability(requirementJsonString);
528         } else if (requirementJson instanceof Map) {
529             Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
530             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) {
531                 requirement.setCapability((String) requirementJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName()));
532             }
533             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) {
534                 requirement.setNode((String) requirementJsonMap.get(ToscaTagNamesEnum.NODE.getElementName()));
535             }
536             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
537                 requirement.setRelationship((String) requirementJsonMap.get(ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
538             }
539             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
540                 List<Object> occurrencesList = (List) requirementJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
541                 validateOccurrences(occurrencesList);
542                 requirement.setMinOccurrences(occurrencesList.get(0).toString());
543                 requirement.setMaxOccurrences(occurrencesList.get(1).toString());
544             }
545         } else {
546             throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
547         }
548         return requirement;
549     }
550
551     private void setProperties(final Map<String, Object> toscaJson, final Resource resource,
552                                final Either<Resource, StorageOperationStatus> existingResource) {
553         final Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
554         ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
555         final Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
556         if (properties.isLeft()) {
557             final Map<String, PropertyDefinition> propertyDefinitionMap = properties.left().value();
558             if (MapUtils.isNotEmpty(propertyDefinitionMap)) {
559                 final List<PropertyDefinition> propertiesList = new ArrayList<>();
560                 for (final Entry<String, PropertyDefinition> entry : propertyDefinitionMap.entrySet()) {
561                     addPropertyToList(resource.getName(), propertiesList, entry);
562                 }
563                 if (existingResource.isLeft()) {
564                     final List<PropertyDefinition> userCreatedResourceProperties =
565                         existingResource.left().value().getProperties().stream()
566                             .filter(PropertyDataDefinition::isUserCreated)
567                             .filter(propertyDefinition -> !propertyDefinitionMap.containsKey(propertyDefinition.getName()))
568                             .collect(Collectors.toList());
569                     if (CollectionUtils.isNotEmpty(userCreatedResourceProperties)) {
570                         propertiesList.addAll(userCreatedResourceProperties);
571                     }
572                 }
573
574                 resource.setProperties(propertiesList);
575             }
576         } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
577             throw new ByActionStatusComponentException(
578                 componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY));
579         }
580     }
581
582     private void addPropertyToList(final String resourceName,
583                                    final List<PropertyDefinition> propertiesList,
584                                    final Entry<String, PropertyDefinition> entry) {
585         final String propertyName = entry.getKey();
586         if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(propertyName).matches()) {
587             log.debug("The property with invalid name {} occured upon import resource {}. ", propertyName, resourceName);
588             throw new ByActionStatusComponentException(
589                 componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY));
590         }
591         final PropertyDefinition propertyDefinition = entry.getValue();
592         propertyDefinition.setName(propertyName);
593         propertiesList.add(propertyDefinition);
594     }
595
596     private void setAttributes(final Map<String, Object> originalToscaJsonMap, final Resource resource) {
597         final Map<String, Object> toscaJsonMap = new HashMap<>(originalToscaJsonMap);
598         ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities");
599         final Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributeEither = ImportUtils.getAttributes(toscaJsonMap);
600         if (getAttributeEither.isRight()) {
601             final ResultStatusEnum resultStatus = getAttributeEither.right().value();
602             if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) {
603                 return;
604             }
605             throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES));
606         }
607         final List<AttributeDefinition> attributeDefinitionList = new ArrayList<>();
608         final Map<String, AttributeDefinition> attributeMap = getAttributeEither.left().value();
609         if (MapUtils.isEmpty(attributeMap)) {
610             return;
611         }
612         for (final Entry<String, AttributeDefinition> entry : attributeMap.entrySet()) {
613             final String name = entry.getKey();
614             if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
615                 log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", name, resource.getName());
616                 throw new ByActionStatusComponentException(
617                     componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, JsonPresentationFields.ATTRIBUTES));
618             }
619             final AttributeDefinition attributeDefinition = entry.getValue();
620             attributeDefinition.setName(name);
621             attributeDefinitionList.add(attributeDefinition);
622         }
623         resource.setAttributes(attributeDefinitionList);
624     }
625
626     private Resource setDerivedFrom(Map<String, Object> toscaJson, Resource resource) {
627         Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils
628             .findFirstToscaStringElement(toscaJson, ToscaTagNamesEnum.DERIVED_FROM);
629         Resource derivedFromResource = null;
630         if (toscaDerivedFromElement.isLeft()) {
631             String derivedFrom = toscaDerivedFromElement.left().value();
632             log.debug("Derived from TOSCA name is {}", derivedFrom);
633             resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom}));
634             Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom,
635                 resource.getModel());
636             if (latestByToscaResourceName.isRight()) {
637                 StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
638                 if (operationStatus == StorageOperationStatus.NOT_FOUND) {
639                     operationStatus = StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND;
640                 }
641                 log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus);
642                 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
643                 BeEcompErrorManager.getInstance().logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom);
644                 throw new ByActionStatusComponentException(convertFromStorageResponse, derivedFrom);
645             }
646             derivedFromResource = latestByToscaResourceName.left().value();
647         }
648         return derivedFromResource;
649     }
650
651     private void setCapabilities(Map<String, Object> toscaJson, Resource resource,
652                                  Resource parentResource) {// Note that parentResource can be null
653         Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils
654             .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.CAPABILITIES);
655         if (toscaCapabilities.isLeft()) {
656             Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
657             Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<>();
658             Iterator<Entry<String, Object>> capabilitiesNameValue = jsonCapabilities.entrySet().iterator();
659             Set<String> capNames = new HashSet<>();
660             // Getting flattened list of capabilities of parent node - cap name
661
662             // to cap type
663             Map<String, String> capName2TypeMap = getCapName2Type(parentResource);
664             while (capabilitiesNameValue.hasNext()) {
665                 Entry<String, Object> capabilityNameValue = capabilitiesNameValue.next();
666                 // Validating that no req/cap duplicates exist in imported YAML
667                 String capNameLowerCase = capabilityNameValue.getKey().toLowerCase();
668                 if (capNames.contains(capNameLowerCase)) {
669                     log.debug("More than one capability with same name {} (case-insensitive) in imported TOSCA file is invalid", capNameLowerCase);
670                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase);
671                 }
672                 capNames.add(capNameLowerCase);
673                 CapabilityDefinition capabilityDef = createCapabilityFromImportFile(capabilityNameValue.getValue());
674                 capabilityDef.setName(capabilityNameValue.getKey());
675                 if (moduleCapabilities.containsKey(capabilityDef.getType())) {
676                     moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef);
677                 } else {
678                     List<CapabilityDefinition> list = new ArrayList<>();
679                     list.add(capabilityDef);
680                     moduleCapabilities.put(capabilityDef.getType(), list);
681                 }
682                 // Validating against req/cap of "derived from" node
683                 Boolean validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef.getType(), capabilityDef.getName());
684                 if (!validateVsParentCap) {
685                     // Here parentResource is for sure not null, so it's
686
687                     // null-safe
688
689                     // Check added to avoid sonar warning
690                     String parentResourceName = parentResource != null ? parentResource.getName() : "";
691                     log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResourceName);
692                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability",
693                         capabilityDef.getName().toLowerCase(), parentResourceName);
694                 }
695             }
696             if (moduleCapabilities.size() > 0) {
697                 resource.setCapabilities(moduleCapabilities);
698             }
699         }
700     }
701
702     private Map<String, String> getCapName2Type(Resource parentResource) {
703         Map<String, String> capName2type = new HashMap<>();
704         if (parentResource != null) {
705             Map<String, List<CapabilityDefinition>> capabilities = parentResource.getCapabilities();
706             if (capabilities != null) {
707                 for (List<CapabilityDefinition> capDefinitions : capabilities.values()) {
708                     for (CapabilityDefinition capDefinition : capDefinitions) {
709                         String nameLowerCase = capDefinition.getName().toLowerCase();
710                         if (capName2type.get(nameLowerCase) != null) {
711                             String parentResourceName = parentResource.getName();
712                             log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName,
713                                 nameLowerCase);
714                             BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
715                                 "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name "
716                                     + nameLowerCase, ErrorSeverity.ERROR);
717                             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
718                         }
719                         capName2type.put(nameLowerCase, capDefinition.getType());
720                     }
721                 }
722             }
723         }
724         return capName2type;
725     }
726
727     private Map<String, String> getReqName2Type(Resource parentResource) {
728         Map<String, String> reqName2type = new HashMap<>();
729         if (parentResource != null) {
730             Map<String, List<RequirementDefinition>> requirements = parentResource.getRequirements();
731             if (requirements != null) {
732                 for (List<RequirementDefinition> reqDefinitions : requirements.values()) {
733                     for (RequirementDefinition reqDefinition : reqDefinitions) {
734                         String nameLowerCase = reqDefinition.getName().toLowerCase();
735                         if (reqName2type.get(nameLowerCase) != null) {
736                             String parentResourceName = parentResource.getName();
737                             log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName,
738                                 nameLowerCase);
739                             BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
740                                 "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name "
741                                     + nameLowerCase, ErrorSeverity.ERROR);
742                             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
743                         }
744                         reqName2type.put(nameLowerCase, reqDefinition.getCapability());
745                     }
746                 }
747             }
748         }
749         return reqName2type;
750     }
751
752     private Boolean validateCapNameVsDerived(Map<String, String> parentCapName2Type, String childCapabilityType, String reqCapName) {
753         String capNameLowerCase = reqCapName.toLowerCase();
754         log.trace("Validating capability {} vs parent resource", capNameLowerCase);
755         String parentCapType = parentCapName2Type.get(capNameLowerCase);
756         if (parentCapType != null) {
757             if (childCapabilityType.equals(parentCapType)) {
758                 log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase,
759                     childCapabilityType);
760                 return true;
761             }
762             Either<Boolean, StorageOperationStatus> capabilityTypeDerivedFrom = capabilityTypeOperation
763                 .isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType);
764             if (capabilityTypeDerivedFrom.isRight()) {
765                 log.debug("Couldn't check whether imported resource capability derives from its parent's capability");
766                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom.right().value()));
767             }
768             return capabilityTypeDerivedFrom.left().value();
769         }
770         return true;
771     }
772
773     private CapabilityDefinition createCapabilityFromImportFile(Object capabilityJson) {
774         CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
775         if (capabilityJson instanceof String) {
776             String capabilityJsonString = (String) capabilityJson;
777             capabilityDefinition.setType(capabilityJsonString);
778         } else if (capabilityJson instanceof Map) {
779             Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
780             // Type
781             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) {
782                 capabilityDefinition.setType((String) capabilityJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()));
783             }
784             // ValidSourceTypes
785             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
786                 capabilityDefinition
787                     .setValidSourceTypes((List<String>) capabilityJsonMap.get(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
788             }
789             // ValidSourceTypes
790             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
791                 capabilityDefinition.setDescription((String) capabilityJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName()));
792             }
793             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
794                 List<Object> occurrencesList = (List) capabilityJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
795                 validateOccurrences(occurrencesList);
796                 capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
797                 capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
798             }
799             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) {
800                 Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
801                 if (propertiesRes.isRight()) {
802                     throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
803                 } else {
804                     propertiesRes.left().value().entrySet().stream().forEach(e -> e.getValue().setName(e.getKey().toLowerCase()));
805                     List<ComponentInstanceProperty> capabilityProperties = propertiesRes.left().value().values().stream()
806                         .map(p -> new ComponentInstanceProperty(p, p.getDefaultValue(), null)).collect(Collectors.toList());
807                     capabilityDefinition.setProperties(capabilityProperties);
808                 }
809             }
810         } else if (!(capabilityJson instanceof List)) {
811             throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
812         }
813         return capabilityDefinition;
814     }
815
816     private void handleImportResourceException(UploadResourceInfo resourceMetaData, User user, boolean isNormative, RuntimeException e) {
817         ResponseFormat responseFormat;
818         ComponentException newException;
819         if (e instanceof ComponentException) {
820             ComponentException componentException = (ComponentException) e;
821             responseFormat = componentException.getResponseFormat();
822             if (responseFormat == null) {
823                 responseFormat = getResponseFormatManager().getResponseFormat(componentException.getActionStatus(), componentException.getParams());
824             }
825             newException = componentException;
826         } else {
827             responseFormat = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR);
828             newException = new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
829         }
830         String payloadName = (resourceMetaData != null) ? resourceMetaData.getPayloadName() : "";
831         BeEcompErrorManager.getInstance().logBeSystemError("Import Resource " + payloadName);
832         log.debug("Error when importing resource from payload:{} Exception text: {}", payloadName, e.getMessage(), e);
833         auditErrorImport(resourceMetaData, user, responseFormat, isNormative);
834         throw newException;
835     }
836
837     private void auditErrorImport(UploadResourceInfo resourceMetaData, User user, ResponseFormat errorResponseWrapper, boolean isNormative) {
838         String version, lifeCycleState;
839         if (isNormative) {
840             version = TypeUtils.getFirstCertifiedVersionVersion();
841             lifeCycleState = LifecycleStateEnum.CERTIFIED.name();
842         } else {
843             version = "";
844             lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
845         }
846         String message = "";
847         if (errorResponseWrapper.getMessageId() != null) {
848             message = errorResponseWrapper.getMessageId() + ": ";
849         }
850         message += errorResponseWrapper.getFormattedMessage();
851         AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
852             CommonAuditData.newBuilder().status(errorResponseWrapper.getStatus()).description(message).requestId(ThreadLocalsHolder.getUuid())
853                 .build(), new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()),
854             ResourceVersionInfo.newBuilder().state(lifeCycleState).version(version).build(),
855             ResourceVersionInfo.newBuilder().state("").version("").build(), "", user, "");
856         getAuditingManager().auditEvent(factory);
857     }
858
859     private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) {
860         this.populateResourceMetadata(resourceMetaData, resource);
861         resource.setCreatorUserId(resourceMetaData.getContactId());
862         final String payloadData = resourceMetaData.getPayloadData();
863         if (payloadData != null) {
864             resource.setToscaVersion(getToscaVersion(payloadData));
865         }
866         final List<CategoryDefinition> categories = resourceMetaData.getCategories();
867         calculateResourceIsAbstract(resource, categories);
868     }
869
870     private Map<String, Object> decodePayload(final String payloadData) {
871         final String decodedPayload = new String(Base64.decodeBase64(payloadData));
872         return (Map<String, Object>) new Yaml().load(decodedPayload);
873     }
874
875     private String getToscaVersion(final String payloadData) {
876         final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData);
877         final Either<String, ResultStatusEnum> findFirstToscaStringElement = ImportUtils
878             .findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.TOSCA_VERSION);
879         if (findFirstToscaStringElement.isLeft()) {
880             return findFirstToscaStringElement.left().value();
881         } else {
882             return null;
883         }
884     }
885
886     private void calculateResourceIsAbstract(Resource resource, List<CategoryDefinition> categories) {
887         if (categories != null && !categories.isEmpty()) {
888             CategoryDefinition categoryDef = categories.get(0);
889             resource.setAbstract(false);
890             if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName().equals(Constants.ABSTRACT_CATEGORY_NAME)) {
891                 SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0);
892                 if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) {
893                     resource.setAbstract(true);
894                 }
895             }
896         }
897     }
898
899     private void setConstantMetaData(Resource resource, boolean shouldBeCertified) {
900         String version;
901         LifecycleStateEnum state;
902         if (shouldBeCertified) {
903             version = TypeUtils.getFirstCertifiedVersionVersion();
904             state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE;
905         } else {
906             version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION;
907             state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT;
908         }
909         resource.setVersion(version);
910         resource.setLifecycleState(state);
911         resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
912         resource.setVendorName(ImportUtils.Constants.VENDOR_NAME);
913         resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE);
914     }
915
916     private void validateOccurrences(List<Object> occurrensesList) {
917         if (!ValidationUtils.validateListNotEmpty(occurrensesList)) {
918             log.debug("Occurrenses list empty");
919             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
920         }
921         if (occurrensesList.size() < 2) {
922             log.debug("Occurrenses list size not 2");
923             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
924         }
925         Object minObj = occurrensesList.get(0);
926         Object maxObj = occurrensesList.get(1);
927         Integer minOccurrences;
928         Integer maxOccurrences;
929         if (minObj instanceof Integer) {
930             minOccurrences = (Integer) minObj;
931         } else {
932             log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj);
933             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
934         }
935         if (minOccurrences < 0) {
936             log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences);
937             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
938         }
939         if (maxObj instanceof String) {
940             if (!"UNBOUNDED".equals(maxObj)) {
941                 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
942                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
943             }
944         } else {
945             if (maxObj instanceof Integer) {
946                 maxOccurrences = (Integer) maxObj;
947             } else {
948                 log.debug("Invalid occurrenses format.  Max occurrence is {}", maxObj);
949                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
950             }
951             if (maxOccurrences < 0 || maxOccurrences < minOccurrences) {
952                 log.debug("Invalid occurrenses format.  min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
953                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
954             }
955         }
956     }
957
958     public synchronized void init(ServletContext servletContext) {
959         if (this.servletContext == null) {
960             this.servletContext = servletContext;
961             responseFormatManager = ResponseFormatManager.getInstance();
962             resourceBusinessLogic = getResourceBL(servletContext);
963         }
964     }
965
966     public boolean isResourceExist(String resourceName) {
967         return resourceBusinessLogic.isResourceExist(resourceName);
968     }
969
970     private ResourceBusinessLogic getResourceBL(ServletContext context) {
971         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context
972             .getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
973         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
974         return webApplicationContext.getBean(ResourceBusinessLogic.class);
975     }
976
977     public ServletContext getServletContext() {
978         return servletContext;
979     }
980
981     public void setServletContext(ServletContext servletContext) {
982         this.servletContext = servletContext;
983     }
984
985     public AuditingManager getAuditingManager() {
986         return auditingManager;
987     }
988
989     @Autowired
990     public void setAuditingManager(AuditingManager auditingManager) {
991         this.auditingManager = auditingManager;
992     }
993
994     public ResponseFormatManager getResponseFormatManager() {
995         return responseFormatManager;
996     }
997
998     public void setResponseFormatManager(ResponseFormatManager responseFormatManager) {
999         this.responseFormatManager = responseFormatManager;
1000     }
1001
1002     public ResourceBusinessLogic getResourceBusinessLogic() {
1003         return resourceBusinessLogic;
1004     }
1005
1006     @Autowired
1007     public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) {
1008         this.resourceBusinessLogic = resourceBusinessLogic;
1009     }
1010
1011     public IGraphLockOperation getGraphLockOperation() {
1012         return graphLockOperation;
1013     }
1014
1015     @Autowired
1016     public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
1017         this.graphLockOperation = graphLockOperation;
1018     }
1019
1020     private List<DataTypeDefinition> extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic,
1021                                                              final Map<String, Object> foundElements,
1022                                                              final String model) {
1023         final List<DataTypeDefinition> dataTypeDefinitionList = new ArrayList<>();
1024         if (MapUtils.isNotEmpty(foundElements)) {
1025             final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll =
1026                 resourceBusinessLogic.applicationDataTypeCache.getAll(model);
1027             if (dataTypeCacheAll.isLeft()) {
1028                 for (final Entry<String, Object> attributeNameValue : foundElements.entrySet()) {
1029                     final Object value = attributeNameValue.getValue();
1030                     if (value instanceof Map) {
1031                         final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue);
1032                         final DataTypeDefinition dataTypeDefinitionParent = dataTypeCacheAll.left().value()
1033                             .get(dataTypeDefinition.getDerivedFromName());
1034                         dataTypeDefinition.setDerivedFrom(dataTypeDefinitionParent);
1035                         dataTypeDefinitionList.add(dataTypeDefinition);
1036                     } else {
1037                         dataTypeDefinitionList.add(createDataType(String.valueOf(value)));
1038                     }
1039                 }
1040             }
1041         }
1042         return dataTypeDefinitionList;
1043     }
1044 }