857c5e32b7abff649d1471e28ed198f2406407c3
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ServiceImportBusinessLogic.java
1 /*
2  * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.be.components.impl;
17
18 import static java.util.stream.Collectors.joining;
19 import static java.util.stream.Collectors.toList;
20 import static java.util.stream.Collectors.toMap;
21 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
22 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement;
23 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
24 import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN;
25
26 import fj.data.Either;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.EnumMap;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.Set;
38 import java.util.regex.Pattern;
39
40 import lombok.Getter;
41 import lombok.Setter;
42 import org.apache.commons.codec.binary.Base64;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.collections.MapUtils;
45 import org.apache.commons.collections4.ListUtils;
46 import org.apache.commons.lang3.tuple.ImmutablePair;
47 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
48 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
49 import org.openecomp.sdc.be.components.csar.CsarInfo;
50 import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine;
51 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
52 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
53 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
54 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
55 import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils;
56 import org.openecomp.sdc.be.components.impl.utils.CreateServiceFromYamlParameter;
57 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
58 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
59 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
60 import org.openecomp.sdc.be.components.path.ForwardingPathValidator;
61 import org.openecomp.sdc.be.components.validation.NodeFilterValidator;
62 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
63 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
64 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
65 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
66 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
67 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
68 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
69 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
70 import org.openecomp.sdc.be.config.BeEcompErrorManager;
71 import org.openecomp.sdc.be.config.ConfigurationManager;
72 import org.openecomp.sdc.be.dao.api.ActionStatus;
73 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
74 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
75 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
76 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
77 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
78 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
79 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
80 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
81 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
82 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
83 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
84 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
85 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
86 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
87 import org.openecomp.sdc.be.datatypes.enums.CreatedFrom;
88 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
89 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
90 import org.openecomp.sdc.be.impl.ComponentsUtils;
91 import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
92 import org.openecomp.sdc.be.model.ArtifactDefinition;
93 import org.openecomp.sdc.be.model.CapabilityDefinition;
94 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
95 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
96 import org.openecomp.sdc.be.model.Component;
97 import org.openecomp.sdc.be.model.ComponentInstance;
98 import org.openecomp.sdc.be.model.ComponentInstanceInput;
99 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
100 import org.openecomp.sdc.be.model.ComponentParametersView;
101 import org.openecomp.sdc.be.model.DataTypeDefinition;
102 import org.openecomp.sdc.be.model.DistributionStatusEnum;
103 import org.openecomp.sdc.be.model.GroupDefinition;
104 import org.openecomp.sdc.be.model.InputDefinition;
105 import org.openecomp.sdc.be.model.InterfaceDefinition;
106 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
107 import org.openecomp.sdc.be.model.LifecycleStateEnum;
108 import org.openecomp.sdc.be.model.NodeTypeInfo;
109 import org.openecomp.sdc.be.model.Operation;
110 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
111 import org.openecomp.sdc.be.model.PropertyDefinition;
112 import org.openecomp.sdc.be.model.RelationshipImpl;
113 import org.openecomp.sdc.be.model.RelationshipInfo;
114 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
115 import org.openecomp.sdc.be.model.RequirementDefinition;
116 import org.openecomp.sdc.be.model.Resource;
117 import org.openecomp.sdc.be.model.Service;
118 import org.openecomp.sdc.be.model.UploadCapInfo;
119 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
120 import org.openecomp.sdc.be.model.UploadInfo;
121 import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
122 import org.openecomp.sdc.be.model.UploadPropInfo;
123 import org.openecomp.sdc.be.model.UploadReqInfo;
124 import org.openecomp.sdc.be.model.UploadResourceInfo;
125 import org.openecomp.sdc.be.model.User;
126 import org.openecomp.sdc.be.model.category.CategoryDefinition;
127 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
128 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
129 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
130 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
131 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation;
132 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
133 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
134 import org.openecomp.sdc.be.model.operations.StorageException;
135 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
136 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
137 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
138 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
139 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
140 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
141 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
142 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
143 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
144 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
145 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
146 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
147 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
148 import org.openecomp.sdc.be.tosca.CsarUtils;
149 import org.openecomp.sdc.be.utils.CommonBeUtils;
150 import org.openecomp.sdc.be.utils.TypeUtils;
151 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
152 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
153 import org.openecomp.sdc.common.api.Constants;
154 import org.openecomp.sdc.common.datastructure.Wrapper;
155 import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
156 import org.openecomp.sdc.common.log.wrappers.Logger;
157 import org.openecomp.sdc.common.util.GeneralUtility;
158 import org.openecomp.sdc.common.util.ValidationUtils;
159 import org.openecomp.sdc.exception.ResponseFormat;
160 import org.springframework.beans.factory.annotation.Autowired;
161 import org.yaml.snakeyaml.DumperOptions;
162 import org.yaml.snakeyaml.Yaml;
163
164 @Getter
165 @Setter
166 @org.springframework.stereotype.Component("serviceImportBusinessLogic")
167 public class ServiceImportBusinessLogic{
168
169     private final UiComponentDataConverter uiComponentDataConverter;
170     private static final String INITIAL_VERSION = "0.1";
171     private static final String CREATE_RESOURCE = "Create Resource";
172     private static final String IN_RESOURCE = "  in resource {} ";
173     private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name ";
174     private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {}  in resource {} ";
175     private static final String CERTIFICATION_ON_IMPORT = "certification on import";
176     private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update";
177     private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes";
178     private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate";
179     private static final String CATEGORY_IS_EMPTY = "Resource category is empty";
180
181
182     @Autowired
183     private ServiceBusinessLogic serviceBusinessLogic;
184     public ServiceBusinessLogic getServiceBusinessLogic() {
185         return serviceBusinessLogic;
186     }
187
188     public void setServiceBusinessLogic(
189         ServiceBusinessLogic serviceBusinessLogic) {
190         this.serviceBusinessLogic = serviceBusinessLogic;
191     }
192
193     @Autowired
194     private CsarBusinessLogic csarBusinessLogic;
195     @Autowired
196     protected ComponentsUtils componentsUtils;
197     @Autowired
198     protected ToscaOperationFacade toscaOperationFacade;
199     @Autowired
200     private CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
201     @Autowired
202     private LifecycleBusinessLogic lifecycleBusinessLogic;
203     @Autowired
204     private CompositionBusinessLogic compositionBusinessLogic;
205     @Autowired
206     private ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
207     @Autowired
208     private ServiceImportParseLogic serviceImportParseLogic;
209
210     private static final Logger log = Logger.getLogger(ServiceImportBusinessLogic.class);
211     @Autowired
212     public ServiceImportBusinessLogic(IElementOperation elementDao,
213         IGroupOperation groupOperation,
214         IGroupInstanceOperation groupInstanceOperation,
215         IGroupTypeOperation groupTypeOperation,
216         GroupBusinessLogic groupBusinessLogic,
217         InterfaceOperation interfaceOperation,
218         InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
219         ArtifactsBusinessLogic artifactsBusinessLogic,
220         IDistributionEngine distributionEngine, ComponentInstanceBusinessLogic componentInstanceBusinessLogic,
221         ServiceDistributionValidation serviceDistributionValidation, ForwardingPathValidator forwardingPathValidator,
222         UiComponentDataConverter uiComponentDataConverter, NodeFilterOperation serviceFilterOperation,
223         NodeFilterValidator serviceFilterValidator, ArtifactsOperations artifactToscaOperation,
224         ComponentContactIdValidator componentContactIdValidator,
225         ComponentNameValidator componentNameValidator,
226         ComponentTagsValidator componentTagsValidator,
227         ComponentValidator componentValidator,
228         ComponentIconValidator componentIconValidator,
229         ComponentProjectCodeValidator componentProjectCodeValidator,
230         ComponentDescriptionValidator componentDescriptionValidator) {
231         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
232         this.uiComponentDataConverter = uiComponentDataConverter;
233     }
234
235     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
236
237     public Service createService(Service service, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload, String payloadName) {
238         log.debug("enter createService");
239         service.setCreatorUserId(user.getUserId());
240         service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
241         service.setVersion(INITIAL_VERSION);
242         service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel());
243         service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
244         service.setInstantiationType("A-la-carte");
245         service.setEnvironmentContext("General_Revenue-Bearing");
246         service.setEcompGeneratedNaming(true);
247
248         try {
249             serviceBusinessLogic.validateServiceBeforeCreate(service, user, auditingAction);
250
251             log.debug("enter createService,validateServiceBeforeCreate success");
252             String csarUUID = payloadName == null ? service.getCsarUUID() : payloadName;
253
254             log.debug("enter createService,get csarUUID:{}", csarUUID);
255             csarBusinessLogic.validateCsarBeforeCreate(service, csarUUID);
256             log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID);
257             return createServiceFromCsar(service, user, csarUIPayload, csarUUID);
258         }catch (Exception e){
259             log.debug("Exception occured when createService,error is:{}",e.getMessage(),e);
260             //ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
261             throw new ComponentException(ActionStatus.GENERAL_ERROR);
262         }
263     }
264
265     protected Service createServiceFromCsar(Service service, User user, Map<String, byte[]> csarUIPayload, String csarUUID) {
266         log.trace("************* created successfully from YAML, resource TOSCA ");
267         try {
268             CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID);
269
270             Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo();
271             Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic
272                     .findNodeTypesArtifactsToHandle(
273                             nodeTypesInfo, csarInfo, service);
274             if (findNodeTypesArtifactsToHandleRes.isRight()) {
275                 log.debug("failed to find node types for update with artifacts during import csar {}. ",
276                         csarInfo.getCsarUUID());
277                 throw new ComponentException(findNodeTypesArtifactsToHandleRes.right().value());
278             }
279             Service cService = createServiceFromYaml(service, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(),
280                     nodeTypesInfo, csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false,
281                     null);
282             return cService;
283         }catch (Exception e){
284             log.debug("Exception occured when createServiceFromCsar,error is:{}",e.getMessage(),e);
285             throw new ComponentException(ActionStatus.GENERAL_ERROR);
286         }
287     }
288
289     protected Service createServiceFromYaml(Service service, String topologyTemplateYaml,
290                                           String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
291                                           Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
292                                           boolean shouldLock, boolean inTransaction, String nodeName) {
293
294         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
295         Service createdService;
296         CreateServiceFromYamlParameter csfyp = new CreateServiceFromYamlParameter();
297         try {
298             ParsedToscaYamlInfo
299                     parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, service);
300             if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances())) {
301                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
302             }
303             log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", service.getName());
304             csfyp.setYamlName(yamlName);
305             csfyp.setParsedToscaYamlInfo(parsedToscaYamlInfo);
306             csfyp.setCreatedArtifacts(createdArtifacts);
307             csfyp.setTopologyTemplateYaml(topologyTemplateYaml);
308             csfyp.setNodeTypesInfo(nodeTypesInfo);
309             csfyp.setCsarInfo(csarInfo);
310             csfyp.setNodeName(nodeName);
311
312             createdService = createServiceAndRIsFromYaml(service, false, nodeTypesArtifactsToCreate, shouldLock, inTransaction, csfyp);
313             log.debug("#createResourceFromYaml - The resource {} has been created ", service.getName());
314         } catch (ComponentException e) {
315             log.debug("create Service From Yaml failed,get ComponentException:{}", e);
316             throw e;
317         } catch (StorageException e) {
318             log.debug("create Service From Yaml failed,get StorageException:{}", e);
319             throw e;
320         }
321         return createdService;
322     }
323
324     protected Service createServiceAndRIsFromYaml(Service service, boolean isNormative,
325                                                 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
326                                                 boolean shouldLock, boolean inTransaction, CreateServiceFromYamlParameter csfyp) {
327
328         List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
329         String yamlName = csfyp.getYamlName();
330         ParsedToscaYamlInfo parsedToscaYamlInfo = csfyp.getParsedToscaYamlInfo();
331         List<ArtifactDefinition> createdArtifacts = csfyp.getCreatedArtifacts();
332         String topologyTemplateYaml = csfyp.getTopologyTemplateYaml();
333         Map<String, NodeTypeInfo> nodeTypesInfo = csfyp.getNodeTypesInfo();
334         CsarInfo csarInfo = csfyp.getCsarInfo();
335         String nodeName = csfyp.getNodeName();
336
337         if (shouldLock) {
338             Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic.lockComponentByName(service.getSystemName(), service, CREATE_RESOURCE);
339             if (lockResult.isRight()) {
340                 serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
341                 throw new ComponentException(lockResult.right().value());
342             }
343             log.debug("name is locked {} status = {}", service.getSystemName(), lockResult);
344         }
345         try {
346             log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
347             service = serviceImportParseLogic.createServiceTransaction(service, csarInfo.getModifier(), isNormative);
348             log.trace("************* Going to add inputs from yaml {}", yamlName);
349
350             Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
351             service = serviceImportParseLogic.createInputsOnService(service, inputs);
352             log.trace("************* Finish to add inputs from yaml {}", yamlName);
353
354             Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo
355                     .getInstances();
356             log.trace("************* Going to create nodes, RI's and Relations  from yaml {}", yamlName);
357
358             service = createRIAndRelationsFromYaml(yamlName, service, uploadComponentInstanceInfoMap,
359                     topologyTemplateYaml, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
360                     nodeTypesArtifactsToCreate, nodeName);
361             log.trace("************* Finished to create nodes, RI and Relation  from yaml {}", yamlName);
362             Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic
363                     .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), service.getSystemName());
364             if (validateUpdateVfGroupNamesRes.isRight()) {
365                 serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
366                 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
367             }
368             Map<String, GroupDefinition> groups;
369             log.trace("************* Going to add groups from yaml {}", yamlName);
370
371             if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
372                 groups = validateUpdateVfGroupNamesRes.left().value();
373             } else {
374                 groups = parsedToscaYamlInfo.getGroups();
375             }
376
377             Either<Service, ResponseFormat> createGroupsOnResource = createGroupsOnResource(service, groups);
378             if (createGroupsOnResource.isRight()) {
379                 serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
380                 throw new ComponentException(createGroupsOnResource.right().value());
381             }
382             service = createGroupsOnResource.left().value();
383             log.trace("************* Going to add artifacts from yaml {}", yamlName);
384
385             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,
386                     nodeTypesArtifactsToCreate);
387
388             Either<Service, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(
389                     ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, createdArtifacts, yamlName,
390                     csarInfo, service, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
391             if (createArtifactsEither.isRight()) {
392                 serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
393                 throw new ComponentException(createArtifactsEither.right().value());
394             }
395             service = serviceImportParseLogic.getServiceWithGroups(createArtifactsEither.left().value().getUniqueId());
396             ASDCKpiApi.countCreatedResourcesKPI();
397             return service;
398         } catch (ComponentException | StorageException e) {
399             serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
400             throw e;
401         } finally {
402             if (!inTransaction) {
403                 serviceBusinessLogic.janusGraphDao.commit();
404             }
405             if (shouldLock) {
406                 serviceBusinessLogic.graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(),
407                         NodeTypeEnum.Resource);
408             }
409         }
410     }
411
412     protected Either<Resource, ResponseFormat> createOrUpdateArtifacts(
413             ArtifactsBusinessLogic.ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
414             String yamlFileName, CsarInfo csarInfo, Resource preparedResource,
415             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, boolean inTransaction, boolean shouldLock) {
416
417         String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
418         Resource resource = preparedResource;
419
420         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
421                 .getNodeTypesArtifactsToHandle();
422         if (preparedResource.getResourceType() == ResourceTypeEnum.VF) {
423             if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
424                 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes =
425                         handleNodeTypeArtifacts(preparedResource, nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true);
426                 if (handleNodeTypeArtifactsRes.isRight()) {
427                     return Either.right(handleNodeTypeArtifactsRes.right().value());
428                 }
429             }
430         } else {
431             Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts,
432                     new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
433             log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
434             if (createdCsarArtifactsEither.isRight()) {
435                 return createdCsarArtifactsEither;
436             }
437             resource = createdCsarArtifactsEither.left().value();
438         }
439         return Either.left(resource);
440     }
441
442     protected Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo,
443                                                                    List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock,
444                                                                    boolean inTransaction) {
445         if (csarInfo.getCsar() != null) {
446             createOrUpdateSingleNonMetaArtifactToComstants(resource, csarInfo, artifactOperation, shouldLock, inTransaction);
447
448             Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource,
449                     createdArtifacts, shouldLock, inTransaction, artifactOperation);
450             if (eitherCreateResult.isRight()) {
451                 return Either.right(eitherCreateResult.right().value());
452             }
453             Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
454             if (eitherGerResource.isRight()) {
455                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
456                         componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource);
457                 return Either.right(responseFormat);
458             }
459             resource = eitherGerResource.left().value();
460             Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
461
462             if (artifacsMetaCsarStatus.isLeft()) {
463                 return getResourceResponseFormatEither(resource, csarInfo, createdArtifacts, artifactOperation, shouldLock, inTransaction, artifacsMetaCsarStatus);
464             } else {
465                 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction);
466             }
467         }
468         return Either.left(resource);
469     }
470
471     protected void createOrUpdateSingleNonMetaArtifactToComstants(Resource resource, CsarInfo csarInfo, ArtifactOperationInfo artifactOperation, boolean shouldLock, boolean inTransaction) {
472         String vendorLicenseModelId = null;
473         String vfLicenseModelId = null;
474         if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.UPDATE) {
475             Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts();
476             if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
477                 for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
478                     if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
479                         vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
480                     }
481                     if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
482                         vfLicenseModelId = artifactEntry.getValue().getUniqueId();
483                     }
484                 }
485             }
486         }
487         createOrUpdateSingleNonMetaArtifact(resource, csarInfo,
488                 CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL, Constants.VENDOR_LICENSE_MODEL,
489                 ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
490                 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME,
491                 Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId, artifactOperation, null, true, shouldLock,
492                 inTransaction);
493         createOrUpdateSingleNonMetaArtifact(resource, csarInfo,
494                 CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
495                 ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
496                 Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId,
497                 artifactOperation, null, true, shouldLock, inTransaction);
498     }
499
500     protected Either<Resource, ResponseFormat> getResourceResponseFormatEither(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock, boolean inTransaction, Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus) {
501         try {
502             String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
503             String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
504             Either<Resource, ResponseFormat> createArtifactsFromCsar;
505             if (ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
506                 createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
507                     .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName,
508                         createdArtifacts);
509             } else {
510                 Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
511                     .updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName,
512                         createdArtifacts, shouldLock, inTransaction);
513                 if ((result.left().value() instanceof Resource) && result.isLeft()) {
514                     Resource service1 = (Resource) result.left().value();
515                     createArtifactsFromCsar = Either.left(service1);
516                 } else {
517                     createArtifactsFromCsar = Either.right(result.right().value());
518                 }
519             }
520             if (createArtifactsFromCsar.isRight()) {
521                 log.debug("Couldn't create artifacts from artifacts.meta");
522                 return Either.right(createArtifactsFromCsar.right().value());
523             }
524             return Either.left(createArtifactsFromCsar.left().value());
525         }catch(Exception e)
526         {
527             log.debug("Exception occured in getResourceResponseFormatEither, message:{}", e.getMessage(), e);
528             return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
529         }
530     }
531
532     protected Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource,
533                                                                             List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction,
534                                                                             ArtifactOperationInfo artifactOperation) {
535         Either<Resource, ResponseFormat> resStatus = null;
536         Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
537         try {
538             Either<List<CsarUtils.NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
539             if (artifactPathAndNameList.isRight()) {
540                 return Either.right(getComponentsUtils().getResponseFormatByArtifactId(
541                         ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value()));
542             }
543             EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
544
545             if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
546                 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class);
547                 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
548             } else {
549                 Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
550                         resource, artifactPathAndNameList.left().value(), csarInfo.getModifier());
551                 if (findVfCsarArtifactsToHandleRes.isRight()) {
552                     resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
553                 }
554                 if (resStatus == null) {
555                     vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
556                 }
557             }
558             if (resStatus == null && vfCsarArtifactsToHandle != null) {
559                 resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle);
560             }
561             if (resStatus == null) {
562                 resStatus = Either.left(resource);
563             }
564         } catch (Exception e) {
565             resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
566             log.debug("Exception occured in createNonMetaArtifacts, message:{}", e.getMessage(), e);
567         } finally {
568             CsarUtils.handleWarningMessages(collectedWarningMessages);
569         }
570         return resStatus;
571     }
572
573     protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(
574             Resource resource, List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, User user) {
575
576         List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
577         if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) {
578             existingArtifacts.addAll(resource.getDeploymentArtifacts().values());
579         }
580         if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) {
581             existingArtifacts.addAll(resource.getArtifacts().values());
582         }
583         existingArtifacts = existingArtifacts.stream()
584                 .filter(this::isNonMetaArtifact).collect(toList());
585
586         List<String> artifactsToIgnore = new ArrayList<>();
587         if (resource.getGroups() != null) {
588             resource.getGroups().stream().forEach(g -> {
589                 if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
590                     artifactsToIgnore.addAll(g.getArtifacts());
591                 }
592             });
593         }
594         existingArtifacts = existingArtifacts.stream()
595                 .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
596         return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user);
597     }
598
599     protected boolean isNonMetaArtifact(ArtifactDefinition artifact) {
600         boolean result = true;
601         if (artifact.getMandatory() || artifact.getArtifactName() == null || !isValidArtifactType(artifact)) {
602             result = false;
603         }
604         return result;
605     }
606
607     private boolean isValidArtifactType(ArtifactDefinition artifact) {
608         boolean result = true;
609         if (artifact.getArtifactType() == null
610                 || ArtifactTypeEnum.findType(artifact.getArtifactType()).equals(ArtifactTypeEnum.VENDOR_LICENSE)
611                 || ArtifactTypeEnum.findType(artifact.getArtifactType()).equals(ArtifactTypeEnum.VF_LICENSE)) {
612             result = false;
613         }
614         return result;
615     }
616
617     protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
618             List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle,
619             Resource resource, User user) {
620
621         EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(
622                 ArtifactsBusinessLogic.ArtifactOperationEnum.class);
623         Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
624         Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
625                 .left(nodeTypeArtifactsToHandle);
626         try {
627             // add all found Csar artifacts to list to upload
628             List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
629             List<CsarUtils.NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
630             List<CsarUtils.NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
631             for (CsarUtils.NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
632                 ArtifactDefinition foundArtifact;
633                 if (!existingArtifactsToHandle.isEmpty()) {
634                     foundArtifact = existingArtifactsToHandle.stream()
635                             .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst()
636                             .orElse(null);
637                     if (foundArtifact != null) {
638                         if (ArtifactTypeEnum.findType(foundArtifact.getArtifactType()).equals(currNewArtifact
639                                 .getArtifactType())) {
640                             if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
641                                 currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
642                                 artifactsToUpdate.add(currNewArtifact);
643                             }
644                             existingArtifactsToHandle.remove(foundArtifact);
645                             artifactsToUpload.remove(currNewArtifact);
646                         } else {
647                             log.debug("Can't upload two artifact with the same name {}.",
648                                     currNewArtifact.getArtifactName());
649                             ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat(
650                                     ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR,
651                                     currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(),
652                                     foundArtifact.getArtifactType());
653                             AuditingActionEnum auditingAction = serviceBusinessLogic.artifactsBusinessLogic
654                                     .detectAuditingType(new ArtifactOperationInfo(false, false,
655                                             ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE), foundArtifact.getArtifactChecksum());
656                             serviceBusinessLogic.artifactsBusinessLogic.handleAuditing(auditingAction, resource, resource.getUniqueId(),
657                                     user, null, null, foundArtifact.getUniqueId(), responseFormat,
658                                     resource.getComponentType(), null);
659                             responseWrapper.setInnerElement(responseFormat);
660                             break;
661                         }
662                     }
663                 }
664             }
665             if (responseWrapper.isEmpty()) {
666                 for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
667                     if (currArtifact.getIsFromCsar()) {
668                         artifactsToDelete.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
669                     } else {
670                         artifactsToUpdate.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
671
672                     }
673                 }
674             }
675             if (responseWrapper.isEmpty()) {
676                 if (!artifactsToUpload.isEmpty()) {
677                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload);
678                 }
679                 if (!artifactsToUpdate.isEmpty()) {
680                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate);
681                 }
682                 if (!artifactsToDelete.isEmpty()) {
683                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete);
684                 }
685             }
686             if (!responseWrapper.isEmpty()) {
687                 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
688             }
689         } catch (Exception e) {
690             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
691             responseWrapper.setInnerElement(responseFormat);
692             log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
693             nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
694         }
695         return nodeTypeArtifactsToHandleRes;
696     }
697
698     protected Either<Resource, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Resource resource, List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, Either<Resource, ResponseFormat> resStatus, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
699         for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle
700                 .entrySet()) {
701             Optional<ResponseFormat> optionalCreateInDBError =
702                     currArtifactOperationPair.getValue().stream()
703                             .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(),
704                                     e.getArtifactName(), e.getArtifactType(),
705                                     e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(),
706                                     CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(),
707                                     new ArtifactOperationInfo(false, false,
708                                             currArtifactOperationPair.getKey()),
709                                     createdArtifacts, e.isFromCsar(), shouldLock, inTransaction))
710                             .filter(Either::isRight).
711                             map(e -> e.right().value()).
712                             findAny();
713             if (optionalCreateInDBError.isPresent()) {
714                 resStatus = Either.right(optionalCreateInDBError.get());
715                 break;
716             }
717         }
718         return resStatus;
719     }
720
721     protected Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo,
722                                                                                 String artifactPath, String artifactFileName, String artifactType, ArtifactGroupTypeEnum artifactGroupType,
723                                                                                 String artifactLabel, String artifactDisplayName, String artifactDescription, String artifactId,
724                                                                                 ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts, boolean isFromCsar, boolean shouldLock,
725                                                                                 boolean inTransaction) {
726         byte[] artifactFileBytes = null;
727
728         if (csarInfo.getCsar().containsKey(artifactPath)) {
729             artifactFileBytes = csarInfo.getCsar().get(artifactPath);
730         }
731         Either<Boolean, ResponseFormat> result = Either.left(true);
732         if (operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE || operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE) {
733             if (serviceImportParseLogic.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
734                 Either<ArtifactDefinition, ResponseFormat> handleDelete = serviceBusinessLogic.artifactsBusinessLogic.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource,
735                         shouldLock, inTransaction);
736                 if (handleDelete.isRight()) {
737                     result = Either.right(handleDelete.right().value());
738                 }
739                 return result;
740             }
741             if (org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
742                 operation = new ArtifactOperationInfo(false, false,
743                         ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE);
744             }
745         }
746         if (artifactFileBytes != null) {
747             Map<String, Object> vendorLicenseModelJson = ArtifactUtils
748                     .buildJsonForUpdateArtifact(artifactId, artifactFileName,
749                             artifactType, artifactGroupType, artifactLabel, artifactDisplayName, artifactDescription,
750                             artifactFileBytes, null, isFromCsar);
751             Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic.createOrUpdateCsarArtifactFromJson(
752                     resource, csarInfo.getModifier(), vendorLicenseModelJson, operation);
753             serviceImportParseLogic
754                     .addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
755             if (eitherNonMetaArtifacts.isRight()) {
756                 BeEcompErrorManager.getInstance()
757                         .logInternalFlowError("UploadLicenseArtifact", "Failed to upload license artifact: "
758                                         + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(),
759                                 BeEcompErrorManager.ErrorSeverity.WARNING);
760                 return Either.right(eitherNonMetaArtifacts.right().value());
761             }
762         }
763         return result;
764     }
765
766     public Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource,
767                                                                                     Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
768                                                                                     List<ArtifactDefinition> createdArtifacts, User user, boolean inTransaction, boolean ignoreLifecycleState) {
769         List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes;
770         Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null;
771         Either<Resource, ResponseFormat> changeStateResponse;
772         try {
773             changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction);
774             if (changeStateResponse.isRight()) {
775                 return Either.right(changeStateResponse.right().value());
776             }
777             nodeTypeResource = changeStateResponse.left().value();
778
779             List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>();
780             log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName());
781             for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle
782                     .entrySet()) {
783                 ArtifactsBusinessLogic.ArtifactOperationEnum curOperation = curOperationEntry.getKey();
784                 List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue();
785                 if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) {
786                     log.debug("************* Going to {} artifact to vfc {}", curOperation.name(),
787                             nodeTypeResource.getName());
788                     handleNodeTypeArtifactsRequestRes = serviceBusinessLogic.artifactsBusinessLogic
789                             .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user,
790                                     createdArtifacts, new ArtifactOperationInfo(false,
791                                             ignoreLifecycleState, curOperation),
792                                     false, inTransaction);
793                     if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(curOperation)) {
794                         createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
795                     }
796                     handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
797                 }
798             }
799             if (handleNodeTypeArtifactsRes == null) {
800                 handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts);
801             }
802         } catch (Exception e) {
803             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
804             handleNodeTypeArtifactsRes = Either.right(responseFormat);
805             log.debug("Exception occured when handleVfcArtifacts, error is:{}", e.getMessage(), e);
806         }
807         return handleNodeTypeArtifactsRes;
808     }
809
810     protected Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) {
811         Either<Resource, ResponseFormat> checkoutResourceRes;
812         try {
813             if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState()
814                     .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
815                 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic.changeComponentState(
816                         resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT,
817                         new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
818                                 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR),
819                         inTransaction, true);
820                 if (checkoutRes.isRight()) {
821                     checkoutResourceRes = Either.right(checkoutRes.right().value());
822                 } else {
823                     checkoutResourceRes = Either.left((Resource) checkoutRes.left().value());
824                 }
825             } else {
826                 checkoutResourceRes = Either.left(resource);
827             }
828         } catch (Exception e) {
829             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
830             checkoutResourceRes = Either.right(responseFormat);
831             log.debug("Exception occured when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(),
832                     e);
833         }
834         return checkoutResourceRes;
835     }
836
837     protected Either<Service, ResponseFormat> createOrUpdateArtifacts(
838             ArtifactsBusinessLogic.ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
839             String yamlFileName, CsarInfo csarInfo, Service preparedService,
840             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, boolean inTransaction, boolean shouldLock) {
841
842         String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
843         Service resource = preparedService;
844         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
845                 .getNodeTypesArtifactsToHandle();
846
847         Either<Service, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedService, csarInfo, createdArtifacts,
848                 new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
849         log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
850         if (createdCsarArtifactsEither.isRight()) {
851             return createdCsarArtifactsEither;
852         }
853         resource = createdCsarArtifactsEither.left().value();
854         return Either.left(resource);
855     }
856
857     protected Either<Service, ResponseFormat> handleVfCsarArtifacts(Service service, CsarInfo csarInfo,
858                                                                   List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock,
859                                                                   boolean inTransaction) {
860
861         if (csarInfo.getCsar() != null) {
862             String vendorLicenseModelId = null;
863             String vfLicenseModelId = null;
864             if (artifactOperation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE) {
865                 Map<String, ArtifactDefinition> deploymentArtifactsMap = service.getDeploymentArtifacts();
866                 if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
867                     for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
868                         if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
869                             vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
870                         }
871                         if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
872                             vfLicenseModelId = artifactEntry.getValue().getUniqueId();
873                         }
874                     }
875                 }
876             }
877             createOrUpdateSingleNonMetaArtifact(service, csarInfo,
878                     CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL, Constants.VENDOR_LICENSE_MODEL,
879                     ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
880                     Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME,
881                     Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId, artifactOperation, null, true, shouldLock,
882                     inTransaction);
883             createOrUpdateSingleNonMetaArtifact(service, csarInfo,
884                     CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
885                     ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
886                     Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId,
887                     artifactOperation, null, true, shouldLock, inTransaction);
888
889             Either<Service, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, service,
890                     createdArtifacts, shouldLock, inTransaction, artifactOperation);
891             if (eitherCreateResult.isRight()) {
892                 return Either.right(eitherCreateResult.right().value());
893             }
894             Either<Service, StorageOperationStatus> eitherGerResource = toscaOperationFacade
895                     .getToscaElement(service.getUniqueId());
896             if (eitherGerResource.isRight()) {
897                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(
898                         componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), service, ComponentTypeEnum.SERVICE);
899                 return Either.right(responseFormat);
900             }
901             service = eitherGerResource.left().value();
902             Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
903
904             if (artifacsMetaCsarStatus.isLeft()) {
905                 String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
906                 String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
907                 Either<Service, ResponseFormat> createArtifactsFromCsar;
908                 if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
909                     createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic.createResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts);
910                 } else {
911                     Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic.updateResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction);
912                     if ((result.left().value() instanceof Service) && result.isLeft()) {
913                         Service service1 = (Service) result.left().value();
914                         createArtifactsFromCsar = Either.left(service1);
915                     } else {
916                         createArtifactsFromCsar = Either.right(result.right().value());
917                     }
918                 }
919                 if (createArtifactsFromCsar.isRight()) {
920                     log.debug("Couldn't create artifacts from artifacts.meta");
921                     return Either.right(createArtifactsFromCsar.right().value());
922                 }
923                 return Either.left(createArtifactsFromCsar.left().value());
924             } else {
925                 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(service, csarInfo, shouldLock, inTransaction);
926             }
927         }
928         return Either.left(service);
929     }
930
931     protected Either<Service, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Service resource,
932                                                                            List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction,
933                                                                            ArtifactOperationInfo artifactOperation) {
934         Either<Service, ResponseFormat> resStatus = null;
935         Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
936
937         try {
938             Either<List<CsarUtils.NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
939             if (artifactPathAndNameList.isRight()) {
940                 return Either.right(getComponentsUtils().getResponseFormatByArtifactId(
941                         ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value()));
942             }
943             EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
944
945             if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
946                 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class);
947                 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
948             } else {
949                 Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
950                         resource, artifactPathAndNameList.left().value(), csarInfo.getModifier());
951
952                 if (findVfCsarArtifactsToHandleRes.isRight()) {
953                     resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
954                 }
955                 if (resStatus == null) {
956                     vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
957                 }
958             }
959             if (resStatus == null && vfCsarArtifactsToHandle != null) {
960                 resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle);
961             }
962             if (resStatus == null) {
963                 resStatus = Either.left(resource);
964             }
965         } catch (Exception e) {
966             resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
967             log.debug("Exception occured in createNonMetaArtifacts, message:{}", e.getMessage(), e);
968         } finally {
969             CsarUtils.handleWarningMessages(collectedWarningMessages);
970         }
971         return resStatus;
972     }
973
974     protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(
975             Service resource, List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, User user) {
976         List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
977         if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) {
978             existingArtifacts.addAll(resource.getDeploymentArtifacts().values());
979         }
980         if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) {
981             existingArtifacts.addAll(resource.getArtifacts().values());
982         }
983         existingArtifacts = existingArtifacts.stream().filter(this::isNonMetaArtifact).collect(toList());
984
985         List<String> artifactsToIgnore = new ArrayList<>();
986         if (resource.getGroups() != null) {
987             resource.getGroups().stream().forEach(g -> {
988                 if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
989                     artifactsToIgnore.addAll(g.getArtifacts());
990                 }
991             });
992         }
993         existingArtifacts = existingArtifacts.stream()
994                 .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
995         return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user);
996     }
997
998     protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
999             List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle,
1000             Service resource, User user) {
1001         EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(
1002                 ArtifactsBusinessLogic.ArtifactOperationEnum.class);
1003         Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
1004         Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
1005                 .left(nodeTypeArtifactsToHandle);
1006         try {
1007             List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
1008             List<CsarUtils.NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
1009             List<CsarUtils.NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
1010             for (CsarUtils.NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
1011                 ArtifactDefinition foundArtifact;
1012
1013                 if (!existingArtifactsToHandle.isEmpty()) {
1014                     foundArtifact = existingArtifactsToHandle.stream()
1015                             .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst()
1016                             .orElse(null);
1017                     if (foundArtifact != null) {
1018                         if (ArtifactTypeEnum.findType(foundArtifact.getArtifactType()).equals(currNewArtifact
1019                                 .getArtifactType())) {
1020                             if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
1021                                 currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
1022                                 artifactsToUpdate.add(currNewArtifact);
1023                             }
1024                             existingArtifactsToHandle.remove(foundArtifact);
1025                             artifactsToUpload.remove(currNewArtifact);
1026                         } else {
1027                             log.debug("Can't upload two artifact with the same name {}.",
1028                                     currNewArtifact.getArtifactName());
1029                             ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat(
1030                                     ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR,
1031                                     currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(),
1032                                     foundArtifact.getArtifactType());
1033                             AuditingActionEnum auditingAction = serviceBusinessLogic.artifactsBusinessLogic
1034                                     .detectAuditingType(new ArtifactOperationInfo(false, false,
1035                                             ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE), foundArtifact.getArtifactChecksum());
1036                             serviceBusinessLogic.artifactsBusinessLogic.handleAuditing(auditingAction, resource, resource.getUniqueId(),
1037                                     user, null, null, foundArtifact.getUniqueId(), responseFormat,
1038                                     resource.getComponentType(), null);
1039                             responseWrapper.setInnerElement(responseFormat);
1040                             break;
1041                         }
1042                     }
1043                 }
1044             }
1045             if (responseWrapper.isEmpty()) {
1046                 for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
1047                     if (currArtifact.getIsFromCsar()) {
1048                         artifactsToDelete.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
1049                     } else {
1050                         artifactsToUpdate.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
1051                     }
1052                 }
1053             }
1054             if (responseWrapper.isEmpty()) {
1055                 if (!artifactsToUpload.isEmpty()) {
1056                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload);
1057                 }
1058                 if (!artifactsToUpdate.isEmpty()) {
1059                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate);
1060                 }
1061                 if (!artifactsToDelete.isEmpty()) {
1062                     nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete);
1063                 }
1064             }
1065             if (!responseWrapper.isEmpty()) {
1066                 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
1067             }
1068         } catch (Exception e) {
1069             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1070             responseWrapper.setInnerElement(responseFormat);
1071             log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
1072             nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
1073         }
1074         return nodeTypeArtifactsToHandleRes;
1075     }
1076
1077     protected Either<Service, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Service resource, List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, Either<Service, ResponseFormat> resStatus, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
1078         for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle
1079                 .entrySet()) {
1080             Optional<ResponseFormat> optionalCreateInDBError =
1081                     currArtifactOperationPair.getValue().stream()
1082                             .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(),
1083                                     e.getArtifactName(), e.getArtifactType(),
1084                                     e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(),
1085                                     CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(),
1086                                     new ArtifactOperationInfo(false, false,
1087                                             currArtifactOperationPair.getKey()),
1088                                     createdArtifacts, e.isFromCsar(), shouldLock, inTransaction))
1089                             .filter(Either::isRight).
1090                             map(e -> e.right().value()).
1091                             findAny();
1092             if (optionalCreateInDBError.isPresent()) {
1093                 resStatus = Either.right(optionalCreateInDBError.get());
1094                 break;
1095             }
1096         }
1097         return resStatus;
1098     }
1099
1100     public ComponentsUtils getComponentsUtils() {
1101         return this.componentsUtils;
1102     }
1103
1104     public void setComponentsUtils(ComponentsUtils componentsUtils) {
1105         this.componentsUtils = componentsUtils;
1106     }
1107
1108     protected Either<List<CsarUtils.NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo, Map<String, Set<List<String>>> collectedWarningMessages) {
1109         List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList =
1110                 csarInfo.getCsar().entrySet().stream()
1111                         .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey())
1112                                 .matches())
1113                         .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(),
1114                                 collectedWarningMessages))
1115                         .filter(Either::isLeft)
1116                         .map(e -> e.left().value())
1117                         .collect(toList());
1118         Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME);
1119         for (CsarUtils.NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) {
1120             if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) {
1121                 return Either.right(nonMetaArtifactInfo.getArtifactName());
1122             }
1123         }
1124         return Either.left(artifactPathAndNameList);
1125     }
1126
1127     protected Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Service service, CsarInfo csarInfo,
1128                                                                                 String artifactPath, String artifactFileName, String artifactType, ArtifactGroupTypeEnum artifactGroupType,
1129                                                                                 String artifactLabel, String artifactDisplayName, String artifactDescription, String artifactId,
1130                                                                                 ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts, boolean isFromCsar, boolean shouldLock,
1131                                                                                 boolean inTransaction) {
1132         byte[] artifactFileBytes = null;
1133         if (csarInfo.getCsar().containsKey(artifactPath)) {
1134             artifactFileBytes = csarInfo.getCsar().get(artifactPath);
1135         }
1136         Either<Boolean, ResponseFormat> result = Either.left(true);
1137         if (operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE || operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE) {
1138             if (serviceImportParseLogic.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
1139                 Either<ArtifactDefinition, ResponseFormat> handleDelete = serviceBusinessLogic.artifactsBusinessLogic.handleDelete(service.getUniqueId(), artifactId, csarInfo.getModifier(), service,
1140                         shouldLock, inTransaction);
1141                 if (handleDelete.isRight()) {
1142                     result = Either.right(handleDelete.right().value());
1143                 }
1144                 return result;
1145             }
1146             if (org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
1147                 operation = new ArtifactOperationInfo(false, false,
1148                         ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE);
1149             }
1150         }
1151         if (artifactFileBytes != null) {
1152             Map<String, Object> vendorLicenseModelJson = ArtifactUtils.buildJsonForUpdateArtifact(artifactId, artifactFileName,
1153                     artifactType, artifactGroupType, artifactLabel, artifactDisplayName, artifactDescription,
1154                     artifactFileBytes, null, isFromCsar);
1155             Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic.createOrUpdateCsarArtifactFromJson(
1156                     service, csarInfo.getModifier(), vendorLicenseModelJson, operation);
1157             serviceImportParseLogic
1158                     .addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
1159             if (eitherNonMetaArtifacts.isRight()) {
1160                 BeEcompErrorManager.getInstance()
1161                         .logInternalFlowError("UploadLicenseArtifact", "Failed to upload license artifact: "
1162                                         + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(),
1163                                 BeEcompErrorManager.ErrorSeverity.WARNING);
1164                 return Either.right(eitherNonMetaArtifacts.right().value());
1165             }
1166         }
1167         return result;
1168     }
1169
1170     protected Either<Service, ResponseFormat> createGroupsOnResource(Service service,
1171                                                                    Map<String, GroupDefinition> groups) {
1172         if (groups != null && !groups.isEmpty()) {
1173             List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, service);
1174             serviceImportParseLogic.handleGroupsProperties(service, groups);
1175             serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
1176             Either<List<GroupDefinition>, ResponseFormat> createGroups = serviceBusinessLogic.groupBusinessLogic.createGroups(service,
1177                     groupsAsList, true);
1178             if (createGroups.isRight()) {
1179                 return Either.right(createGroups.right().value());
1180             }
1181         } else {
1182             return Either.left(service);
1183         }
1184         Either<Service, StorageOperationStatus> updatedResource = toscaOperationFacade
1185                 .getToscaElement(service.getUniqueId());
1186         if (updatedResource.isRight()) {
1187             ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(
1188                     componentsUtils.convertFromStorageResponse(updatedResource.right().value()), service, ComponentTypeEnum.SERVICE);
1189             return Either.right(responseFormat);
1190         }
1191         return Either.left(updatedResource.left().value());
1192     }
1193
1194     protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Service component) {
1195         List<GroupDefinition> result = new ArrayList<>();
1196         List<ComponentInstance> componentInstances = component.getComponentInstances();
1197         if (groups != null) {
1198             Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = serviceImportParseLogic.validateCyclicGroupsDependencies(groups);
1199             if (validateCyclicGroupsDependencies.isRight()) {
1200                 throw new ComponentException(validateCyclicGroupsDependencies.right().value());
1201             }
1202             for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
1203                 String groupName = entry.getKey();
1204                 GroupDefinition groupDefinition = entry.getValue();
1205                 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
1206                 updatedGroupDefinition.setMembers(null);
1207                 Map<String, String> members = groupDefinition.getMembers();
1208                 if (members != null) {
1209                     serviceImportParseLogic
1210                             .updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
1211                 }
1212                 result.add(updatedGroupDefinition);
1213             }
1214         }
1215         return result;
1216     }
1217
1218
1219     protected Resource createRIAndRelationsFromYaml(String yamlName, Resource resource,
1220                                                   Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
1221                                                   String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1222                                                   Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1223                                                   Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1224                                                   String nodeName) {
1225         try {
1226             log.debug("************* Going to create all nodes {}", yamlName);
1227             handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts,
1228                     nodeTypesInfo, csarInfo, nodeName);
1229             log.debug("************* Going to create all resource instances {}", yamlName);
1230             resource = createResourceInstances(yamlName, resource,
1231                     uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes());
1232             log.debug("************* Finished to create all resource instances {}", yamlName);
1233             resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, uploadComponentInstanceInfoMap);
1234             log.debug("************* Going to create positions {}", yamlName);
1235             compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId());
1236             log.debug("************* Finished to set positions {}", yamlName);
1237             return resource;
1238         } catch (Exception e) {
1239             throw new ComponentException(ActionStatus.GENERAL_ERROR);
1240         }
1241     }
1242
1243     protected Resource createResourceInstancesRelations(User user, String yamlName, Resource resource,
1244                                                       Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
1245         log.debug("#createResourceInstancesRelations - Going to create relations ");
1246         List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
1247         if (((MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) &&
1248                 resource.getResourceType() != ResourceTypeEnum.PNF)) { // PNF can have no resource instances
1249             log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ", resource.getUniqueId(), yamlName);
1250             BeEcompErrorManager.getInstance().logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ", BeEcompErrorManager.ErrorSeverity.ERROR);
1251             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName));
1252         }
1253         Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
1254         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
1255         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
1256         Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
1257         Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
1258         Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>();
1259         Map<String, Resource> originCompMap = new HashMap<>();
1260         List<RequirementCapabilityRelDef> relations = new ArrayList<>();
1261         Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
1262
1263         log.debug("enter ServiceImportBusinessLogic createResourceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. ");
1264         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = serviceBusinessLogic.dataTypeCache.getAll();
1265         if (allDataTypes.isRight()) {
1266             JanusGraphOperationStatus status = allDataTypes.right().value();
1267             BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance",
1268                     "Failed to update property value on instance. Status is " + status, BeEcompErrorManager.ErrorSeverity.ERROR);
1269             throw new ComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
1270                     DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName));
1271         }
1272         Resource finalResource = resource;
1273         uploadResInstancesMap
1274                 .values()
1275                 .forEach(i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes,
1276                         instProperties, instCapabilities, instRequirements, instDeploymentArtifacts,
1277                         instArtifacts, instAttributes, originCompMap, instInputs, i));
1278         serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties);
1279         serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, resource, instInputs);
1280         serviceImportParseLogic
1281                 .associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts);
1282         serviceImportParseLogic.associateArtifactsToInstances(yamlName, resource, instArtifacts);
1283         serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements);
1284         serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes);
1285         resource = serviceImportParseLogic.getResourceAfterCreateRelations(resource);
1286
1287         serviceImportParseLogic
1288                 .addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations);
1289         serviceImportParseLogic.associateResourceInstances(yamlName, resource, relations);
1290         handleSubstitutionMappings(resource, uploadResInstancesMap);
1291         log.debug("************* in create relations, getResource start");
1292         Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
1293         log.debug("************* in create relations, getResource end");
1294         if (eitherGetResource.isRight()) {
1295             throw new ComponentException(componentsUtils.getResponseFormatByResource(
1296                     componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource));
1297         }
1298         return eitherGetResource.left().value();
1299     }
1300
1301     protected void processComponentInstance(String yamlName, Resource resource, List<ComponentInstance> componentInstancesList, Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<ComponentInstanceProperty>> instProperties, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, Map<String, Map<String, ArtifactDefinition>> instArtifacts, Map<String, List<AttributeDataDefinition>> instAttributes, Map<String, Resource> originCompMap, Map<String, List<ComponentInstanceInput>> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) {
1302         Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
1303                 .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName()))
1304                 .findFirst();
1305         if (!currentCompInstanceOpt.isPresent()) {
1306             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(),
1307                     resource.getUniqueId());
1308             BeEcompErrorManager.getInstance().logInternalDataError(
1309                     COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE,
1310                     resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
1311             ResponseFormat responseFormat = componentsUtils
1312                     .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
1313             throw new ComponentException(responseFormat);
1314         }
1315         ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
1316         String resourceInstanceId = currentCompInstance.getUniqueId();
1317         Resource originResource = getOriginResource(yamlName, originCompMap, currentCompInstance);
1318         log.debug("enter processComponentInstance,get originResource Requirements:{}",
1319                 originResource.getRequirements());
1320         if (MapUtils.isNotEmpty(originResource.getRequirements())) {
1321             instRequirements.put(currentCompInstance, originResource.getRequirements());
1322         }
1323         if (MapUtils.isNotEmpty(originResource.getCapabilities())) {
1324             processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo,
1325                     currentCompInstance, originResource);
1326         }
1327         if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
1328             instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
1329         }
1330         if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
1331             instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
1332         }
1333         if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
1334             instAttributes.put(resourceInstanceId, originResource.getAttributes());
1335         }
1336         if (originResource.getResourceType() != ResourceTypeEnum.VF) {
1337             ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource,
1338                     originResource, currentCompInstance, instProperties, allDataTypes.left().value());
1339             if (addPropertiesValueToRiRes.getStatus() != 200) {
1340                 throw new ComponentException(addPropertiesValueToRiRes);
1341             }
1342         } else {
1343             addInputsValuesToRi(uploadComponentInstanceInfo, resource,
1344                     originResource, currentCompInstance, instInputs, allDataTypes.left().value());
1345         }
1346     }
1347
1348     protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo,
1349                                      Resource resource, Resource originResource, ComponentInstance currentCompInstance,
1350                                      Map<String, List<ComponentInstanceInput>> instInputs, Map<String, DataTypeDefinition> allDataTypes) {
1351         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
1352         try {
1353             if (MapUtils.isNotEmpty(propMap)) {
1354                 Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
1355                 List<ComponentInstanceInput> instPropList = new ArrayList<>();
1356
1357                 if (CollectionUtils.isEmpty(originResource.getInputs())) {
1358                     log.debug("failed to find properties ");
1359                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
1360                 }
1361                 originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p));
1362                 for (List<UploadPropInfo> propertyList : propMap.values()) {
1363                     processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
1364                 }
1365                 currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
1366                 instInputs.put(currentCompInstance.getUniqueId(), instPropList);
1367             }
1368         } catch (Exception e) {
1369             log.debug("failed to add Inputs Values To Ri");
1370             throw new ComponentException(ActionStatus.GENERAL_ERROR);
1371         }
1372     }
1373
1374     protected void processProperty(Resource resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes, Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList, List<UploadPropInfo> propertyList) {
1375         UploadPropInfo propertyInfo = propertyList.get(0);
1376         String propName = propertyInfo.getName();
1377         if (!currPropertiesMap.containsKey(propName)) {
1378             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND,
1379                     propName));
1380         }
1381         InputDefinition curPropertyDef = currPropertiesMap.get(propName);
1382         ComponentInstanceInput property = null;
1383
1384         String value = null;
1385         List<GetInputValueDataDefinition> getInputs = null;
1386         boolean isValidate = true;
1387         if (propertyInfo.getValue() != null) {
1388             getInputs = propertyInfo.getGet_input();
1389             isValidate = getInputs == null || getInputs.isEmpty();
1390             if (isValidate) {
1391                 value = getPropertyJsonStringValue(propertyInfo.getValue(),
1392                         curPropertyDef.getType());
1393             } else {
1394                 value = getPropertyJsonStringValue(propertyInfo.getValue(),
1395                         TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
1396             }
1397         }
1398         String innerType = null;
1399         property = new ComponentInstanceInput(curPropertyDef, value, null);
1400         String validPropertyVAlue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
1401         property.setValue(validPropertyVAlue);
1402
1403         if (isNotEmpty(getInputs)) {
1404             List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
1405             for (GetInputValueDataDefinition getInput : getInputs) {
1406                 List<InputDefinition> inputs = resource.getInputs();
1407                 if (CollectionUtils.isEmpty(inputs)) {
1408                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1409                 }
1410
1411                 Optional<InputDefinition> optional = inputs.stream()
1412                         .filter(p -> p.getName().equals(getInput.getInputName())).findAny();
1413                 if (!optional.isPresent()) {
1414                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1415                 }
1416                 InputDefinition input = optional.get();
1417                 getInput.setInputId(input.getUniqueId());
1418                 getInputValues.add(getInput);
1419
1420                 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1421                 processGetInput(getInputValues, inputs, getInputIndex);
1422             }
1423             property.setGetInputValues(getInputValues);
1424         }
1425         instPropList.add(property);
1426         currPropertiesMap.remove(property.getName());
1427     }
1428
1429     protected void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
1430         if (resource.getResourceType() == ResourceTypeEnum.VF) {
1431             Either<Resource, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaFullElement(resource.getUniqueId());
1432             if (getResourceRes.isRight()) {
1433                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
1434                         componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
1435                 throw new ComponentException(responseFormat);
1436             }
1437             getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(getResourceRes.left().value(),
1438                     uploadResInstancesMap);
1439             if (getResourceRes.isRight()) {
1440                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
1441                         componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
1442                 throw new ComponentException(responseFormat);
1443             }
1444         }
1445     }
1446
1447     protected Resource createResourceInstances(String yamlName, Resource resource,
1448                                              Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
1449                                              Map<String, Resource> nodeNamespaceMap) {
1450         Either<Resource, ResponseFormat> eitherResource = null;
1451         log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
1452         if (MapUtils.isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
1453             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
1454             throw new ComponentException(responseFormat);
1455         }
1456         Map<String, Resource> existingNodeTypeMap = new HashMap<>();
1457         if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
1458             nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
1459         }
1460         Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
1461         uploadResInstancesMap
1462                 .values()
1463                 .forEach(i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
1464
1465         if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
1466             try {
1467                 toscaOperationFacade.associateComponentInstancesToComponent(resource,
1468                         resourcesInstancesMap, false, false);
1469             } catch (StorageException exp) {
1470                 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
1471                     log.debug("Failed to add component instances to container component {}", resource.getName());
1472                     ResponseFormat responseFormat = componentsUtils
1473                             .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
1474                     eitherResource = Either.right(responseFormat);
1475                     throw new ByResponseFormatComponentException(eitherResource.right().value());
1476                 }
1477             }
1478         }
1479         log.debug("*************Going to get resource {}", resource.getUniqueId());
1480         Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade
1481                 .getToscaElement(resource.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
1482         log.debug("*************finished to get resource {}", resource.getUniqueId());
1483         if (eitherGetResource.isRight()) {
1484             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
1485                     componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource);
1486             throw new ComponentException(responseFormat);
1487         }
1488         if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances()) &&
1489                 resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
1490             log.debug("Error when create resource instance from csar. ComponentInstances list empty");
1491             BeEcompErrorManager.getInstance().logBeDaoSystemError(
1492                     "Error when create resource instance from csar. ComponentInstances list empty");
1493             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
1494         }
1495         return eitherGetResource.left().value();
1496     }
1497
1498     protected void handleNodeTypes(String yamlName, Resource resource,
1499                                  String topologyTemplateYaml, boolean needLock,
1500                                  Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1501                                  List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
1502                                  CsarInfo csarInfo, String nodeName) {
1503         try {
1504             for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
1505                 if (nodeTypeEntry.getValue().isNested()) {
1506
1507                     handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts,
1508                             nodeTypesInfo, csarInfo, nodeTypeEntry.getKey());
1509                     log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
1510                 }
1511             }
1512             Map<String, Object> mappedToscaTemplate = null;
1513             if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo)
1514                     && nodeTypesInfo.containsKey(nodeName)) {
1515                 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
1516             }
1517             if (MapUtils.isEmpty(mappedToscaTemplate)) {
1518                 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
1519             }
1520             createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
1521                     nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
1522         } catch (ComponentException e) {
1523             ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat()
1524                     : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1525             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1526             throw e;
1527         } catch (StorageException e) {
1528             ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
1529             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1530             throw e;
1531         }catch (Exception e){
1532             log.debug("Exception occured when handleNodeTypes, error is:{}", e.getMessage(), e);
1533             throw new ComponentException(ActionStatus.GENERAL_ERROR);
1534         }
1535     }
1536
1537     protected Resource handleNestedVfc(Resource resource, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1538                                      List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1539                                      String nodeName) {
1540         String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
1541         Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
1542
1543         log.debug("************* Going to create node types from yaml {}", yamlName);
1544         createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false,
1545                 nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo);
1546
1547         if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
1548             log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
1549             resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
1550                     csarInfo, nodeName, yamlName);
1551         }
1552         return resource;
1553     }
1554
1555     protected Resource handleComplexVfc(Resource resource, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1556                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1557                                       String nodeName, String yamlName) {
1558         Resource oldComplexVfc = null;
1559         Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo);
1560         Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
1561                 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
1562         if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
1563             oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
1564                     serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(),
1565                             nodeName).getRight());
1566         }
1567         if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
1568             throw new ComponentException(ActionStatus.GENERAL_ERROR);
1569         } else if (oldComplexVfcRes.isLeft()) {
1570             log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
1571             Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic.validateNestedDerivedFromDuringUpdate(
1572                     oldComplexVfcRes.left().value(), newComplexVfc,
1573                     ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
1574             if (eitherValidation.isLeft()) {
1575                 oldComplexVfc = oldComplexVfcRes.left().value();
1576             }
1577         }
1578         newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName,
1579                 oldComplexVfc, newComplexVfc);
1580         csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
1581         LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(
1582                 CERTIFICATION_ON_IMPORT, LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1583         log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
1584         final Resource result = serviceImportParseLogic
1585                 .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false,
1586                         true);
1587         csarInfo.getCreatedNodes().put(nodeName, result);
1588         csarInfo.removeNodeFromQueue();
1589         return result;
1590     }
1591
1592     public Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate, boolean needLock,
1593                                                                       Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1594                                                                       List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
1595                                                                       CsarInfo csarInfo) {
1596         Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
1597                 TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
1598         if (toscaVersion.isRight()) {
1599             throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
1600         }
1601         Map<String, Object> mapToConvert = new HashMap<>();
1602         mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
1603         Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
1604         createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, nodeTypes);
1605         return csarInfo.getCreatedNodes();
1606     }
1607
1608     protected void createNodeTypes(String yamlName, Resource resource, boolean needLock, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
1609         Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
1610         Resource vfcCreated = null;
1611         while (nodesNameValueIter.hasNext()) {
1612             Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
1613             Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = nodeTypesArtifactsToHandle == null
1614                     || nodeTypesArtifactsToHandle.isEmpty() ? null
1615                     : nodeTypesArtifactsToHandle.get(nodeType.getKey());
1616
1617             if (nodeTypesInfo.containsKey(nodeType.getKey())) {
1618                 log.trace("************* Going to handle nested vfc {}", nodeType.getKey());
1619                 vfcCreated = handleNestedVfc(resource,
1620                         nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1621                         nodeType.getKey());
1622                 log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
1623             } else if (csarInfo.getCreatedNodesToscaResourceNames() != null
1624                     && !csarInfo.getCreatedNodesToscaResourceNames().containsKey(nodeType.getKey())) {
1625                 log.trace("************* Going to create node {}", nodeType.getKey());
1626                 ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert,
1627                         resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true,
1628                         csarInfo, true);
1629                 log.debug("************* Finished to create node {}", nodeType.getKey());
1630
1631                 vfcCreated = resourceCreated.getLeft();
1632                 csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(),
1633                         vfcCreated.getToscaResourceName());
1634             }
1635             if (vfcCreated != null) {
1636                 csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
1637             }
1638             mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
1639         }
1640     }
1641
1642     protected ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml(
1643             String yamlName, Map.Entry<String, Object> nodeNameValue, User user, Map<String, Object> mapToConvert,
1644             Resource resourceVf, boolean needLock,
1645             Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1646             List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo,
1647             boolean isNested) {
1648         UploadResourceInfo resourceMetaData = serviceImportParseLogic
1649                 .fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user);
1650
1651         String singleVfcYaml = serviceImportParseLogic.buildNodeTypeYaml(nodeNameValue, mapToConvert,
1652                 resourceMetaData.getResourceType(), csarInfo);
1653         user = serviceBusinessLogic.validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true);
1654         return serviceImportParseLogic.createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock,
1655                 nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo,
1656                 nodeNameValue.getKey(), isNested);
1657     }
1658
1659     protected Service createRIAndRelationsFromYaml(String yamlName, Service service,
1660                                                  Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
1661                                                  String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1662                                                  Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1663                                                  Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1664                                                  String nodeName) {
1665         log.debug("************* Going to create all nodes {}", yamlName);
1666         handleServiceNodeTypes(yamlName, service, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeName);
1667         log.debug("************* Going to create all resource instances {}", yamlName);
1668         service = createServiceInstances(yamlName, service, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes());
1669         log.debug("************* Going to create all relations {}", yamlName);
1670         service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap);
1671         log.debug("************* Going to create positions {}", yamlName);
1672         compositionBusinessLogic.setPositionsForComponentInstances(service, csarInfo.getModifier().getUserId());
1673         log.debug("************* Finished to set positions {}", yamlName);
1674         return service;
1675     }
1676
1677     protected Service createServiceInstancesRelations(User user, String yamlName, Service service,
1678                                                     Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
1679         log.debug("#createResourceInstancesRelations - Going to create relations ");
1680         List<ComponentInstance> componentInstancesList = service.getComponentInstances();
1681         if (((MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)))) { // PNF can have no resource instances
1682             log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ", service.getUniqueId(), yamlName);
1683             BeEcompErrorManager.getInstance().logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ", BeEcompErrorManager.ErrorSeverity.ERROR);
1684             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName));
1685         }
1686         Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
1687         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
1688         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
1689         Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
1690         Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
1691         Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>();
1692         Map<String, Resource> originCompMap = new HashMap<>();
1693         List<RequirementCapabilityRelDef> relations = new ArrayList<>();
1694         Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
1695
1696         log.debug("enter ServiceImportBusinessLogic  createServiceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. ");
1697         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = serviceBusinessLogic.dataTypeCache.getAll();
1698         if (allDataTypes.isRight()) {
1699             JanusGraphOperationStatus status = allDataTypes.right().value();
1700             BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance",
1701                     "Failed to update property value on instance. Status is " + status, BeEcompErrorManager.ErrorSeverity.ERROR);
1702             throw new ComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
1703                     DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName));
1704         }
1705         Service finalResource = service;
1706         uploadResInstancesMap
1707                 .values()
1708                 .forEach(i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes,
1709                         instProperties, instCapabilities, instRequirements, instDeploymentArtifacts,
1710                         instArtifacts, instAttributes, originCompMap, instInputs, i));
1711         serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, service, instProperties);
1712         serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, service, instInputs);
1713         serviceImportParseLogic.associateDeploymentArtifactsToInstances(user, yamlName, service, instDeploymentArtifacts);
1714         serviceImportParseLogic.associateArtifactsToInstances(yamlName, service, instArtifacts);
1715         serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, service, instCapabilities, instRequirements);
1716         log.debug("enter createServiceInstancesRelations test,instRequirements:{},instCapabilities:{}",
1717                 instRequirements, instCapabilities);
1718         serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, service, instAttributes);
1719         ToscaElement serviceTemplate = ModelConverter.convertToToscaElement(service);
1720         Map<String, ListCapabilityDataDefinition> capabilities = serviceTemplate.getCapabilities();
1721         Map<String, ListRequirementDataDefinition> requirements = serviceTemplate.getRequirements();
1722
1723         serviceImportParseLogic.associateCapabilitiesToService(yamlName, service, capabilities);
1724         serviceImportParseLogic.associateRequirementsToService(yamlName, service, requirements);
1725         service = getResourceAfterCreateRelations(service);
1726
1727         addRelationsToRI(yamlName, service, uploadResInstancesMap, componentInstancesList, relations);
1728         serviceImportParseLogic.associateResourceInstances(yamlName, service, relations);
1729         handleSubstitutionMappings(service, uploadResInstancesMap);
1730         log.debug("************* in create relations, getResource start");
1731         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId());
1732         log.debug("************* in create relations, getResource end");
1733         if (eitherGetResource.isRight()) {
1734             throw new ComponentException(componentsUtils.getResponseFormatByComponent(
1735                     componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service, service.getComponentType()));
1736         }
1737         return eitherGetResource.left().value();
1738     }
1739
1740     protected void processComponentInstance(String yamlName, Service service, List<ComponentInstance> componentInstancesList, Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<ComponentInstanceProperty>> instProperties, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, Map<String, Map<String, ArtifactDefinition>> instArtifacts, Map<String, List<AttributeDataDefinition>> instAttributes, Map<String, Resource> originCompMap, Map<String, List<ComponentInstanceInput>> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) {
1741         log.debug("enter ServiceImportBusinessLogic processComponentInstance");
1742         Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
1743                 .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName()))
1744                 .findFirst();
1745         if (!currentCompInstanceOpt.isPresent()) {
1746             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(),
1747                     service.getUniqueId());
1748             BeEcompErrorManager.getInstance().logInternalDataError(
1749                     COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE,
1750                     service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
1751             ResponseFormat responseFormat = componentsUtils
1752                     .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
1753             throw new ComponentException(responseFormat);
1754         }
1755         ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
1756         String resourceInstanceId = currentCompInstance.getUniqueId();
1757         Resource originResource = getOriginResource(yamlName, originCompMap, currentCompInstance);
1758
1759         if (MapUtils.isNotEmpty(originResource.getRequirements())) {
1760             instRequirements.put(currentCompInstance, originResource.getRequirements());
1761         }
1762         if (MapUtils.isNotEmpty(originResource.getCapabilities())) {
1763             processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo,
1764                     currentCompInstance, originResource);
1765         }
1766         if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
1767             instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
1768         }
1769         if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
1770             instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
1771         }
1772         if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
1773             instAttributes.put(resourceInstanceId, originResource.getAttributes());
1774         }
1775         if (originResource.getResourceType() != ResourceTypeEnum.VF) {
1776             ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, service,
1777                     originResource, currentCompInstance, instProperties, allDataTypes.left().value());
1778             if (addPropertiesValueToRiRes.getStatus() != 200) {
1779                 throw new ComponentException(addPropertiesValueToRiRes);
1780             }
1781         } else {
1782             addInputsValuesToRi(uploadComponentInstanceInfo, service,
1783                     originResource, currentCompInstance, instInputs, allDataTypes.left().value());
1784         }
1785     }
1786
1787     protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo,
1788                                      Service resource, Resource originResource, ComponentInstance currentCompInstance,
1789                                      Map<String, List<ComponentInstanceInput>> instInputs, Map<String, DataTypeDefinition> allDataTypes) {
1790         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
1791         if (MapUtils.isNotEmpty(propMap)) {
1792             Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
1793             List<ComponentInstanceInput> instPropList = new ArrayList<>();
1794
1795             if (CollectionUtils.isEmpty(originResource.getInputs())) {
1796                 log.debug("failed to find properties ");
1797                 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
1798             }
1799             originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p));
1800             for (List<UploadPropInfo> propertyList : propMap.values()) {
1801                 processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
1802             }
1803             currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
1804             instInputs.put(currentCompInstance.getUniqueId(), instPropList);
1805         }
1806     }
1807
1808     protected void processProperty(Service resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes, Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList, List<UploadPropInfo> propertyList) {
1809         UploadPropInfo propertyInfo = propertyList.get(0);
1810         String propName = propertyInfo.getName();
1811         if (!currPropertiesMap.containsKey(propName)) {
1812             log.debug("failed to find property {} ", propName);
1813             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND,
1814                     propName));
1815         }
1816         InputDefinition curPropertyDef = currPropertiesMap.get(propName);
1817         ComponentInstanceInput property = null;
1818
1819         String value = null;
1820         List<GetInputValueDataDefinition> getInputs = null;
1821         boolean isValidate = true;
1822         if (propertyInfo.getValue() != null) {
1823             getInputs = propertyInfo.getGet_input();
1824             isValidate = getInputs == null || getInputs.isEmpty();
1825             if (isValidate) {
1826                 value = getPropertyJsonStringValue(propertyInfo.getValue(),
1827                         curPropertyDef.getType());
1828             } else {
1829                 value = getPropertyJsonStringValue(propertyInfo.getValue(),
1830                         TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
1831             }
1832         }
1833         String innerType = null;
1834         property = new ComponentInstanceInput(curPropertyDef, value, null);
1835         String validPropertyVAlue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
1836         property.setValue(validPropertyVAlue);
1837
1838         if (isNotEmpty(getInputs)) {
1839             List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
1840             for (GetInputValueDataDefinition getInput : getInputs) {
1841                 List<InputDefinition> inputs = resource.getInputs();
1842                 if (CollectionUtils.isEmpty(inputs)) {
1843                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1844                 }
1845                 Optional<InputDefinition> optional = inputs.stream()
1846                         .filter(p -> p.getName().equals(getInput.getInputName())).findAny();
1847                 if (!optional.isPresent()) {
1848                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1849                 }
1850                 InputDefinition input = optional.get();
1851                 getInput.setInputId(input.getUniqueId());
1852                 getInputValues.add(getInput);
1853
1854                 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1855                 processGetInput(getInputValues, inputs, getInputIndex);
1856             }
1857             property.setGetInputValues(getInputValues);
1858         }
1859         instPropList.add(property);
1860         currPropertiesMap.remove(property.getName());
1861     }
1862
1863     protected void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs, GetInputValueDataDefinition getInputIndex) {
1864         Optional<InputDefinition> optional;
1865         if (getInputIndex != null) {
1866             optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName()))
1867                     .findAny();
1868             if (!optional.isPresent()) {
1869                 log.debug("Failed to find input {} ", getInputIndex.getInputName());
1870                 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1871             }
1872             InputDefinition inputIndex = optional.get();
1873             getInputIndex.setInputId(inputIndex.getUniqueId());
1874             getInputValues.add(getInputIndex);
1875         }
1876     }
1877
1878     protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo,
1879                                                  Resource resource, Resource originResource, ComponentInstance currentCompInstance,
1880                                                  Map<String, List<ComponentInstanceProperty>> instProperties, Map<String, DataTypeDefinition> allDataTypes) {
1881         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
1882         Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
1883
1884         List<PropertyDefinition> listFromMap = originResource.getProperties();
1885         if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) {
1886             log.debug("failed to find properties ");
1887             return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
1888         }
1889         if (listFromMap == null || listFromMap.isEmpty()) {
1890             return componentsUtils.getResponseFormat(ActionStatus.OK);
1891         }
1892         for (PropertyDefinition prop : listFromMap) {
1893             String propName = prop.getName();
1894             if (!currPropertiesMap.containsKey(propName)) {
1895                 currPropertiesMap.put(propName, prop);
1896             }
1897         }
1898         List<ComponentInstanceProperty> instPropList = new ArrayList<>();
1899         if (propMap != null && propMap.size() > 0) {
1900             for (List<UploadPropInfo> propertyList : propMap.values()) {
1901                 UploadPropInfo propertyInfo = propertyList.get(0);
1902                 String propName = propertyInfo.getName();
1903                 if (!currPropertiesMap.containsKey(propName)) {
1904                     log.debug("failed to find property {} ", propName);
1905                     return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND,
1906                             propName);
1907                 }
1908                 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
1909                 ComponentInstanceProperty property = null;
1910
1911                 String value = null;
1912                 List<GetInputValueDataDefinition> getInputs = null;
1913                 boolean isValidate = true;
1914                 if (propertyInfo.getValue() != null) {
1915                     getInputs = propertyInfo.getGet_input();
1916                     isValidate = getInputs == null || getInputs.isEmpty();
1917                     if (isValidate) {
1918                         value = getPropertyJsonStringValue(propertyInfo.getValue(),
1919                                 curPropertyDef.getType());
1920                     } else {
1921                         value = getPropertyJsonStringValue(propertyInfo.getValue(),
1922                                 TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
1923                     }
1924                 }
1925                 String innerType = null;
1926                 property = new ComponentInstanceProperty(curPropertyDef, value, null);
1927
1928                 String validatePropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
1929                 property.setValue(validatePropValue);
1930
1931                 if (getInputs != null && !getInputs.isEmpty()) {
1932                     List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
1933                     for (GetInputValueDataDefinition getInput : getInputs) {
1934                         List<InputDefinition> inputs = resource.getInputs();
1935                         if (inputs == null || inputs.isEmpty()) {
1936                             log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
1937                             serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, property.getGetInputValues()
1938                                     .stream()
1939                                     .map(GetInputValueDataDefinition::getInputName)
1940                                     .collect(toList()).toString());
1941                         }
1942                         InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput);
1943                         getInput.setInputId(input.getUniqueId());
1944                         getInputValues.add(getInput);
1945
1946                         GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1947                         if (getInputIndex != null) {
1948                             input = serviceImportParseLogic.findInputByName(inputs, getInputIndex);
1949                             getInputIndex.setInputId(input.getUniqueId());
1950                             getInputValues.add(getInputIndex);
1951                         }
1952                     }
1953                     property.setGetInputValues(getInputValues);
1954                 }
1955                 instPropList.add(property);
1956                 currPropertiesMap.remove(property.getName());
1957             }
1958         }
1959         if (!currPropertiesMap.isEmpty()) {
1960             for (PropertyDefinition value : currPropertiesMap.values()) {
1961                 instPropList.add(new ComponentInstanceProperty(value));
1962             }
1963         }
1964         instProperties.put(currentCompInstance.getUniqueId(), instPropList);
1965         return componentsUtils.getResponseFormat(ActionStatus.OK);
1966     }
1967
1968     protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo,
1969                                                  Service service, Resource originResource, ComponentInstance currentCompInstance,
1970                                                  Map<String, List<ComponentInstanceProperty>> instProperties, Map<String, DataTypeDefinition> allDataTypes) {
1971         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
1972         Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
1973
1974         List<PropertyDefinition> listFromMap = originResource.getProperties();
1975         if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) {
1976             log.debug("failed to find properties ");
1977             return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
1978         }
1979         if (listFromMap == null || listFromMap.isEmpty()) {
1980             return componentsUtils.getResponseFormat(ActionStatus.OK);
1981         }
1982         for (PropertyDefinition prop : listFromMap) {
1983             String propName = prop.getName();
1984             if (!currPropertiesMap.containsKey(propName)) {
1985                 currPropertiesMap.put(propName, prop);
1986             }
1987         }
1988         List<ComponentInstanceProperty> instPropList = new ArrayList<>();
1989         if (propMap != null && propMap.size() > 0) {
1990             for (List<UploadPropInfo> propertyList : propMap.values()) {
1991
1992                 UploadPropInfo propertyInfo = propertyList.get(0);
1993                 String propName = propertyInfo.getName();
1994                 if (!currPropertiesMap.containsKey(propName)) {
1995                     log.debug("failed to find property {} ", propName);
1996                     return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND,
1997                             propName);
1998                 }
1999                 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
2000                 ComponentInstanceProperty property = null;
2001
2002                 String value = null;
2003                 List<GetInputValueDataDefinition> getInputs = null;
2004                 boolean isValidate = true;
2005                 if (propertyInfo.getValue() != null) {
2006                     getInputs = propertyInfo.getGet_input();
2007                     isValidate = getInputs == null || getInputs.isEmpty();
2008                     if (isValidate) {
2009                         value = getPropertyJsonStringValue(propertyInfo.getValue(),
2010                                 curPropertyDef.getType());
2011                     } else {
2012                         value = getPropertyJsonStringValue(propertyInfo.getValue(),
2013                                 TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2014                     }
2015                 }
2016                 String innerType = null;
2017                 property = new ComponentInstanceProperty(curPropertyDef, value, null);
2018
2019                 String validatePropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2020                 property.setValue(validatePropValue);
2021
2022                 if (getInputs != null && !getInputs.isEmpty()) {
2023                     List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
2024                     for (GetInputValueDataDefinition getInput : getInputs) {
2025                         List<InputDefinition> inputs = service.getInputs();
2026                         if (inputs == null || inputs.isEmpty()) {
2027                             log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
2028                             serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, property.getGetInputValues()
2029                                     .stream()
2030                                     .map(GetInputValueDataDefinition::getInputName)
2031                                     .collect(toList()).toString());
2032                         }
2033                         InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput);
2034                         getInput.setInputId(input.getUniqueId());
2035                         getInputValues.add(getInput);
2036
2037                         GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
2038                         if (getInputIndex != null) {
2039                             input = serviceImportParseLogic.findInputByName(inputs, getInputIndex);
2040                             getInputIndex.setInputId(input.getUniqueId());
2041                             getInputValues.add(getInputIndex);
2042                         }
2043                     }
2044                     property.setGetInputValues(getInputValues);
2045                 }
2046                 instPropList.add(property);
2047                 currPropertiesMap.remove(property.getName());
2048             }
2049         }
2050         if (!currPropertiesMap.isEmpty()) {
2051             for (PropertyDefinition value : currPropertiesMap.values()) {
2052                 instPropList.add(new ComponentInstanceProperty(value));
2053             }
2054         }
2055         instProperties.put(currentCompInstance.getUniqueId(), instPropList);
2056         return componentsUtils.getResponseFormat(ActionStatus.OK);
2057     }
2058
2059     protected void processComponentInstanceCapabilities(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance, Resource originResource) {
2060         log.debug("enter processComponentInstanceCapabilities");
2061         Map<String, List<CapabilityDefinition>> originCapabilities;
2062         if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2063             originCapabilities = new HashMap<>();
2064             Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2065             originResource.getCapabilities().forEach((k, v) -> serviceImportParseLogic
2066                     .addCapabilities(originCapabilities, k, v));
2067             uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> serviceImportParseLogic
2068                     .addCapabilitiesProperties(newPropertiesMap, l));
2069             updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
2070         } else {
2071             originCapabilities = originResource.getCapabilities();
2072         }
2073         instCapabilties.put(currentCompInstance, originCapabilities);
2074     }
2075
2076     protected void updateCapabilityPropertiesValues(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<CapabilityDefinition>> originCapabilities, Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
2077         originCapabilities.values().stream()
2078                 .flatMap(Collection::stream)
2079                 .filter(c -> newPropertiesMap.containsKey(c.getName()))
2080                 .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes.left().value()));
2081     }
2082
2083     protected void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2084                                       Map<String, DataTypeDefinition> allDataTypes) {
2085         properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2086     }
2087
2088     protected String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2089                                        Map<String, DataTypeDefinition> allDataTypes) {
2090         String value = null;
2091         List<GetInputValueDataDefinition> getInputs = null;
2092         boolean isValidate = true;
2093         if (null != propertyInfo && propertyInfo.getValue() != null) {
2094             getInputs = propertyInfo.getGet_input();
2095             isValidate = getInputs == null || getInputs.isEmpty();
2096             if (isValidate) {
2097                 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2098             } else {
2099                 value = getPropertyJsonStringValue(propertyInfo.getValue(),
2100                         TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2101             }
2102         }
2103         property.setValue(value);
2104         return serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2105     }
2106
2107     protected Resource getOriginResource(String yamlName, Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2108         Resource originResource;
2109         log.debug("after enter ServiceImportBusinessLogic processComponentInstance, enter getOriginResource");
2110         if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2111             Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2112                     .getToscaFullElement(currentCompInstance.getComponentUid());
2113             if (getOriginResourceRes.isRight()) {
2114                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(
2115                         componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName);
2116                 throw new ComponentException(responseFormat);
2117             }
2118             originResource = getOriginResourceRes.left().value();
2119             originCompMap.put(originResource.getUniqueId(), originResource);
2120         } else {
2121             originResource = originCompMap.get(currentCompInstance.getComponentUid());
2122         }
2123         return originResource;
2124     }
2125
2126     protected void handleSubstitutionMappings(Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2127         if (false) {
2128             Either<Resource, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaFullElement(service.getUniqueId());
2129             if (getResourceRes.isRight()) {
2130                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(
2131                         componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), service, ComponentTypeEnum.SERVICE);
2132                 throw new ComponentException(responseFormat);
2133             }
2134             getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(getResourceRes.left().value(),
2135                     uploadResInstancesMap);
2136             if (getResourceRes.isRight()) {
2137                 ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(
2138                         componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), service, ComponentTypeEnum.SERVICE);
2139                 throw new ComponentException(responseFormat);
2140             }
2141         }
2142     }
2143
2144     protected Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2145                                                                                                     Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2146         Either<Resource, StorageOperationStatus> updateRes = null;
2147         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2148         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2149         StorageOperationStatus status = toscaOperationFacade
2150                 .deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2151         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2152             log.debug(
2153                     "Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2154                     resource.getUniqueId(), status);
2155             updateRes = Either.right(status);
2156         }
2157         if (updateRes == null) {
2158             fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap,
2159                     updatedInstCapabilities, updatedInstRequirements);
2160             status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements,
2161                     resource);
2162             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2163                 updateRes = Either.right(status);
2164             }
2165         }
2166         if (updateRes == null) {
2167             updateRes = Either.left(resource);
2168         }
2169         return updateRes;
2170     }
2171
2172     protected void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2173                                                          Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2174                                                          Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2175                                                          Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2176         componentInstances.stream().forEach(i -> {
2177             fillUpdatedInstCapabilities(updatedInstCapabilities, i,
2178                     uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2179             fillUpdatedInstRequirements(updatedInstRequirements, i,
2180                     uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2181         });
2182     }
2183
2184     protected void fillUpdatedInstCapabilities(
2185             Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2186             ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2187         Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2188         Set<String> updatedCapNames = new HashSet<>();
2189         if (MapUtils.isNotEmpty(capabilitiesNamesToUpdate)) {
2190             for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2191                 updatedCapabilities.put(requirements.getKey(),
2192                         requirements.getValue().stream()
2193                                 .filter(c -> capabilitiesNamesToUpdate.containsKey(c.getName())
2194                                         && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2195                                 .map(c -> {
2196                                     c.setParentName(c.getName());
2197                                     c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2198                                     updatedCapNames.add(c.getName());
2199                                     return c;
2200                                 }).collect(toList()));
2201             }
2202         }
2203         if (MapUtils.isNotEmpty(updatedCapabilities)) {
2204             updatedInstCapabilties.put(instance, updatedCapabilities);
2205         }
2206     }
2207
2208     protected void fillUpdatedInstRequirements(
2209             Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2210             ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2211         Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2212         Set<String> updatedReqNames = new HashSet<>();
2213         if (MapUtils.isNotEmpty(requirementsNamesToUpdate)) {
2214             for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2215                 updatedRequirements.put(requirements.getKey(),
2216                         requirements.getValue().stream()
2217                                 .filter(r -> requirementsNamesToUpdate.containsKey(r.getName())
2218                                         && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2219                                 .map(r -> {
2220                                     r.setParentName(r.getName());
2221                                     r.setName(requirementsNamesToUpdate.get(r.getName()));
2222                                     updatedReqNames.add(r.getName());
2223                                     return r;
2224                                 }).collect(toList()));
2225             }
2226         }
2227         if (MapUtils.isNotEmpty(updatedRequirements)) {
2228             updatedInstRequirements.put(instance, updatedRequirements);
2229         }
2230     }
2231
2232     protected void addRelationsToRI(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2233         for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2234             UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2235             ComponentInstance currentCompInstance = null;
2236             for (ComponentInstance compInstance : componentInstancesList) {
2237                 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2238                     currentCompInstance = compInstance;
2239                     break;
2240                 }
2241             }
2242             if (currentCompInstance == null) {
2243                 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(),
2244                         service.getUniqueId());
2245                 BeEcompErrorManager.getInstance().logInternalDataError(
2246                         COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE,
2247                         service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2248                 ResponseFormat responseFormat = componentsUtils
2249                         .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2250                 throw new ComponentException(responseFormat);
2251             }
2252
2253             ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, service, entry.getValue(), relations);
2254             if (addRelationToRiRes.getStatus() != 200) {
2255                 throw new ComponentException(addRelationToRiRes);
2256             }
2257         }
2258     }
2259
2260     protected ResponseFormat addRelationToRI(String yamlName, Service service,
2261                                            UploadComponentInstanceInfo nodesInfoValue, List<RequirementCapabilityRelDef> relations) {
2262         List<ComponentInstance> componentInstancesList = service.getComponentInstances();
2263         ComponentInstance currentCompInstance = null;
2264         for (ComponentInstance compInstance : componentInstancesList) {
2265             if (compInstance.getName().equals(nodesInfoValue.getName())) {
2266                 currentCompInstance = compInstance;
2267                 break;
2268             }
2269         }
2270         if (currentCompInstance == null) {
2271             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(),
2272                     service.getUniqueId());
2273             BeEcompErrorManager.getInstance().logInternalDataError(
2274                     COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE,
2275                     service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2276             return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE,
2277                     yamlName);
2278         }
2279         String resourceInstanceId = currentCompInstance.getUniqueId();
2280         Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2281
2282         if (regMap != null) {
2283             Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator();
2284             while (nodesRegValue.hasNext()) {
2285                 Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next();
2286                 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2287                 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2288                     log.debug("Going to create  relation {}", uploadRegInfo.getName());
2289                     String regName = uploadRegInfo.getName();
2290                     RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2291                     regCapRelDef.setFromNode(resourceInstanceId);
2292                     log.debug("try to find available requirement {} ", regName);
2293                     Either<RequirementDefinition, ResponseFormat> eitherReqStatus = serviceImportParseLogic.findAviableRequiremen(regName,
2294                             yamlName, nodesInfoValue, currentCompInstance,
2295                             uploadRegInfo.getCapabilityName());
2296                     if (eitherReqStatus.isRight()) {
2297                         log.debug("failed to find available requirement {} status is {}", regName,
2298                                 eitherReqStatus.right().value());
2299                         return eitherReqStatus.right().value();
2300                     }
2301
2302                     RequirementDefinition validReq = eitherReqStatus.left().value();
2303                     List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef
2304                             .getRelationships();
2305                     if (reqAndRelationshipPairList == null) {
2306                         reqAndRelationshipPairList = new ArrayList<>();
2307                     }
2308                     RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2309                     reqAndRelationshipPair.setRequirement(regName);
2310                     reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2311                     reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2312                     RelationshipImpl relationship = new RelationshipImpl();
2313                     relationship.setType(validReq.getCapability());
2314                     reqAndRelationshipPair.setRelationships(relationship);
2315
2316                     ComponentInstance currentCapCompInstance = null;
2317                     for (ComponentInstance compInstance : componentInstancesList) {
2318                         if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2319                             currentCapCompInstance = compInstance;
2320                             break;
2321                         }
2322                     }
2323                     if (currentCapCompInstance == null) {
2324                         log.debug("The component instance  with name {} not found on resource {} ",
2325                                 uploadRegInfo.getNode(), service.getUniqueId());
2326                         BeEcompErrorManager.getInstance().logInternalDataError(
2327                                 COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE,
2328                                 service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2329                         return componentsUtils
2330                                 .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2331                     }
2332                     regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2333                     log.debug("try to find aviable Capability  req name is {} ", validReq.getName());
2334                     CapabilityDefinition aviableCapForRel = serviceImportParseLogic.findAvailableCapabilityByTypeOrName(validReq,
2335                             currentCapCompInstance, uploadRegInfo);
2336                     reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2337                     reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2338                     reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2339                     if (aviableCapForRel == null) {
2340                         BeEcompErrorManager.getInstance().logInternalDataError(
2341                                 "aviable capability was not found. req name is " + validReq.getName()
2342                                         + " component instance is " + currentCapCompInstance.getUniqueId(),
2343                                 service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2344                         return componentsUtils
2345                                 .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2346                     }
2347                     CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2348                     capReqRel.setRelation(reqAndRelationshipPair);
2349                     reqAndRelationshipPairList.add(capReqRel);
2350                     regCapRelDef.setRelationships(reqAndRelationshipPairList);
2351                     relations.add(regCapRelDef);
2352                 }
2353             }
2354         }
2355         return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2356     }
2357
2358     protected Service getResourceAfterCreateRelations(Service service) {
2359         ComponentParametersView parametersView = serviceImportParseLogic.getComponentFilterAfterCreateRelations();
2360         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade
2361                 .getToscaElement(service.getUniqueId(), parametersView);
2362         if (eitherGetResource.isRight()) {
2363             serviceImportParseLogic.throwComponentExceptionByResource(eitherGetResource.right().value(), service);
2364         }
2365         return eitherGetResource.left().value();
2366     }
2367
2368     protected Service createServiceInstances(String yamlName, Service service,
2369                                            Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2370                                            Map<String, Resource> nodeNamespaceMap) {
2371         Either<Resource, ResponseFormat> eitherResource = null;
2372         log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
2373         if (MapUtils.isEmpty(uploadResInstancesMap)) { // PNF can have no resource instances
2374             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
2375             throw new ComponentException(responseFormat);
2376         }
2377         Map<String, Resource> existingNodeTypeMap = new HashMap<>();
2378         if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
2379             nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
2380         }
2381         Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
2382         uploadResInstancesMap
2383                 .values()
2384                 .forEach(i -> createAndAddResourceInstance(i, yamlName, service, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
2385
2386         if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
2387             try {
2388                 toscaOperationFacade.associateComponentInstancesToComponent(service,
2389                         resourcesInstancesMap, false, false);
2390             } catch (StorageException exp) {
2391                 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
2392                     log.debug("Failed to add component instances to container component {}", service.getName());
2393                     ResponseFormat responseFormat = componentsUtils
2394                             .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
2395                     eitherResource = Either.right(responseFormat);
2396                     throw new ComponentException(eitherResource.right().value());
2397                 }
2398             }
2399         }
2400         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade
2401                 .getToscaElement(service.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
2402         log.debug("*************finished to get resource {}", service.getUniqueId());
2403         if (eitherGetResource.isRight()) {
2404             ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(
2405                     componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service, ComponentTypeEnum.SERVICE);
2406             throw new ComponentException(responseFormat);
2407         }
2408         if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances())) { // PNF can have no resource instances
2409             log.debug("Error when create resource instance from csar. ComponentInstances list empty");
2410             BeEcompErrorManager.getInstance().logBeDaoSystemError(
2411                     "Error when create resource instance from csar. ComponentInstances list empty");
2412             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
2413         }
2414         return eitherGetResource.left().value();
2415     }
2416
2417     protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName,
2418                                               Resource resource, Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap, Map<ComponentInstance, Resource> resourcesInstancesMap) {
2419         Either<Resource, ResponseFormat> eitherResource;
2420         log.debug("*************Going to create  resource instances {}", uploadComponentInstanceInfo.getName());
2421         try {
2422             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2423                 uploadComponentInstanceInfo
2424                         .setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
2425             }
2426             Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo,
2427                     existingnodeTypeMap);
2428
2429             ComponentInstance componentInstance = new ComponentInstance();
2430             componentInstance.setComponentUid(refResource.getUniqueId());
2431
2432             Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
2433             if (directives != null && !directives.isEmpty()) {
2434                 componentInstance.setDirectives(new ArrayList<>(directives));
2435             }
2436             UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
2437             if (uploadNodeFilterInfo != null) {
2438                 componentInstance.setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo,
2439                         componentInstance.getUniqueId()));
2440             }
2441
2442             ComponentTypeEnum containerComponentType = resource.getComponentType();
2443             NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
2444             if (containerNodeType.equals(NodeTypeEnum.Resource)
2445                     && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())
2446                     && MapUtils.isNotEmpty(refResource.getCapabilities())) {
2447                 serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
2448                 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic.getValidComponentInstanceCapabilities(
2449                         refResource.getUniqueId(), refResource.getCapabilities(),
2450                         uploadComponentInstanceInfo.getCapabilities());
2451                 componentInstance.setCapabilities(validComponentInstanceCapabilities);
2452             }
2453             if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
2454                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE,
2455                         yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
2456                 throw new ComponentException(responseFormat);
2457             }
2458             Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
2459             componentInstance.setName(uploadComponentInstanceInfo.getName());
2460             componentInstance.setIcon(origResource.getIcon());
2461             resourcesInstancesMap.put(componentInstance, origResource);
2462         } catch (Exception e) {
2463             throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage());
2464         }
2465     }
2466
2467     protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName,
2468                                               Service service, Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap, Map<ComponentInstance, Resource> resourcesInstancesMap) {
2469         Either<Resource, ResponseFormat> eitherResource;
2470         log.debug("*************Going to create  resource instances {}", uploadComponentInstanceInfo.getName());
2471         try {
2472             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2473                 uploadComponentInstanceInfo
2474                         .setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
2475             }
2476             Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo,
2477                     existingnodeTypeMap);
2478
2479             ComponentInstance componentInstance = new ComponentInstance();
2480             componentInstance.setComponentUid(refResource.getUniqueId());
2481             Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
2482             if (directives != null && !directives.isEmpty()) {
2483                 componentInstance.setDirectives(new ArrayList<>(directives));
2484             }
2485             UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
2486             if (uploadNodeFilterInfo != null) {
2487                 componentInstance.setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo,
2488                         componentInstance.getUniqueId()));
2489             }
2490
2491             ComponentTypeEnum containerComponentType = service.getComponentType();
2492             NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
2493             if (containerNodeType.equals(NodeTypeEnum.Resource)
2494                     && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())
2495                     && MapUtils.isNotEmpty(refResource.getCapabilities())) {
2496                 serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
2497                 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic.getValidComponentInstanceCapabilities(
2498                         refResource.getUniqueId(), refResource.getCapabilities(),
2499                         uploadComponentInstanceInfo.getCapabilities());
2500                 componentInstance.setCapabilities(validComponentInstanceCapabilities);
2501             }
2502             if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
2503                 log.debug(
2504                         "createResourceInstances - not found lates version for resource instance with name {} and type ",
2505                         uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
2506                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE,
2507                         yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
2508                 throw new ComponentException(responseFormat);
2509             }
2510             Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
2511             componentInstance.setName(uploadComponentInstanceInfo.getName());
2512             componentInstance.setIcon(origResource.getIcon());
2513             resourcesInstancesMap.put(componentInstance, origResource);
2514         } catch (Exception e) {
2515             throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage());
2516         }
2517     }
2518
2519     protected Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo,
2520                                                           Map<String, Resource> nodeNamespaceMap) {
2521         Resource refResource;
2522         try {
2523             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2524                 refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
2525             } else {
2526                 Either<Resource, StorageOperationStatus> findResourceEither = toscaOperationFacade
2527                         .getLatestResourceByToscaResourceName(uploadComponentInstanceInfo.getType());
2528                 if (findResourceEither.isRight()) {
2529                     ResponseFormat responseFormat = componentsUtils.getResponseFormat(
2530                             componentsUtils.convertFromStorageResponse(findResourceEither.right().value()));
2531                     throw new ComponentException(responseFormat);
2532                 }
2533                 refResource = findResourceEither.left().value();
2534                 nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
2535             }
2536             String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
2537             if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
2538                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE,
2539                         refResource.getComponentType().getValue(), refResource.getName(), componentState);
2540                 throw new ComponentException(responseFormat);
2541             }
2542             if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.VF) {
2543                 log.debug("validateResourceInstanceBeforeCreate -  ref resource type is  ", refResource.getResourceType());
2544                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE,
2545                         yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
2546                 throw new ComponentException(responseFormat);
2547             }
2548             return refResource;
2549         } catch (Exception e) {
2550             throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage());
2551         }
2552     }
2553
2554     protected void handleServiceNodeTypes(String yamlName, Service service,
2555                                         String topologyTemplateYaml, boolean needLock,
2556                                         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2557                                         List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
2558                                         CsarInfo csarInfo, String nodeName) {
2559         try {
2560             for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
2561                 boolean isResourceNotExisted = validateResourceNotExisted(nodeTypeEntry.getKey());
2562                 if (nodeTypeEntry.getValue().isNested() && isResourceNotExisted) {
2563                     handleNestedVF(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts,
2564                             nodeTypesInfo, csarInfo, nodeTypeEntry.getKey());
2565                     log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
2566                 }
2567             }
2568             Map<String, Object> mappedToscaTemplate = null;
2569             if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo)
2570                     && nodeTypesInfo.containsKey(nodeName)) {
2571                 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
2572             }
2573             if (MapUtils.isEmpty(mappedToscaTemplate)) {
2574                 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
2575             }
2576             createResourcesFromYamlNodeTypesList(yamlName, service, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
2577                     nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
2578         } catch (ComponentException e) {
2579             throw e;
2580         } catch (StorageException e) {
2581             throw e;
2582         }catch (Exception e){
2583                     log.debug("Exception occured when handleServiceNodeTypes, error is:{}", e.getMessage(), e);
2584             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2585         }
2586     }
2587
2588     protected boolean validateResourceNotExisted(String type) {
2589         try {
2590             Either<Resource, StorageOperationStatus> latestResource = toscaOperationFacade.getLatestResourceByToscaResourceName(type);
2591             return latestResource.isRight() ? true : false;
2592         } catch (Exception e) {
2593                     log.debug("Exception occured when validateResourceNotExisted, error is:{}", e.getMessage(), e);
2594             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2595         }
2596     }
2597
2598     protected Resource handleNestedVF(Service service, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2599                                     List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
2600                                     String nodeName) {
2601         try {
2602             String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
2603             Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
2604
2605             createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false,
2606                     nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo);
2607             log.debug("************* Finished to create node types from yaml {}", yamlName);
2608
2609             if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
2610                 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
2611                 Resource resource = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo,
2612                         csarInfo, nodeName, yamlName);
2613                 return resource;
2614             }
2615             return new Resource();
2616         } catch (Exception e) {
2617             log.debug("Exception occured when handleNestedVF, error is:{}", e.getMessage(), e);
2618             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2619         }
2620     }
2621
2622     protected Resource handleNestedVfc(Service service, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2623                                      List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
2624                                      String nodeName) {
2625         try {
2626             String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
2627             Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
2628             createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false,
2629                     nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo);
2630             log.debug("************* Finished to create node types from yaml {}", yamlName);
2631
2632             if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
2633                 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
2634                 Resource resource = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo,
2635                         csarInfo, nodeName, yamlName);
2636                 return resource;
2637             }
2638             return new Resource();
2639         }  catch (Exception e) {
2640             log.debug("Exception occured when handleNestedVFc, error is:{}", e.getMessage(), e);
2641             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2642         }
2643     }
2644
2645     protected Resource handleComplexVfc(Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2646                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
2647                                       String nodeName, String yamlName) {
2648         try {
2649             Resource oldComplexVfc = null;
2650             Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(csarInfo, nodeName, nodesInfo);
2651             Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
2652                     .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
2653             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
2654                 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
2655                         serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(),
2656                                 nodeName).getRight());
2657             }
2658             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
2659                 log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ",
2660                         newComplexVfc.getToscaResourceName(), oldComplexVfcRes.right().value());
2661                 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2662             } else if (oldComplexVfcRes.isLeft()) {
2663                 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
2664                 Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic.validateNestedDerivedFromDuringUpdate(
2665                         oldComplexVfcRes.left().value(), newComplexVfc,
2666                         ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
2667                 if (eitherValidation.isLeft()) {
2668                     oldComplexVfc = oldComplexVfcRes.left().value();
2669                 }
2670             }
2671             newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName,
2672                     oldComplexVfc, newComplexVfc);
2673             csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
2674             LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(
2675                     CERTIFICATION_ON_IMPORT, LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
2676             log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
2677             final Resource result = serviceImportParseLogic
2678                     .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false,
2679                             true);
2680             csarInfo.getCreatedNodes().put(nodeName, result);
2681             csarInfo.removeNodeFromQueue();
2682             return result;
2683         } catch (Exception e) {
2684             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
2685             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2686         }
2687     }
2688
2689     protected Resource handleComplexVfc(Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2690                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
2691                                       String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) {
2692         Resource handleComplexVfcRes;
2693         try {
2694             Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
2695             String yamlContent = new String(csarInfo.getCsar().get(yamlName));
2696             Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream()
2697                     .collect(toMap(Map.Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
2698             CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
2699             if (oldComplexVfc == null) {
2700                 handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo,
2701                         csarInfo, nodesArtifactsToHandle, false, true, nodeName);
2702             } else {
2703                 handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc,
2704                         AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts, yamlContent, yamlName, csarInfo,
2705                         newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
2706             }
2707             return handleComplexVfcRes;
2708         } catch (Exception e) {
2709             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
2710             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2711         }
2712     }
2713
2714     protected Resource updateResourceFromYaml(Resource oldRresource, Resource newRresource,
2715                                             AuditingActionEnum actionEnum, List<ArtifactDefinition> createdArtifacts,
2716                                             String yamlFileName, String yamlFileContent, CsarInfo csarInfo, Map<String, NodeTypeInfo> nodeTypesInfo,
2717                                             Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2718                                             String nodeName, boolean isNested) {
2719         boolean inTransaction = true;
2720         boolean shouldLock = false;
2721         Resource preparedResource = null;
2722         ParsedToscaYamlInfo uploadComponentInstanceInfoMap = null;
2723         try {
2724             uploadComponentInstanceInfoMap = csarBusinessLogic.getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldRresource);
2725             Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
2726             if (MapUtils.isEmpty(instances) && newRresource.getResourceType() != ResourceTypeEnum.PNF) {
2727                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
2728             }
2729             preparedResource = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(),
2730                     inTransaction, shouldLock, isNested).left;
2731             log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
2732             serviceImportParseLogic.handleResourceGenericType(preparedResource);
2733             handleNodeTypes(yamlFileName, preparedResource, yamlFileContent,
2734                     shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo, csarInfo, nodeName);
2735             preparedResource = serviceImportParseLogic
2736                     .createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
2737             preparedResource = createResourceInstances(yamlFileName, preparedResource, instances, csarInfo.getCreatedNodes());
2738             preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances);
2739         } catch (ComponentException e) {
2740             ResponseFormat responseFormat = e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
2741             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
2742             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
2743             throw e;
2744         } catch (StorageException e) {
2745             ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
2746             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
2747             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
2748             throw e;
2749         }
2750         Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic
2751                 .validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(),
2752                         preparedResource.getSystemName());
2753         if (validateUpdateVfGroupNamesRes.isRight()) {
2754             throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
2755         }
2756         Map<String, GroupDefinition> groups;
2757         if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
2758             groups = validateUpdateVfGroupNamesRes.left().value();
2759         } else {
2760             groups = uploadComponentInstanceInfoMap.getGroups();
2761         }
2762         serviceImportParseLogic.handleGroupsProperties(preparedResource, groups);
2763         preparedResource = serviceImportParseLogic.updateGroupsOnResource(preparedResource, groups);
2764         NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,
2765                 nodeTypesArtifactsToHandle);
2766
2767         Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(
2768                 ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName,
2769                 csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
2770         if (updateArtifactsEither.isRight()) {
2771             log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
2772             throw new ComponentException(updateArtifactsEither.right().value());
2773         }
2774         preparedResource = serviceImportParseLogic.getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
2775
2776         ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldRresource, preparedResource);
2777         if (mergingPropsAndInputsStatus != ActionStatus.OK) {
2778             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus,
2779                     preparedResource);
2780             throw new ComponentException(responseFormat);
2781         }
2782         compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
2783         return preparedResource;
2784     }
2785
2786     protected Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml,
2787                                             String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
2788                                             Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
2789                                             boolean shouldLock, boolean inTransaction, String nodeName) {
2790         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
2791         Resource createdResource;
2792         try {
2793             ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
2794             if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
2795                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2796             }
2797             log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
2798             createdResource = createResourceAndRIsFromYaml(yamlName, resource,
2799                     parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false, createdArtifacts, topologyTemplateYaml,
2800                     nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
2801             log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
2802         } catch (ComponentException e) {
2803             ResponseFormat responseFormat = e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
2804             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
2805             throw e;
2806         } catch (StorageException e) {
2807             ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
2808             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
2809             throw e;
2810         }
2811         return createdResource;
2812     }
2813
2814     protected Resource createResourceAndRIsFromYaml(String yamlName, Resource resource,
2815                                                   ParsedToscaYamlInfo parsedToscaYamlInfo, AuditingActionEnum actionEnum, boolean isNormative,
2816                                                   List<ArtifactDefinition> createdArtifacts, String topologyTemplateYaml,
2817                                                   Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
2818                                                   Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
2819                                                   boolean shouldLock, boolean inTransaction, String nodeName) {
2820         List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
2821         if (shouldLock) {
2822             Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic.lockComponentByName(resource.getSystemName(), resource,
2823                     CREATE_RESOURCE);
2824             if (lockResult.isRight()) {
2825                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
2826                 throw new ComponentException(lockResult.right().value());
2827             }
2828             log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
2829         }
2830         try {
2831             log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
2832             Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource);
2833             resource = createResourceTransaction(resource,
2834                     csarInfo.getModifier(), isNormative);
2835             log.trace("************* Going to add inputs from yaml {}", yamlName);
2836
2837             Map<String, Object> yamlMap = ImportUtils.loadYamlAsStrictMap(csarInfo.getMainTemplateContent());
2838             Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata");
2839             String type = (String) metadata.get("type");
2840             if (resource.shouldGenerateInputs() && !"Service".equalsIgnoreCase(type))
2841                 serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
2842             Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
2843             resource = serviceImportParseLogic.createInputsOnResource(resource, inputs);
2844             Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo
2845                     .getInstances();
2846             resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap,
2847                     topologyTemplateYaml, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
2848                     nodeTypesArtifactsToCreate, nodeName);
2849             log.trace("************* Finished to create nodes, RI and Relation  from yaml {}", yamlName);
2850             // validate update vf module group names
2851             Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic
2852                     .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName());
2853             if (validateUpdateVfGroupNamesRes.isRight()) {
2854                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
2855                 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
2856             }
2857             Map<String, GroupDefinition> groups;
2858             log.trace("************* Going to add groups from yaml {}", yamlName);
2859
2860             if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
2861                 groups = validateUpdateVfGroupNamesRes.left().value();
2862             } else {
2863                 groups = parsedToscaYamlInfo.getGroups();
2864             }
2865
2866             Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource,
2867                     groups);
2868             if (createGroupsOnResource.isRight()) {
2869                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
2870                 throw new ComponentException(createGroupsOnResource.right().value());
2871             }
2872             resource = createGroupsOnResource.left().value();
2873             log.trace("************* Going to add artifacts from yaml {}", yamlName);
2874
2875             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate);
2876
2877             Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(
2878                     ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, createdArtifacts, yamlName,
2879                     csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
2880             if (createArtifactsEither.isRight()) {
2881                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
2882                 throw new ComponentException(createArtifactsEither.right().value());
2883             }
2884
2885             resource = serviceImportParseLogic.getResourceWithGroups(createArtifactsEither.left().value().getUniqueId());
2886             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
2887             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
2888             ASDCKpiApi.countCreatedResourcesKPI();
2889             return resource;
2890         } catch (ComponentException | StorageException e) {
2891             serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
2892             throw e;
2893         } finally {
2894             if (!inTransaction) {
2895                 serviceBusinessLogic.janusGraphDao.commit();
2896             }
2897             if (shouldLock) {
2898                 serviceBusinessLogic.graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
2899             }
2900         }
2901     }
2902
2903     protected Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource,
2904                                                                     Map<String, GroupDefinition> groups) {
2905         if (groups != null && !groups.isEmpty()) {
2906             List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(
2907                     groups, resource);
2908             serviceImportParseLogic.handleGroupsProperties(resource, groups);
2909             serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
2910             Either<List<GroupDefinition>, ResponseFormat> createGroups = serviceBusinessLogic.groupBusinessLogic.createGroups(resource,
2911                     groupsAsList, true);
2912             if (createGroups.isRight()) {
2913                 return Either.right(createGroups.right().value());
2914             }
2915         } else {
2916             return Either.left(resource);
2917         }
2918         Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade
2919                 .getToscaElement(resource.getUniqueId());
2920         if (updatedResource.isRight()) {
2921             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
2922                     componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource);
2923             return Either.right(responseFormat);
2924         }
2925         return Either.left(updatedResource.left().value());
2926     }
2927
2928     protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
2929         List<GroupDefinition> result = new ArrayList<>();
2930         List<ComponentInstance> componentInstances = component.getComponentInstances();
2931
2932         if (groups != null) {
2933             Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = serviceImportParseLogic.validateCyclicGroupsDependencies(groups);
2934             if (validateCyclicGroupsDependencies.isRight()) {
2935                 throw new ComponentException(validateCyclicGroupsDependencies.right().value());
2936             }
2937             for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
2938                 String groupName = entry.getKey();
2939                 GroupDefinition groupDefinition = entry.getValue();
2940                 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
2941                 updatedGroupDefinition.setMembers(null);
2942                 Map<String, String> members = groupDefinition.getMembers();
2943                 if (members != null) {
2944                     updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
2945                 }
2946                 result.add(updatedGroupDefinition);
2947             }
2948         }
2949         return result;
2950     }
2951
2952     protected void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component, List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
2953         Set<String> compInstancesNames = members.keySet();
2954         if (CollectionUtils.isEmpty(componentInstances)) {
2955             String membersAstString = compInstancesNames.stream().collect(joining(","));
2956             log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.",
2957                     membersAstString, groupName, component.getNormalizedName());
2958             throw new ComponentException(componentsUtils.getResponseFormat(
2959                     ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
2960                     component.getNormalizedName(), serviceImportParseLogic.getComponentTypeForResponse(component)));
2961         }
2962         Map<String, String> memberNames = componentInstances.stream()
2963                 .collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
2964         memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
2965         Map<String, String> relevantInstances = memberNames.entrySet().stream()
2966                 .filter(n -> compInstancesNames.contains(n.getKey()))
2967                 .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
2968
2969         if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) {
2970             List<String> foundMembers = new ArrayList<>();
2971             if (relevantInstances != null) {
2972                 foundMembers = relevantInstances.keySet().stream().collect(toList());
2973             }
2974             compInstancesNames.removeAll(foundMembers);
2975             String membersAstString = compInstancesNames.stream().collect(joining(","));
2976             throw new ComponentException(componentsUtils.getResponseFormat(
2977                     ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
2978                     component.getNormalizedName(), serviceImportParseLogic.getComponentTypeForResponse(component)));
2979         }
2980         updatedGroupDefinition.setMembers(relevantInstances);
2981     }
2982
2983     protected Resource createResourceTransaction(Resource resource, User user,
2984                                                boolean isNormative) {
2985         Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade.validateComponentNameExists(
2986                 resource.getName(), resource.getResourceType(), resource.getComponentType());
2987         if (eitherValidation.isRight()) {
2988             ResponseFormat errorResponse = componentsUtils
2989                     .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
2990             throw new ComponentException(errorResponse);
2991         }
2992         if (eitherValidation.left().value()) {
2993             log.debug("resource with name: {}, already exists", resource.getName());
2994             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
2995                     ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
2996             throw new ComponentException(errorResponse);
2997         }
2998         log.debug("send resource {} to dao for create", resource.getName());
2999         serviceImportParseLogic.createArtifactsPlaceHolderData(resource, user);
3000         if (!isNormative) {
3001             log.debug("enrich resource with creator, version and state");
3002             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
3003             resource.setVersion(INITIAL_VERSION);
3004             resource.setHighestVersion(true);
3005             if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.VF) {
3006                 resource.setAbstract(false);
3007             }
3008         }
3009         return toscaOperationFacade.createToscaComponent(resource)
3010                 .left()
3011                 .on(r -> serviceImportParseLogic.throwComponentExceptionByResource(r, resource));
3012     }
3013
3014     protected ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(
3015             Resource newResource, Resource oldResource, User user, boolean inTransaction, boolean needLock,
3016             boolean isNested) {
3017         String lockedResourceId = oldResource.getUniqueId();
3018         log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId,
3019                 oldResource.getVersion(), oldResource.getLifecycleState());
3020         ImmutablePair<Resource, ActionStatus> resourcePair = null;
3021         try {
3022             serviceBusinessLogic.lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3023             oldResource = serviceImportParseLogic
3024                     .prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3025             serviceImportParseLogic.mergeOldResourceMetadataWithNew(oldResource, newResource);
3026             serviceImportParseLogic.validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3027             serviceImportParseLogic
3028                     .validateCapabilityTypesCreate(user, serviceImportParseLogic.getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3029             newResource.setContactId(newResource.getContactId().toLowerCase());
3030             newResource.setCreatorUserId(user.getUserId());
3031             newResource.setCreatorFullName(user.getFullName());
3032             newResource.setLastUpdaterUserId(user.getUserId());
3033             newResource.setLastUpdaterFullName(user.getFullName());
3034             newResource.setUniqueId(oldResource.getUniqueId());
3035             newResource.setVersion(oldResource.getVersion());
3036             newResource.setInvariantUUID(oldResource.getInvariantUUID());
3037             newResource.setLifecycleState(oldResource.getLifecycleState());
3038             newResource.setUUID(oldResource.getUUID());
3039             newResource.setNormalizedName(oldResource.getNormalizedName());
3040             newResource.setSystemName(oldResource.getSystemName());
3041             if (oldResource.getCsarUUID() != null) {
3042                 newResource.setCsarUUID(oldResource.getCsarUUID());
3043             }
3044             if (oldResource.getImportedToscaChecksum() != null) {
3045                 newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3046             }
3047             if (newResource.getDerivedFromGenericType() == null || newResource.getDerivedFromGenericType().isEmpty()) {
3048                 newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3049             }
3050             if (newResource.getDerivedFromGenericVersion() == null || newResource.getDerivedFromGenericVersion().isEmpty()) {
3051                 newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3052             }
3053             if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) {
3054                 serviceBusinessLogic.setToscaArtifactsPlaceHolders(newResource, user);
3055             }
3056             if (newResource.getInterfaces() == null || newResource.getInterfaces().isEmpty()) {
3057                 newResource.setInterfaces(oldResource.getInterfaces());
3058             }
3059             if (CollectionUtils.isEmpty(newResource.getProperties())) {
3060                 newResource.setProperties(oldResource.getProperties());
3061             }
3062
3063             Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade
3064                     .overrideComponent(newResource, oldResource);
3065             if (overrideResource.isRight()) {
3066                 ResponseFormat responseFormat = new ResponseFormat();
3067                 serviceBusinessLogic.throwComponentException(responseFormat);
3068             }
3069             log.debug("Resource updated successfully!!!");
3070             resourcePair = new ImmutablePair<>(overrideResource.left().value(),
3071                     ActionStatus.OK);
3072             return resourcePair;
3073         } finally {
3074             if (resourcePair == null) {
3075                 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3076                 serviceBusinessLogic.janusGraphDao.rollback();
3077             } else if (!inTransaction) {
3078                 serviceBusinessLogic.janusGraphDao.commit();
3079             }
3080             if (needLock) {
3081                 log.debug("unlock resource {}", lockedResourceId);
3082                 serviceBusinessLogic.graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3083             }
3084         }
3085     }
3086
3087     protected Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Service service, Map<String, Object> mappedToscaTemplate, boolean needLock,
3088                                                                       Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3089                                                                       List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
3090                                                                       CsarInfo csarInfo) {
3091         try {
3092             Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
3093                     TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
3094             if (toscaVersion.isRight()) {
3095                 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
3096             }
3097             Map<String, Object> mapToConvert = new HashMap<>();
3098             mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
3099             Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
3100             createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, nodeTypes);
3101             return csarInfo.getCreatedNodes();
3102         } catch (Exception e) {
3103                         log.debug("Exception occured when createResourcesFromYamlNodeTypesList,error is:{}",e.getMessage(),e);
3104             throw  new ComponentException(ActionStatus.GENERAL_ERROR);
3105         }
3106     }
3107
3108     protected void createNodeTypes(String yamlName, Service service, boolean needLock, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
3109         Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
3110         Resource vfcCreated = null;
3111         while (nodesNameValueIter.hasNext()) {
3112             Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
3113             Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = nodeTypesArtifactsToHandle == null
3114                     || nodeTypesArtifactsToHandle.isEmpty() ? null
3115                     : nodeTypesArtifactsToHandle.get(nodeType.getKey());
3116
3117             if (nodeTypesInfo.containsKey(nodeType.getKey())) {
3118                 vfcCreated = handleNestedVfc(service,
3119                         nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3120                         nodeType.getKey());
3121                 log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
3122             } else if (csarInfo.getCreatedNodesToscaResourceNames() != null
3123                     && !csarInfo.getCreatedNodesToscaResourceNames().containsKey(nodeType.getKey())) {
3124                 ImmutablePair<Resource, ActionStatus> resourceCreated = serviceImportParseLogic
3125                         .createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert,
3126                                 service, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true,
3127                                 csarInfo, true);
3128                 log.debug("************* Finished to create node {}", nodeType.getKey());
3129
3130                 vfcCreated = resourceCreated.getLeft();
3131                 csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(),
3132                         vfcCreated.getName());
3133             }
3134             if (vfcCreated != null) {
3135                 csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
3136             }
3137             mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
3138         }
3139     }
3140
3141 }