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