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