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