Maintain VFC UI added interface operations after an upgrade
[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, existingResource);
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, Either<Resource, StorageOperationStatus> existingResource) {
438         final Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils
439             .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES);
440         if (toscaInterfaces.isLeft()) {
441             final Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
442             final Map<String, Object> map = toscaInterfaces.left().value();
443             for (final Entry<String, Object> interfaceNameValue : map.entrySet()) {
444                 final Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue.getValue(),
445                     resource.getModel());
446                 if (eitherInterface.isRight()) {
447                     log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
448                 } else {
449                     final InterfaceDefinition interfaceDefinition = eitherInterface.left().value();
450                     moduleInterfaces.put(interfaceDefinition.getType(), interfaceDefinition);
451                 }
452             }
453             if (existingResource.isLeft()) {
454                 final Map<String, InterfaceDefinition> userCreatedInterfaceDefinitions =
455                     existingResource.left().value().getInterfaces().entrySet().stream()
456                         .filter(i -> i.getValue().isUserCreated())
457                         .filter(i -> !map.containsKey(i.getValue().getType()))
458                         .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
459                 if (MapUtils.isNotEmpty(userCreatedInterfaceDefinitions)) {
460                     moduleInterfaces.putAll(userCreatedInterfaceDefinitions);
461                 }
462             }
463
464             if (MapUtils.isNotEmpty(moduleInterfaces)) {
465                 resource.setInterfaces(moduleInterfaces);
466             }
467         }
468     }
469
470     private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(final Object interfaceJson, final String model) {
471         try {
472             if (interfaceJson instanceof String) {
473                 final InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
474                 interfaceDefinition.setType((String) interfaceJson);
475                 return Either.left(interfaceDefinition);
476             }
477             if (interfaceJson instanceof Map) {
478                 final Map<String, Object> interfaceJsonMap = (Map<String, Object>) interfaceJson;
479                 final InterfaceDefinition interfaceDefinition = interfaceDefinitionHandler.create(interfaceJsonMap, model);
480                 return Either.left(interfaceDefinition);
481             }
482             return Either.right(ResultStatusEnum.GENERAL_ERROR);
483         } catch (final Exception e) {
484             BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface");
485             log.debug("error when creating interface, message:{}", e.getMessage(), e);
486             return Either.right(ResultStatusEnum.GENERAL_ERROR);
487         }
488     }
489
490     private void setRequirements(Map<String, Object> toscaJson, Resource resource,
491                                  Resource parentResource) {// Note that parentResource can be null
492         Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils
493             .findFirstToscaListElement(toscaJson, ToscaTagNamesEnum.REQUIREMENTS);
494         if (toscaRequirements.isLeft()) {
495             List<Object> jsonRequirements = toscaRequirements.left().value();
496             Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<>();
497             // Checking for name duplication
498             Set<String> reqNames = new HashSet<>();
499             // Getting flattened list of capabilities of parent node - cap name to cap type
500             Map<String, String> reqName2TypeMap = getReqName2Type(parentResource);
501             for (Object jsonRequirementObj : jsonRequirements) {
502                 // Requirement
503                 Map<String, Object> requirementJsonWrapper = (Map<String, Object>) jsonRequirementObj;
504                 String requirementName = requirementJsonWrapper.keySet().iterator().next();
505                 String reqNameLowerCase = requirementName.toLowerCase();
506                 if (reqNames.contains(reqNameLowerCase)) {
507                     log.debug("More than one requirement with same name {} (case-insensitive) in imported TOSCA file is invalid", reqNameLowerCase);
508                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase);
509                 }
510                 reqNames.add(reqNameLowerCase);
511                 RequirementDefinition requirementDef = createRequirementFromImportFile(requirementJsonWrapper.get(requirementName));
512                 requirementDef.setName(requirementName);
513                 if (moduleRequirements.containsKey(requirementDef.getCapability())) {
514                     moduleRequirements.get(requirementDef.getCapability()).add(requirementDef);
515                 } else {
516                     List<RequirementDefinition> list = new ArrayList<>();
517                     list.add(requirementDef);
518                     moduleRequirements.put(requirementDef.getCapability(), list);
519                 }
520                 // Validating against req/cap of "derived from" node
521                 Boolean validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef.getCapability(), requirementDef.getName());
522                 if (!validateVsParentCap) {
523                     String parentResourceName = parentResource != null ? parentResource.getName() : "";
524                     log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResourceName);
525                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement",
526                         requirementDef.getName().toLowerCase(), parentResourceName);
527                 }
528             }
529             if (moduleRequirements.size() > 0) {
530                 resource.setRequirements(moduleRequirements);
531             }
532         }
533     }
534
535     private RequirementDefinition createRequirementFromImportFile(Object requirementJson) {
536         RequirementDefinition requirement = new RequirementDefinition();
537         if (requirementJson instanceof String) {
538             String requirementJsonString = (String) requirementJson;
539             requirement.setCapability(requirementJsonString);
540         } else if (requirementJson instanceof Map) {
541             Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
542             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) {
543                 requirement.setCapability((String) requirementJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName()));
544             }
545             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) {
546                 requirement.setNode((String) requirementJsonMap.get(ToscaTagNamesEnum.NODE.getElementName()));
547             }
548             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
549                 requirement.setRelationship((String) requirementJsonMap.get(ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
550             }
551             if (requirementJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
552                 List<Object> occurrencesList = (List) requirementJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
553                 validateOccurrences(occurrencesList);
554                 requirement.setMinOccurrences(occurrencesList.get(0).toString());
555                 requirement.setMaxOccurrences(occurrencesList.get(1).toString());
556             }
557         } else {
558             throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
559         }
560         return requirement;
561     }
562
563     private void setProperties(final Map<String, Object> toscaJson, final Resource resource,
564                                final Either<Resource, StorageOperationStatus> existingResource) {
565         final Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
566         ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
567         final Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
568         if (properties.isLeft()) {
569             final Map<String, PropertyDefinition> propertyDefinitionMap = properties.left().value();
570             if (MapUtils.isNotEmpty(propertyDefinitionMap)) {
571                 final List<PropertyDefinition> propertiesList = new ArrayList<>();
572                 for (final Entry<String, PropertyDefinition> entry : propertyDefinitionMap.entrySet()) {
573                     addPropertyToList(resource.getName(), propertiesList, entry);
574                 }
575                 if (existingResource.isLeft()) {
576                     final List<PropertyDefinition> userCreatedResourceProperties =
577                         existingResource.left().value().getProperties().stream()
578                             .filter(PropertyDataDefinition::isUserCreated)
579                             .filter(propertyDefinition -> !propertyDefinitionMap.containsKey(propertyDefinition.getName()))
580                             .collect(Collectors.toList());
581                     if (CollectionUtils.isNotEmpty(userCreatedResourceProperties)) {
582                         propertiesList.addAll(userCreatedResourceProperties);
583                     }
584                 }
585
586                 resource.setProperties(propertiesList);
587             }
588         } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
589             throw new ByActionStatusComponentException(
590                 componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY));
591         }
592     }
593
594     private void addPropertyToList(final String resourceName,
595                                    final List<PropertyDefinition> propertiesList,
596                                    final Entry<String, PropertyDefinition> entry) {
597         final String propertyName = entry.getKey();
598         if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(propertyName).matches()) {
599             log.debug("The property with invalid name {} occured upon import resource {}. ", propertyName, resourceName);
600             throw new ByActionStatusComponentException(
601                 componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY));
602         }
603         final PropertyDefinition propertyDefinition = entry.getValue();
604         propertyDefinition.setName(propertyName);
605         propertiesList.add(propertyDefinition);
606     }
607
608     private void setAttributes(final Map<String, Object> originalToscaJsonMap, final Resource resource) {
609         final Map<String, Object> toscaJsonMap = new HashMap<>(originalToscaJsonMap);
610         ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities");
611         final Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributeEither = ImportUtils.getAttributes(toscaJsonMap);
612         if (getAttributeEither.isRight()) {
613             final ResultStatusEnum resultStatus = getAttributeEither.right().value();
614             if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) {
615                 return;
616             }
617             throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES));
618         }
619         final List<AttributeDefinition> attributeDefinitionList = new ArrayList<>();
620         final Map<String, AttributeDefinition> attributeMap = getAttributeEither.left().value();
621         if (MapUtils.isEmpty(attributeMap)) {
622             return;
623         }
624         for (final Entry<String, AttributeDefinition> entry : attributeMap.entrySet()) {
625             final String name = entry.getKey();
626             if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
627                 log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", name, resource.getName());
628                 throw new ByActionStatusComponentException(
629                     componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, JsonPresentationFields.ATTRIBUTES));
630             }
631             final AttributeDefinition attributeDefinition = entry.getValue();
632             attributeDefinition.setName(name);
633             attributeDefinitionList.add(attributeDefinition);
634         }
635         resource.setAttributes(attributeDefinitionList);
636     }
637
638     private Resource setDerivedFrom(Map<String, Object> toscaJson, Resource resource) {
639         Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils
640             .findFirstToscaStringElement(toscaJson, ToscaTagNamesEnum.DERIVED_FROM);
641         Resource derivedFromResource = null;
642         if (toscaDerivedFromElement.isLeft()) {
643             String derivedFrom = toscaDerivedFromElement.left().value();
644             log.debug("Derived from TOSCA name is {}", derivedFrom);
645             resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom}));
646             Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom,
647                 resource.getModel());
648             if (latestByToscaResourceName.isRight()) {
649                 StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
650                 if (operationStatus == StorageOperationStatus.NOT_FOUND) {
651                     operationStatus = StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND;
652                 }
653                 log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus);
654                 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
655                 BeEcompErrorManager.getInstance().logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom);
656                 throw new ByActionStatusComponentException(convertFromStorageResponse, derivedFrom);
657             }
658             derivedFromResource = latestByToscaResourceName.left().value();
659         }
660         return derivedFromResource;
661     }
662
663     private void setCapabilities(Map<String, Object> toscaJson, Resource resource,
664                                  Resource parentResource) {// Note that parentResource can be null
665         Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils
666             .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.CAPABILITIES);
667         if (toscaCapabilities.isLeft()) {
668             Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
669             Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<>();
670             Iterator<Entry<String, Object>> capabilitiesNameValue = jsonCapabilities.entrySet().iterator();
671             Set<String> capNames = new HashSet<>();
672             // Getting flattened list of capabilities of parent node - cap name
673
674             // to cap type
675             Map<String, String> capName2TypeMap = getCapName2Type(parentResource);
676             while (capabilitiesNameValue.hasNext()) {
677                 Entry<String, Object> capabilityNameValue = capabilitiesNameValue.next();
678                 // Validating that no req/cap duplicates exist in imported YAML
679                 String capNameLowerCase = capabilityNameValue.getKey().toLowerCase();
680                 if (capNames.contains(capNameLowerCase)) {
681                     log.debug("More than one capability with same name {} (case-insensitive) in imported TOSCA file is invalid", capNameLowerCase);
682                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase);
683                 }
684                 capNames.add(capNameLowerCase);
685                 CapabilityDefinition capabilityDef = createCapabilityFromImportFile(capabilityNameValue.getValue());
686                 capabilityDef.setName(capabilityNameValue.getKey());
687                 if (moduleCapabilities.containsKey(capabilityDef.getType())) {
688                     moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef);
689                 } else {
690                     List<CapabilityDefinition> list = new ArrayList<>();
691                     list.add(capabilityDef);
692                     moduleCapabilities.put(capabilityDef.getType(), list);
693                 }
694                 // Validating against req/cap of "derived from" node
695                 Boolean validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef.getType(), capabilityDef.getName());
696                 if (!validateVsParentCap) {
697                     // Here parentResource is for sure not null, so it's
698
699                     // null-safe
700
701                     // Check added to avoid sonar warning
702                     String parentResourceName = parentResource != null ? parentResource.getName() : "";
703                     log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResourceName);
704                     throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability",
705                         capabilityDef.getName().toLowerCase(), parentResourceName);
706                 }
707             }
708             if (moduleCapabilities.size() > 0) {
709                 resource.setCapabilities(moduleCapabilities);
710             }
711         }
712     }
713
714     private Map<String, String> getCapName2Type(Resource parentResource) {
715         Map<String, String> capName2type = new HashMap<>();
716         if (parentResource != null) {
717             Map<String, List<CapabilityDefinition>> capabilities = parentResource.getCapabilities();
718             if (capabilities != null) {
719                 for (List<CapabilityDefinition> capDefinitions : capabilities.values()) {
720                     for (CapabilityDefinition capDefinition : capDefinitions) {
721                         String nameLowerCase = capDefinition.getName().toLowerCase();
722                         if (capName2type.get(nameLowerCase) != null) {
723                             String parentResourceName = parentResource.getName();
724                             log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName,
725                                 nameLowerCase);
726                             BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
727                                 "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name "
728                                     + nameLowerCase, ErrorSeverity.ERROR);
729                             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
730                         }
731                         capName2type.put(nameLowerCase, capDefinition.getType());
732                     }
733                 }
734             }
735         }
736         return capName2type;
737     }
738
739     private Map<String, String> getReqName2Type(Resource parentResource) {
740         Map<String, String> reqName2type = new HashMap<>();
741         if (parentResource != null) {
742             Map<String, List<RequirementDefinition>> requirements = parentResource.getRequirements();
743             if (requirements != null) {
744                 for (List<RequirementDefinition> reqDefinitions : requirements.values()) {
745                     for (RequirementDefinition reqDefinition : reqDefinitions) {
746                         String nameLowerCase = reqDefinition.getName().toLowerCase();
747                         if (reqName2type.get(nameLowerCase) != null) {
748                             String parentResourceName = parentResource.getName();
749                             log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName,
750                                 nameLowerCase);
751                             BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
752                                 "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name "
753                                     + nameLowerCase, ErrorSeverity.ERROR);
754                             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
755                         }
756                         reqName2type.put(nameLowerCase, reqDefinition.getCapability());
757                     }
758                 }
759             }
760         }
761         return reqName2type;
762     }
763
764     private Boolean validateCapNameVsDerived(Map<String, String> parentCapName2Type, String childCapabilityType, String reqCapName) {
765         String capNameLowerCase = reqCapName.toLowerCase();
766         log.trace("Validating capability {} vs parent resource", capNameLowerCase);
767         String parentCapType = parentCapName2Type.get(capNameLowerCase);
768         if (parentCapType != null) {
769             if (childCapabilityType.equals(parentCapType)) {
770                 log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase,
771                     childCapabilityType);
772                 return true;
773             }
774             Either<Boolean, StorageOperationStatus> capabilityTypeDerivedFrom = capabilityTypeOperation
775                 .isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType);
776             if (capabilityTypeDerivedFrom.isRight()) {
777                 log.debug("Couldn't check whether imported resource capability derives from its parent's capability");
778                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom.right().value()));
779             }
780             return capabilityTypeDerivedFrom.left().value();
781         }
782         return true;
783     }
784
785     private CapabilityDefinition createCapabilityFromImportFile(Object capabilityJson) {
786         CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
787         if (capabilityJson instanceof String) {
788             String capabilityJsonString = (String) capabilityJson;
789             capabilityDefinition.setType(capabilityJsonString);
790         } else if (capabilityJson instanceof Map) {
791             Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
792             // Type
793             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) {
794                 capabilityDefinition.setType((String) capabilityJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()));
795             }
796             // ValidSourceTypes
797             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
798                 capabilityDefinition
799                     .setValidSourceTypes((List<String>) capabilityJsonMap.get(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
800             }
801             // ValidSourceTypes
802             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
803                 capabilityDefinition.setDescription((String) capabilityJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName()));
804             }
805             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
806                 List<Object> occurrencesList = (List) capabilityJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
807                 validateOccurrences(occurrencesList);
808                 capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
809                 capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
810             }
811             if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) {
812                 Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
813                 if (propertiesRes.isRight()) {
814                     throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
815                 } else {
816                     propertiesRes.left().value().entrySet().stream().forEach(e -> e.getValue().setName(e.getKey().toLowerCase()));
817                     List<ComponentInstanceProperty> capabilityProperties = propertiesRes.left().value().values().stream()
818                         .map(p -> new ComponentInstanceProperty(p, p.getDefaultValue(), null)).collect(Collectors.toList());
819                     capabilityDefinition.setProperties(capabilityProperties);
820                 }
821             }
822         } else if (!(capabilityJson instanceof List)) {
823             throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
824         }
825         return capabilityDefinition;
826     }
827
828     private void handleImportResourceException(UploadResourceInfo resourceMetaData, User user, boolean isNormative, RuntimeException e) {
829         ResponseFormat responseFormat;
830         ComponentException newException;
831         if (e instanceof ComponentException) {
832             ComponentException componentException = (ComponentException) e;
833             responseFormat = componentException.getResponseFormat();
834             if (responseFormat == null) {
835                 responseFormat = getResponseFormatManager().getResponseFormat(componentException.getActionStatus(), componentException.getParams());
836             }
837             newException = componentException;
838         } else {
839             responseFormat = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR);
840             newException = new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
841         }
842         String payloadName = (resourceMetaData != null) ? resourceMetaData.getPayloadName() : "";
843         BeEcompErrorManager.getInstance().logBeSystemError("Import Resource " + payloadName);
844         log.debug("Error when importing resource from payload:{} Exception text: {}", payloadName, e.getMessage(), e);
845         auditErrorImport(resourceMetaData, user, responseFormat, isNormative);
846         throw newException;
847     }
848
849     private void auditErrorImport(UploadResourceInfo resourceMetaData, User user, ResponseFormat errorResponseWrapper, boolean isNormative) {
850         String version, lifeCycleState;
851         if (isNormative) {
852             version = TypeUtils.getFirstCertifiedVersionVersion();
853             lifeCycleState = LifecycleStateEnum.CERTIFIED.name();
854         } else {
855             version = "";
856             lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
857         }
858         String message = "";
859         if (errorResponseWrapper.getMessageId() != null) {
860             message = errorResponseWrapper.getMessageId() + ": ";
861         }
862         message += errorResponseWrapper.getFormattedMessage();
863         AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
864             CommonAuditData.newBuilder().status(errorResponseWrapper.getStatus()).description(message).requestId(ThreadLocalsHolder.getUuid())
865                 .build(), new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()),
866             ResourceVersionInfo.newBuilder().state(lifeCycleState).version(version).build(),
867             ResourceVersionInfo.newBuilder().state("").version("").build(), "", user, "");
868         getAuditingManager().auditEvent(factory);
869     }
870
871     private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) {
872         this.populateResourceMetadata(resourceMetaData, resource);
873         resource.setCreatorUserId(resourceMetaData.getContactId());
874         final String payloadData = resourceMetaData.getPayloadData();
875         if (payloadData != null) {
876             resource.setToscaVersion(getToscaVersion(payloadData));
877         }
878         final List<CategoryDefinition> categories = resourceMetaData.getCategories();
879         calculateResourceIsAbstract(resource, categories);
880     }
881
882     private Map<String, Object> decodePayload(final String payloadData) {
883         final String decodedPayload = new String(Base64.decodeBase64(payloadData));
884         return (Map<String, Object>) new Yaml().load(decodedPayload);
885     }
886
887     private String getToscaVersion(final String payloadData) {
888         final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData);
889         final Either<String, ResultStatusEnum> findFirstToscaStringElement = ImportUtils
890             .findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.TOSCA_VERSION);
891         if (findFirstToscaStringElement.isLeft()) {
892             return findFirstToscaStringElement.left().value();
893         } else {
894             return null;
895         }
896     }
897
898     private void calculateResourceIsAbstract(Resource resource, List<CategoryDefinition> categories) {
899         if (categories != null && !categories.isEmpty()) {
900             CategoryDefinition categoryDef = categories.get(0);
901             resource.setAbstract(false);
902             if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName().equals(Constants.ABSTRACT_CATEGORY_NAME)) {
903                 SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0);
904                 if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) {
905                     resource.setAbstract(true);
906                 }
907             }
908         }
909     }
910
911     private void setConstantMetaData(Resource resource, boolean shouldBeCertified) {
912         String version;
913         LifecycleStateEnum state;
914         if (shouldBeCertified) {
915             version = TypeUtils.getFirstCertifiedVersionVersion();
916             state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE;
917         } else {
918             version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION;
919             state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT;
920         }
921         resource.setVersion(version);
922         resource.setLifecycleState(state);
923         resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
924         resource.setVendorName(ImportUtils.Constants.VENDOR_NAME);
925         resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE);
926     }
927
928     private void validateOccurrences(List<Object> occurrensesList) {
929         if (!ValidationUtils.validateListNotEmpty(occurrensesList)) {
930             log.debug("Occurrenses list empty");
931             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
932         }
933         if (occurrensesList.size() < 2) {
934             log.debug("Occurrenses list size not 2");
935             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
936         }
937         Object minObj = occurrensesList.get(0);
938         Object maxObj = occurrensesList.get(1);
939         Integer minOccurrences;
940         Integer maxOccurrences;
941         if (minObj instanceof Integer) {
942             minOccurrences = (Integer) minObj;
943         } else {
944             log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj);
945             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
946         }
947         if (minOccurrences < 0) {
948             log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences);
949             throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
950         }
951         if (maxObj instanceof String) {
952             if (!"UNBOUNDED".equals(maxObj)) {
953                 log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
954                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
955             }
956         } else {
957             if (maxObj instanceof Integer) {
958                 maxOccurrences = (Integer) maxObj;
959             } else {
960                 log.debug("Invalid occurrenses format.  Max occurrence is {}", maxObj);
961                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
962             }
963             if (maxOccurrences < 0 || maxOccurrences < minOccurrences) {
964                 log.debug("Invalid occurrenses format.  min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
965                 throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
966             }
967         }
968     }
969
970     public synchronized void init(ServletContext servletContext) {
971         if (this.servletContext == null) {
972             this.servletContext = servletContext;
973             responseFormatManager = ResponseFormatManager.getInstance();
974             resourceBusinessLogic = getResourceBL(servletContext);
975         }
976     }
977
978     public boolean isResourceExist(String resourceName) {
979         return resourceBusinessLogic.isResourceExist(resourceName);
980     }
981
982     private ResourceBusinessLogic getResourceBL(ServletContext context) {
983         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context
984             .getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
985         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
986         return webApplicationContext.getBean(ResourceBusinessLogic.class);
987     }
988
989     public ServletContext getServletContext() {
990         return servletContext;
991     }
992
993     public void setServletContext(ServletContext servletContext) {
994         this.servletContext = servletContext;
995     }
996
997     public AuditingManager getAuditingManager() {
998         return auditingManager;
999     }
1000
1001     @Autowired
1002     public void setAuditingManager(AuditingManager auditingManager) {
1003         this.auditingManager = auditingManager;
1004     }
1005
1006     public ResponseFormatManager getResponseFormatManager() {
1007         return responseFormatManager;
1008     }
1009
1010     public void setResponseFormatManager(ResponseFormatManager responseFormatManager) {
1011         this.responseFormatManager = responseFormatManager;
1012     }
1013
1014     public ResourceBusinessLogic getResourceBusinessLogic() {
1015         return resourceBusinessLogic;
1016     }
1017
1018     @Autowired
1019     public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) {
1020         this.resourceBusinessLogic = resourceBusinessLogic;
1021     }
1022
1023     public IGraphLockOperation getGraphLockOperation() {
1024         return graphLockOperation;
1025     }
1026
1027     @Autowired
1028     public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
1029         this.graphLockOperation = graphLockOperation;
1030     }
1031
1032     private List<DataTypeDefinition> extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic,
1033                                                              final Map<String, Object> foundElements,
1034                                                              final String model) {
1035         final List<DataTypeDefinition> dataTypeDefinitionList = new ArrayList<>();
1036         if (MapUtils.isNotEmpty(foundElements)) {
1037             final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll =
1038                 resourceBusinessLogic.applicationDataTypeCache.getAll(model);
1039             if (dataTypeCacheAll.isLeft()) {
1040                 for (final Entry<String, Object> attributeNameValue : foundElements.entrySet()) {
1041                     final Object value = attributeNameValue.getValue();
1042                     if (value instanceof Map) {
1043                         final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue);
1044                         final DataTypeDefinition dataTypeDefinitionParent = dataTypeCacheAll.left().value()
1045                             .get(dataTypeDefinition.getDerivedFromName());
1046                         dataTypeDefinition.setDerivedFrom(dataTypeDefinitionParent);
1047                         dataTypeDefinitionList.add(dataTypeDefinition);
1048                     } else {
1049                         dataTypeDefinitionList.add(createDataType(String.valueOf(value)));
1050                     }
1051                 }
1052             }
1053         }
1054         return dataTypeDefinitionList;
1055     }
1056 }