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