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