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