Fix NPE on service import
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ServiceImportBusinessLogic.java
1 /*
2  * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
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             instAttributes.get(resourceInstanceId).addAll(addImplicitAttributeValues(originResource, uploadComponentInstanceInfo));
2161         }
2162         if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() == null) {
2163             instNodeFilter.put(resourceInstanceId, new UploadNodeFilterInfo());
2164         } else {
2165             instNodeFilter.put(resourceInstanceId, uploadComponentInstanceInfo.getUploadNodeFilterInfo());
2166         }
2167         if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getInterfaces())) {
2168
2169             ResponseFormat addInterfacesToRiRes = addInterfaceValuesToRi(
2170                 uploadComponentInstanceInfo,
2171                 component,
2172                 originResource,
2173                 currentCompInstance,
2174                 instInterfaces
2175             );
2176             if (addInterfacesToRiRes.getStatus() != 200) {
2177                 throw new ComponentException(addInterfacesToRiRes);
2178             }
2179         }
2180         if (originResource.getResourceType() != ResourceTypeEnum.VF) {
2181             ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, component, originResource,
2182                 currentCompInstance, instProperties, allDataTypes);
2183             if (addPropertiesValueToRiRes.getStatus() != 200) {
2184                 throw new ComponentException(addPropertiesValueToRiRes);
2185             }
2186         } else {
2187             addInputsValuesToRi(uploadComponentInstanceInfo, component, originResource, currentCompInstance, instInputs, allDataTypes);
2188         }
2189     }
2190
2191     private List<AttributeDefinition> addImplicitAttributeValues(Resource originResource, UploadComponentInstanceInfo uploadComponentInstanceInfo) {
2192         if (uploadComponentInstanceInfo.getAttributes() == null) {
2193             return  Collections.emptyList();
2194         }
2195         List<String> origAttributes = originResource.getAttributes().stream().map(AttributeDefinition::getName).collect(toList());
2196         Map<String, UploadAttributeInfo> uploadAttributes = uploadComponentInstanceInfo.getAttributes();
2197         List<String> newAttributesToAdd =
2198             uploadAttributes.keySet().stream().filter(newAttribute -> !origAttributes.contains(newAttribute))
2199                 .collect(toList());
2200         List<PropertyDefinition> propsToAddAsAttributes =
2201             originResource.getProperties().stream().filter(prop -> newAttributesToAdd.contains(prop.getName())).collect(toList());
2202         propsToAddAsAttributes.stream().forEach(prop -> {
2203             Object value = uploadAttributes.get(prop.getName()).getValue();
2204             if (value instanceof Collection<?> || value instanceof Map<?, ?>) {
2205                 Gson gson = new Gson();
2206                 String json = gson.toJson(value);
2207                 prop.setValue(json);
2208             } else {
2209                 prop.setValue(String.valueOf(value));
2210             }
2211         });
2212         List<AttributeDefinition> attributesToAdd = new ArrayList<>();
2213         for (PropertyDefinition prop: propsToAddAsAttributes) {
2214             attributesToAdd.add(getPropertyAsAttribute(prop));
2215         }
2216         return attributesToAdd;
2217     }
2218
2219     private AttributeDefinition getPropertyAsAttribute(PropertyDefinition property) {
2220         AttributeDefinition attribute = new AttributeDefinition();
2221         attribute.setName(property.getName());
2222         attribute.setType(property.getType());
2223         attribute.setSchema(property.getSchema());
2224         attribute.setValue(property.getValue());
2225         attribute.setDefaultValue(property.getDefaultValue());
2226         return attribute;
2227     }
2228
2229     protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component, Resource originResource,
2230                                        ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
2231                                        Map<String, DataTypeDefinition> allDataTypes) {
2232         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2233         try {
2234             if (MapUtils.isNotEmpty(propMap)) {
2235                 Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
2236                 List<ComponentInstanceInput> instPropList = new ArrayList<>();
2237                 if (CollectionUtils.isEmpty(originResource.getInputs())) {
2238                     log.debug("failed to find properties ");
2239                     throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
2240                 }
2241                 originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p));
2242                 for (List<UploadPropInfo> propertyList : propMap.values()) {
2243                     processProperty(component, allDataTypes, currPropertiesMap, instPropList, propertyList);
2244                 }
2245                 currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
2246                 instInputs.put(currentCompInstance.getUniqueId(), instPropList);
2247             }
2248         } catch (Exception e) {
2249             log.debug("failed to add Inputs Values To Ri");
2250             throw new ComponentException(ActionStatus.GENERAL_ERROR);
2251         }
2252     }
2253
2254     protected void processProperty(Component component, Map<String, DataTypeDefinition> allDataTypes,
2255                                    Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
2256                                    List<UploadPropInfo> propertyList) {
2257         UploadPropInfo propertyInfo = propertyList.get(0);
2258         String propName = propertyInfo.getName();
2259         if (!currPropertiesMap.containsKey(propName)) {
2260             log.debug("failed to find property {} ", propName);
2261             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName));
2262         }
2263         processProperty(allDataTypes, currPropertiesMap, instPropList, propertyInfo, propName, component.getInputs());
2264     }
2265
2266     protected void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs,
2267                                    GetInputValueDataDefinition getInputIndex) {
2268         Optional<InputDefinition> optional;
2269         if (getInputIndex != null) {
2270             optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny();
2271             if (!optional.isPresent()) {
2272                 log.debug("Failed to find input {} ", getInputIndex.getInputName());
2273                 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2274             }
2275             InputDefinition inputIndex = optional.get();
2276             getInputIndex.setInputId(inputIndex.getUniqueId());
2277             getInputValues.add(getInputIndex);
2278         }
2279     }
2280
2281     private void addAttributeValueToResourceInstance(Map<String, List<AttributeDefinition>> instAttributes,
2282                                                      Map<String, UploadAttributeInfo> attributeMap) {
2283         if (attributeMap == null) {
2284             return;
2285         }
2286         attributeMap.forEach((attributeName, attributeValue) -> instAttributes.values()
2287             .forEach(value -> value.stream().filter(attr -> attr.getName().equals(attributeName)).forEach(attr -> {
2288                 if (attributeValue.getValue() instanceof Collection<?> || attributeValue.getValue() instanceof Map<?, ?>) {
2289                     Gson gson = new Gson();
2290                     String json = gson.toJson(attributeValue.getValue());
2291                     attr.setValue(json);
2292                 } else {
2293                     attr.setValue(String.valueOf(attributeValue.getValue()));
2294                 }
2295             })));
2296     }
2297
2298     protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component,
2299                                                    Resource originResource, ComponentInstance currentCompInstance,
2300                                                    Map<String, List<ComponentInstanceProperty>> instProperties,
2301                                                    Map<String, DataTypeDefinition> allDataTypes) {
2302         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2303         Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
2304         List<PropertyDefinition> originalPropertyList = originResource.getProperties();
2305         if (MapUtils.isNotEmpty(propMap) && CollectionUtils.isEmpty(originalPropertyList)) {
2306             log.debug("failed to find properties ");
2307             return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
2308         }
2309         if (CollectionUtils.isEmpty(originalPropertyList)) {
2310             return componentsUtils.getResponseFormat(ActionStatus.OK);
2311         }
2312         originalPropertyList.stream()
2313             .filter(property -> !currPropertiesMap.containsKey(property.getName()))
2314             .forEach(property -> currPropertiesMap.put(property.getName(), property));
2315         List<ComponentInstanceProperty> instPropList = new ArrayList<>();
2316         if (MapUtils.isNotEmpty(propMap)) {
2317             for (final List<UploadPropInfo> propertyList : propMap.values()) {
2318                 UploadPropInfo propertyInfo = propertyList.get(0);
2319                 String propName = propertyInfo.getName();
2320                 if (!currPropertiesMap.containsKey(propName)) {
2321                     log.debug("failed to find property {} ", propName);
2322                     return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName);
2323                 }
2324                 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
2325                 String value = null;
2326                 final List<GetInputValueDataDefinition> getInputs = new ArrayList<>();
2327                 boolean isValidate = true;
2328                 if (propertyInfo.getValue() != null) {
2329                     getInputs.addAll(propertyInfo.getGet_input());
2330                     isValidate = getInputs.isEmpty();
2331                     if (isValidate) {
2332                         value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
2333                     } else {
2334                         value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2335                     }
2336                 }
2337                 final var property = new ComponentInstanceProperty(curPropertyDef, value, null);
2338                 String validatedPropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, true, allDataTypes);
2339
2340                 addSubPropertyYamlToscaFunctions(validatedPropValue, value, property.getType(), propertyInfo, allDataTypes);
2341
2342                 if (CollectionUtils.isNotEmpty(propertyInfo.getSubPropertyToscaFunctions())) {
2343                     validatedPropValue = value;
2344                 }
2345
2346                 property.setValue(validatedPropValue);
2347
2348                 if (tryHandlingAsYamlToscaFunction(validatedPropValue, value, propertyInfo)) {
2349                     try {
2350                         final Object yamlValue = new Yaml().loadAs(value, Object.class);
2351                         CustomYamlFunction toscaFunction = new CustomYamlFunction();
2352                         toscaFunction.setYamlValue(yamlValue);
2353                         property.setToscaFunction(toscaFunction);
2354                     } catch (Exception exception) {
2355                         log.info("Cannot create YAML value for {}", propName);
2356                     }
2357                 } else {
2358                     property.setToscaFunction(propertyInfo.getToscaFunction());
2359                 }
2360                 property.setSubPropertyToscaFunctions(propertyInfo.getSubPropertyToscaFunctions());
2361                 if (!getInputs.isEmpty() && CollectionUtils.isEmpty(property.getSubPropertyToscaFunctions())) {
2362                     final List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
2363                     for (final GetInputValueDataDefinition getInput : getInputs) {
2364                         final List<InputDefinition> inputs = component.getInputs();
2365                         if (inputs == null || inputs.isEmpty()) {
2366                             log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
2367                             serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
2368                                 property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
2369                         }
2370                         InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput);
2371                         getInput.setInputId(input.getUniqueId());
2372                         getInputValues.add(getInput);
2373                         GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
2374                         if (getInputIndex != null) {
2375                             input = serviceImportParseLogic.findInputByName(inputs, getInputIndex);
2376                             getInputIndex.setInputId(input.getUniqueId());
2377                             getInputValues.add(getInputIndex);
2378                         }
2379                     }
2380                     property.setGetInputValues(getInputValues);
2381                 }
2382                 instPropList.add(property);
2383                 currPropertiesMap.remove(property.getName());
2384             }
2385         }
2386         if (!currPropertiesMap.isEmpty()) {
2387             for (PropertyDefinition value : currPropertiesMap.values()) {
2388                 instPropList.add(new ComponentInstanceProperty(value));
2389             }
2390         }
2391         instProperties.put(currentCompInstance.getUniqueId(), instPropList);
2392         return componentsUtils.getResponseFormat(ActionStatus.OK);
2393     }
2394
2395     private boolean tryHandlingAsYamlToscaFunction(String validatedPropValue, String value, UploadPropInfo propertyInfo) {
2396         return StringUtils.isEmpty(validatedPropValue) && StringUtils.isNotEmpty(value) && propertyInfo.getToscaFunction() == null
2397             && CollectionUtils.isEmpty(propertyInfo.getSubPropertyToscaFunctions());
2398     }
2399
2400     private void addSubPropertyYamlToscaFunctions(final String validatedPropValue, final String value, final String propertyType,
2401                                                   final UploadPropInfo propertyInfo, final Map<String, DataTypeDefinition> allDataTypes) {
2402         if (StringUtils.isNotEmpty(validatedPropValue) || StringUtils.isEmpty(value) || ToscaPropertyType.isValidType(propertyType) != null) {
2403             return;
2404         }
2405         try {
2406             final JsonObject jsonObject = JsonParser.parseString(value).getAsJsonObject();
2407
2408             final DataTypeDefinition dataTypeDefinition = allDataTypes.get(propertyType);
2409             final List<String> propertyNames =
2410                 dataTypeDefinition.getProperties().stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2411
2412             boolean hasSubPropertyValues = jsonObject.entrySet().stream().allMatch(entry -> propertyNames.contains(entry.getKey()));
2413
2414             if (hasSubPropertyValues) {
2415                 for (final PropertyDefinition prop : dataTypeDefinition.getProperties()) {
2416                     if (propertyInfo.getSubPropertyToscaFunctions().stream()
2417                         .anyMatch(subPropertyToscaFunction -> subPropertyToscaFunction.getSubPropertyPath().get(0).equals(prop.getName()))) {
2418                         continue;
2419                     }
2420                     Optional<SubPropertyToscaFunction> subPropertyToscaFunction = createSubPropertyYamlToscaFunction(jsonObject, prop, allDataTypes);
2421                     if (subPropertyToscaFunction.isPresent()) {
2422                         propertyInfo.getSubPropertyToscaFunctions().add(subPropertyToscaFunction.get());
2423                     }
2424                 }
2425             }
2426         } catch (Exception exception) {
2427             log.info("Cannot create YAML value for {}", value);
2428         }
2429     }
2430
2431     private Optional<SubPropertyToscaFunction> createSubPropertyYamlToscaFunction(final JsonObject jsonObject, final PropertyDefinition prop,
2432                                                                                   final Map<String, DataTypeDefinition> allDataTypes) {
2433         JsonElement propJsonElement = jsonObject.get(prop.getName());
2434         if (propJsonElement != null) {
2435             final String subPropValue = propJsonElement.toString();
2436             final ComponentInstanceProperty subProperty = new ComponentInstanceProperty(prop, subPropValue, null);
2437             final String validateSubPropValue =
2438                 serviceBusinessLogic.validatePropValueBeforeCreate(subProperty, subPropValue, true, allDataTypes);
2439
2440             if (StringUtils.isEmpty(validateSubPropValue) && StringUtils.isNotEmpty(subPropValue)) {
2441                 try {
2442                     Object yamlValue = new Yaml().loadAs(subPropValue, Object.class);
2443                     SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
2444                     CustomYamlFunction toscaFunction = new CustomYamlFunction();
2445                     toscaFunction.setYamlValue(yamlValue);
2446                     subPropertyToscaFunction.setToscaFunction(toscaFunction);
2447                     subPropertyToscaFunction.setSubPropertyPath(Collections.singletonList(prop.getName()));
2448                     return Optional.of(subPropertyToscaFunction);
2449                 } catch (Exception exception) {
2450                     log.info("Cannot create YAML value for {}", subPropValue);
2451                 }
2452             }
2453         }
2454         return Optional.empty();
2455     }
2456
2457     protected ResponseFormat addInterfaceValuesToRi(
2458         UploadComponentInstanceInfo uploadComponentInstanceInfo,
2459         Component component,
2460         Resource originResource, ComponentInstance currentCompInstance,
2461         Map<String, Map<String, InterfaceDefinition>> instInterfaces
2462     ) {
2463         Map<String, UploadInterfaceInfo> instanceInterfacesMap = uploadComponentInstanceInfo.getInterfaces();
2464         Map<String, InterfaceDefinition> currInterfacesMap = new HashMap<>();
2465         Map<String, InterfaceDefinition> interfacesFromNodeType = originResource.getInterfaces();
2466         if ((MapUtils.isNotEmpty(instanceInterfacesMap)) && (MapUtils.isEmpty(interfacesFromNodeType))) {
2467             log.debug("failed to find interfaces ");
2468             return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT);
2469         }
2470         if (interfacesFromNodeType == null || interfacesFromNodeType.isEmpty()) {
2471             return componentsUtils.getResponseFormat(ActionStatus.OK);
2472         }
2473         for (Map.Entry<String, InterfaceDefinition> entryInstances : interfacesFromNodeType.entrySet()) {
2474             String interfaceName = entryInstances.getKey().substring(entryInstances.getKey().lastIndexOf(".") + 1);
2475             if (!currInterfacesMap.containsKey(interfaceName)) {
2476                 currInterfacesMap.put(interfaceName, entryInstances.getValue());
2477             }
2478         }
2479
2480         Map<String, InterfaceDefinition> instInterfacesMap = new HashMap<>();
2481         if (MapUtils.isNotEmpty(instanceInterfacesMap)) {
2482             for (UploadInterfaceInfo uploadInterfaceInfo : instanceInterfacesMap.values()) {
2483                 String interfaceName = uploadInterfaceInfo.getName();
2484                 if (!currInterfacesMap.containsKey(interfaceName)) {
2485                     log.debug("failed to find interface {} ", interfaceName);
2486                     return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceName);
2487                 }
2488                 InterfaceDefinition currentInterfaceDef = currInterfacesMap.get(interfaceName);
2489                 Map<String, OperationDataDefinition> operationsToAdd = new HashMap<>();
2490
2491                 Map<String, OperationDataDefinition> operations = uploadInterfaceInfo.getOperations();
2492                 for (Map.Entry<String, OperationDataDefinition> operation : operations.entrySet()) {
2493                     OperationDataDefinition instanceOperation = operation.getValue();
2494                     OperationDataDefinition templateOperation = currentInterfaceDef.getOperationsMap()
2495                         .getOrDefault(operation.getKey(), new Operation(instanceOperation));
2496                     //Inputs
2497                     ListDataDefinition<OperationInputDefinition> instanceInputs = instanceOperation.getInputs();
2498                     if (null != instanceInputs) {
2499                         mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs);
2500                         component.getProperties()
2501                             .forEach(property -> instanceInputs.getListToscaDataDefinition().stream()
2502                                 .filter(instanceInput ->
2503                                     instanceInput.getToscaFunction() instanceof ToscaGetFunctionDataDefinition &&
2504                                         property.getName().equals(instanceInput.getToscaFunction() != null ?
2505                                             ((ToscaGetFunctionDataDefinition) instanceInput.getToscaFunction()).getPropertyName() : null))
2506                                 .forEach(oldInput -> oldInput.setType(property.getType()))
2507                             );
2508                         templateOperation.setInputs(instanceInputs);
2509                     }
2510                     //Implementation
2511                     templateOperation.setImplementation(instanceOperation.getImplementation());
2512                     //Description
2513                     templateOperation.setDescription(instanceOperation.getDescription());
2514                     operationsToAdd.put(operation.getKey(), templateOperation);
2515                 }
2516                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
2517                 interfaceDef.setModel(component.getModel());
2518                 interfaceDef.setType(currentInterfaceDef.getType());
2519                 interfaceDef.setUniqueId(currentInterfaceDef.getType());
2520                 interfaceDef.setDescription(uploadInterfaceInfo.getDescription());
2521                 interfaceDef.setOperations(operationsToAdd);
2522                 instInterfacesMap.put(currentInterfaceDef.getType(), interfaceDef);
2523                 currInterfacesMap.remove(interfaceName);
2524             }
2525         }
2526         if (!currInterfacesMap.isEmpty()) {
2527             for (InterfaceDefinition value : currInterfacesMap.values()) {
2528                 instInterfacesMap.put(value.getUniqueId(), value);
2529             }
2530         }
2531         instInterfaces.put(currentCompInstance.getUniqueId(), instInterfacesMap);
2532         return componentsUtils.getResponseFormat(ActionStatus.OK);
2533     }
2534
2535     private void mergeOperationInputDefinitions(ListDataDefinition<OperationInputDefinition> inputsFromNodeType,
2536                                                 ListDataDefinition<OperationInputDefinition> instanceInputs) {
2537         if (inputsFromNodeType == null || CollectionUtils.isEmpty(inputsFromNodeType.getListToscaDataDefinition()) || instanceInputs == null
2538             || CollectionUtils.isEmpty(instanceInputs.getListToscaDataDefinition())) {
2539             return;
2540         }
2541         instanceInputs.getListToscaDataDefinition().forEach(
2542             instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().filter(
2543                 templateInput -> templateInput.getName().equals(instanceInput.getName())
2544             ).forEach(
2545                 newInstanceInput -> {
2546                     instanceInput.setSourceProperty(newInstanceInput.getSourceProperty());
2547                     instanceInput.setSource(newInstanceInput.getSource());
2548                     instanceInput.setType(newInstanceInput.getType());
2549                 }
2550             )
2551         );
2552         instanceInputs.getListToscaDataDefinition().stream()
2553             .filter(instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().noneMatch(
2554                 inputFromNodeType -> inputFromNodeType.getName().equals(instanceInput.getName())
2555             ))
2556             .forEach(oldInput -> oldInput.setType("string"));
2557     }
2558
2559     protected void processComponentInstanceCapabilities(Map<String, DataTypeDefinition> allDataTypes,
2560                                                         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2561                                                         UploadComponentInstanceInfo uploadComponentInstanceInfo,
2562                                                         ComponentInstance currentCompInstance, Resource originResource) {
2563         log.debug("enter processComponentInstanceCapabilities");
2564         Map<String, List<CapabilityDefinition>> originCapabilities;
2565         if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2566             originCapabilities = new HashMap<>();
2567             Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2568             originResource.getCapabilities().forEach((k, v) -> serviceImportParseLogic.addCapabilities(originCapabilities, k, v));
2569             uploadComponentInstanceInfo.getCapabilities().values()
2570                 .forEach(l -> serviceImportParseLogic.addCapabilitiesProperties(newPropertiesMap, l));
2571             updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
2572         } else {
2573             originCapabilities = originResource.getCapabilities();
2574         }
2575         instCapabilties.put(currentCompInstance, originCapabilities);
2576     }
2577
2578     protected void updateCapabilityPropertiesValues(Map<String, DataTypeDefinition> allDataTypes,
2579                                                     Map<String, List<CapabilityDefinition>> originCapabilities,
2580                                                     Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
2581         originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
2582             .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes));
2583     }
2584
2585     protected void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2586                                         Map<String, DataTypeDefinition> allDataTypes) {
2587         properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2588     }
2589
2590     protected String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2591                                          Map<String, DataTypeDefinition> allDataTypes) {
2592         String value = null;
2593         List<GetInputValueDataDefinition> getInputs = null;
2594         boolean isValidate = true;
2595         if (null != propertyInfo && propertyInfo.getValue() != null) {
2596             getInputs = propertyInfo.getGet_input();
2597             isValidate = getInputs == null || getInputs.isEmpty();
2598             if (isValidate) {
2599                 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2600             } else {
2601                 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2602             }
2603         }
2604         property.setValue(value);
2605         return serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2606     }
2607
2608     protected Resource getOriginResource(String yamlName, Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2609         Resource originResource;
2610         log.debug("after enter ServiceImportBusinessLogic processComponentInstance, enter getOriginResource");
2611         if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2612             Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2613                 .getToscaFullElement(currentCompInstance.getComponentUid());
2614             if (getOriginResourceRes.isRight()) {
2615                 ResponseFormat responseFormat = componentsUtils
2616                     .getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName);
2617                 throw new ComponentException(responseFormat);
2618             }
2619             originResource = getOriginResourceRes.left().value();
2620             originCompMap.put(originResource.getUniqueId(), originResource);
2621         } else {
2622             originResource = originCompMap.get(currentCompInstance.getComponentUid());
2623         }
2624         return originResource;
2625     }
2626
2627     protected Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2628                                                                                                       Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2629         Either<Resource, StorageOperationStatus> updateRes = null;
2630         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2631         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2632         StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2633         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2634             log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2635                 resource.getUniqueId(), status);
2636             updateRes = Either.right(status);
2637         }
2638         if (updateRes == null) {
2639             fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
2640                 updatedInstRequirements);
2641             status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
2642             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2643                 updateRes = Either.right(status);
2644             }
2645         }
2646         if (updateRes == null) {
2647             updateRes = Either.left(resource);
2648         }
2649         return updateRes;
2650     }
2651
2652     protected void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2653                                                            Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2654                                                            Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2655                                                            Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2656         componentInstances.stream().forEach(i -> {
2657             fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2658             fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2659         });
2660     }
2661
2662     protected void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2663                                                ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2664         Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2665         Set<String> updatedCapNames = new HashSet<>();
2666         if (MapUtils.isNotEmpty(capabilitiesNamesToUpdate)) {
2667             for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2668                 updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
2669                         c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2670                     .map(c -> {
2671                         c.setParentName(c.getName());
2672                         c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2673                         updatedCapNames.add(c.getName());
2674                         return c;
2675                     }).collect(toList()));
2676             }
2677         }
2678         if (MapUtils.isNotEmpty(updatedCapabilities)) {
2679             updatedInstCapabilties.put(instance, updatedCapabilities);
2680         }
2681     }
2682
2683     protected void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2684                                                ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2685         Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2686         Set<String> updatedReqNames = new HashSet<>();
2687         if (MapUtils.isNotEmpty(requirementsNamesToUpdate)) {
2688             for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2689                 updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
2690                         r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2691                     .map(r -> {
2692                         r.setParentName(r.getName());
2693                         r.setName(requirementsNamesToUpdate.get(r.getName()));
2694                         updatedReqNames.add(r.getName());
2695                         return r;
2696                     }).collect(toList()));
2697             }
2698         }
2699         if (MapUtils.isNotEmpty(updatedRequirements)) {
2700             updatedInstRequirements.put(instance, updatedRequirements);
2701         }
2702     }
2703
2704     protected void addRelationsToRI(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2705                                     List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2706         for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2707             UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2708             ComponentInstance currentCompInstance = null;
2709             for (ComponentInstance compInstance : componentInstancesList) {
2710                 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2711                     currentCompInstance = compInstance;
2712                     break;
2713                 }
2714             }
2715             if (currentCompInstance == null) {
2716                 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), service.getUniqueId());
2717                 BeEcompErrorManager.getInstance()
2718                     .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, service.getUniqueId(),
2719                         BeEcompErrorManager.ErrorSeverity.ERROR);
2720                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2721                 throw new ComponentException(responseFormat);
2722             }
2723             ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, service, entry.getValue(), relations);
2724             if (addRelationToRiRes.getStatus() != 200) {
2725                 throw new ComponentException(addRelationToRiRes);
2726             }
2727         }
2728     }
2729
2730     protected ResponseFormat addRelationToRI(String yamlName, Service service, UploadComponentInstanceInfo nodesInfoValue,
2731                                              List<RequirementCapabilityRelDef> relations) {
2732         List<ComponentInstance> componentInstancesList = service.getComponentInstances();
2733         ComponentInstance currentCompInstance = null;
2734         for (ComponentInstance compInstance : componentInstancesList) {
2735             if (compInstance.getName().equals(nodesInfoValue.getName())) {
2736                 currentCompInstance = compInstance;
2737                 break;
2738             }
2739         }
2740         if (currentCompInstance == null) {
2741             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), service.getUniqueId());
2742             BeEcompErrorManager.getInstance()
2743                 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, service.getUniqueId(),
2744                     BeEcompErrorManager.ErrorSeverity.ERROR);
2745             return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2746         }
2747         String resourceInstanceId = currentCompInstance.getUniqueId();
2748         Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2749         if (regMap != null) {
2750             Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator();
2751             while (nodesRegValue.hasNext()) {
2752                 Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next();
2753                 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2754                 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2755                     log.debug("Going to create  relation {}", uploadRegInfo.getName());
2756                     String regName = uploadRegInfo.getName();
2757                     RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2758                     regCapRelDef.setFromNode(resourceInstanceId);
2759                     log.debug("try to find available requirement {} ", regName);
2760                     Either<RequirementDefinition, ResponseFormat> eitherReqStatus = serviceImportParseLogic
2761                         .findAvailableRequirement(regName, yamlName, nodesInfoValue, currentCompInstance, uploadRegInfo.getCapabilityName());
2762                     if (eitherReqStatus.isRight()) {
2763                         log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
2764                         return eitherReqStatus.right().value();
2765                     }
2766                     RequirementDefinition validReq = eitherReqStatus.left().value();
2767                     List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
2768                     if (reqAndRelationshipPairList == null) {
2769                         reqAndRelationshipPairList = new ArrayList<>();
2770                     }
2771                     RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2772                     reqAndRelationshipPair.setRequirement(regName);
2773                     reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2774                     reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2775                     RelationshipImpl relationship = new RelationshipImpl();
2776                     relationship.setType(validReq.getCapability());
2777                     reqAndRelationshipPair.setRelationships(relationship);
2778                     ComponentInstance currentCapCompInstance = null;
2779                     for (ComponentInstance compInstance : componentInstancesList) {
2780                         if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2781                             currentCapCompInstance = compInstance;
2782                             break;
2783                         }
2784                     }
2785                     if (currentCapCompInstance == null) {
2786                         log.debug("The component instance  with name {} not found on resource {} ", uploadRegInfo.getNode(), service.getUniqueId());
2787                         BeEcompErrorManager.getInstance()
2788                             .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, service.getUniqueId(),
2789                                 BeEcompErrorManager.ErrorSeverity.ERROR);
2790                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2791                     }
2792                     regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2793                     log.debug("try to find aviable Capability  req name is {} ", validReq.getName());
2794                     CapabilityDefinition aviableCapForRel = serviceImportParseLogic
2795                         .findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
2796                     if (aviableCapForRel == null) {
2797                         BeEcompErrorManager.getInstance().logInternalDataError(
2798                             "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
2799                                 .getUniqueId(), service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2800                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2801                     }
2802                     reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2803                     reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2804                     reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2805                     CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2806                     capReqRel.setRelation(reqAndRelationshipPair);
2807                     if (StringUtils.isNotEmpty(uploadRegInfo.getRelationshipTemplate())) {
2808                         capReqRel.setOperations(getOperations(nodesInfoValue.getOperations(), uploadRegInfo.getRelationshipTemplate()));
2809                     }
2810                     reqAndRelationshipPairList.add(capReqRel);
2811                     regCapRelDef.setRelationships(reqAndRelationshipPairList);
2812                     relations.add(regCapRelDef);
2813                 }
2814             }
2815         }
2816         return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2817     }
2818
2819     private List<OperationUi> getOperations(final Map<String, List<OperationUi>> operations, final String relationshipTemplate) {
2820         final List<OperationUi> operationUiList = new ArrayList<>();
2821         operations.forEach((operationKey, operationValues) -> {
2822             if (operationKey.equals(relationshipTemplate)) {
2823                 operationUiList.addAll(operationValues);
2824             }
2825         });
2826         return operationUiList;
2827     }
2828
2829     protected Service getResourceAfterCreateRelations(Service service) {
2830         ComponentParametersView parametersView = serviceImportParseLogic.getComponentFilterAfterCreateRelations();
2831         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId(), parametersView);
2832         if (eitherGetResource.isRight()) {
2833             serviceImportParseLogic.throwComponentExceptionByResource(eitherGetResource.right().value(), service);
2834         }
2835         return eitherGetResource.left().value();
2836     }
2837
2838     protected Service createServiceInstances(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2839                                              Map<String, Resource> nodeNamespaceMap) {
2840         Either<Resource, ResponseFormat> eitherResource = null;
2841         log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
2842         if (MapUtils.isEmpty(uploadResInstancesMap)) { // PNF can have no resource instances
2843             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
2844             throw new ComponentException(responseFormat);
2845         }
2846         Map<String, Resource> existingNodeTypeMap = new HashMap<>();
2847         if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
2848             nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
2849         }
2850         Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
2851         uploadResInstancesMap.values()
2852             .forEach(i -> createAndAddResourceInstance(i, yamlName, service, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
2853         if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
2854             try {
2855                 toscaOperationFacade.associateComponentInstancesToComponent(service, resourcesInstancesMap, false, false);
2856             } catch (StorageException exp) {
2857                 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
2858                     log.debug("Failed to add component instances to container component {}", service.getName());
2859                     ResponseFormat responseFormat = componentsUtils
2860                         .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
2861                     eitherResource = Either.right(responseFormat);
2862                     throw new ComponentException(eitherResource.right().value());
2863                 }
2864             }
2865         }
2866         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade
2867             .getToscaElement(service.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
2868         log.debug("*************finished to get resource {}", service.getUniqueId());
2869         if (eitherGetResource.isRight()) {
2870             ResponseFormat responseFormat = componentsUtils
2871                 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service,
2872                     ComponentTypeEnum.SERVICE);
2873             throw new ComponentException(responseFormat);
2874         }
2875         if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances())) { // PNF can have no resource instances
2876             log.debug("Error when create resource instance from csar. ComponentInstances list empty");
2877             BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
2878             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
2879         }
2880         return eitherGetResource.left().value();
2881     }
2882
2883     protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Component component,
2884                                                 Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
2885                                                 Map<ComponentInstance, Resource> resourcesInstancesMap) {
2886         log.debug("*************Going to create  resource instances {}", uploadComponentInstanceInfo.getName());
2887         try {
2888             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2889                 uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
2890             }
2891             Resource refResource =
2892                 validateResourceInstanceBeforeCreate(yamlName, component.getModel(), uploadComponentInstanceInfo, existingnodeTypeMap);
2893             ComponentInstance componentInstance = new ComponentInstance();
2894             componentInstance.setComponentUid(refResource.getUniqueId());
2895             Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
2896             if (directives != null && !directives.isEmpty()) {
2897                 componentInstance.setDirectives(new ArrayList<>(directives));
2898             }
2899             UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
2900             if (uploadNodeFilterInfo != null) {
2901                 componentInstance
2902                     .setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
2903             }
2904             ComponentTypeEnum containerComponentType = component.getComponentType();
2905             NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
2906             if (containerNodeType.equals(NodeTypeEnum.Resource) && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && MapUtils
2907                 .isNotEmpty(refResource.getCapabilities())) {
2908                 serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
2909                 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic
2910                     .getValidComponentInstanceCapabilities(refResource.getUniqueId(), refResource.getCapabilities(),
2911                         uploadComponentInstanceInfo.getCapabilities());
2912                 componentInstance.setCapabilities(validComponentInstanceCapabilities);
2913             }
2914             if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
2915                 ResponseFormat responseFormat = componentsUtils
2916                     .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2917                         uploadComponentInstanceInfo.getType());
2918                 throw new ComponentException(responseFormat);
2919             }
2920             Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
2921             componentInstance.setName(uploadComponentInstanceInfo.getName());
2922             componentInstance.setIcon(origResource.getIcon());
2923             componentInstance.setMinOccurrences(uploadComponentInstanceInfo.getMinOccurrences());
2924             componentInstance.setMaxOccurrences(uploadComponentInstanceInfo.getMaxOccurrences());
2925             componentInstance.setInstanceCount(uploadComponentInstanceInfo.getInstanceCount());
2926             resourcesInstancesMap.put(componentInstance, origResource);
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 Resource validateResourceInstanceBeforeCreate(String yamlName, String model, UploadComponentInstanceInfo uploadComponentInstanceInfo,
2935                                                             Map<String, Resource> nodeNamespaceMap) {
2936         Resource refResource;
2937         try {
2938             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2939                 refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
2940             } else {
2941                 final Either<Component, StorageOperationStatus> resourceEither =
2942                     toscaOperationFacade.getLatestByToscaResourceName(uploadComponentInstanceInfo.getType(), model);
2943                 if (resourceEither.isRight()) {
2944                     ResponseFormat responseFormat = componentsUtils
2945                         .getResponseFormat(componentsUtils.convertFromStorageResponse(resourceEither.right().value()));
2946                     throw new ComponentException(responseFormat);
2947                 }
2948                 refResource = (Resource) resourceEither.left().value();
2949                 nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
2950             }
2951             String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
2952             if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
2953                 ResponseFormat responseFormat = componentsUtils
2954                     .getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(), refResource.getName(),
2955                         componentState);
2956                 throw new ComponentException(responseFormat);
2957             }
2958             if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.VF) {
2959                 log.debug("validateResourceInstanceBeforeCreate -  ref resource type is  ", refResource.getResourceType());
2960                 ResponseFormat responseFormat = componentsUtils
2961                     .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2962                         uploadComponentInstanceInfo.getType());
2963                 throw new ComponentException(responseFormat);
2964             }
2965             return refResource;
2966         } catch (final ComponentException e) {
2967             throw e;
2968         } catch (final Exception e) {
2969             throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage());
2970         }
2971     }
2972
2973     protected void handleServiceNodeTypes(String yamlName, Service service, String topologyTemplateYaml, boolean needLock,
2974                                           Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2975                                           List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
2976                                           CsarInfo csarInfo, String nodeName) {
2977         try {
2978             for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
2979                 boolean isResourceNotExisted = validateResourceNotExisted(nodeTypeEntry.getKey());
2980                 if (nodeTypeEntry.getValue().isNested() && isResourceNotExisted) {
2981                     handleNestedVF(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
2982                         nodeTypeEntry.getKey());
2983                     log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
2984                 }
2985             }
2986             Map<String, Object> mappedToscaTemplate = null;
2987             if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) && nodeTypesInfo
2988                 .containsKey(nodeName)) {
2989                 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
2990             }
2991             if (MapUtils.isEmpty(mappedToscaTemplate)) {
2992                 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
2993             }
2994             createResourcesFromYamlNodeTypesList(yamlName, service, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
2995                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
2996         } catch (ComponentException | StorageException e) {
2997             throw e;
2998         } catch (Exception e) {
2999             log.debug("Exception occured when handleServiceNodeTypes, error is:{}", e.getMessage(), e);
3000             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3001         }
3002     }
3003
3004     protected boolean validateResourceNotExisted(String type) {
3005         try {
3006             Either<Resource, StorageOperationStatus> latestResource = toscaOperationFacade.getLatestResourceByToscaResourceName(type);
3007             return latestResource.isRight();
3008         } catch (Exception e) {
3009             log.debug("Exception occured when validateResourceNotExisted, error is:{}", e.getMessage(), e);
3010             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3011         }
3012     }
3013
3014     protected Resource handleNestedVF(Service service,
3015                                       Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3016                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
3017                                       String nodeName) {
3018         try {
3019             String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
3020             Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
3021             createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
3022                 csarInfo);
3023             log.debug("************* Finished to create node types from yaml {}", yamlName);
3024             if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
3025                 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
3026                 return handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
3027             }
3028             return new Resource();
3029         } catch (Exception e) {
3030             log.debug("Exception occured when handleNestedVF, error is:{}", e.getMessage(), e);
3031             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3032         }
3033     }
3034
3035     protected Resource handleComplexVfc(
3036         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3037         List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName) {
3038         try {
3039             Resource oldComplexVfc = null;
3040             Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(csarInfo, nodeName, nodesInfo);
3041             Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
3042                 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
3043             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
3044                 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
3045                     serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName)
3046                         .getRight());
3047             }
3048             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
3049                 log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
3050                     oldComplexVfcRes.right().value());
3051                 throw new ComponentException(ActionStatus.GENERAL_ERROR);
3052             } else if (oldComplexVfcRes.isLeft()) {
3053                 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
3054                 Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic
3055                     .validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(), newComplexVfc,
3056                         ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
3057                 if (eitherValidation.isLeft()) {
3058                     oldComplexVfc = oldComplexVfcRes.left().value();
3059                 }
3060             }
3061             newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
3062                 newComplexVfc);
3063             csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
3064             LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
3065                 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
3066             log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
3067             final Resource result = serviceImportParseLogic
3068                 .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
3069             csarInfo.getCreatedNodes().put(nodeName, result);
3070             csarInfo.removeNodeFromQueue();
3071             return result;
3072         } catch (Exception e) {
3073             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
3074             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3075         }
3076     }
3077
3078     protected Resource handleComplexVfc(
3079         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3080         List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName,
3081         Resource oldComplexVfc, Resource newComplexVfc) {
3082         Resource handleComplexVfcRes;
3083         try {
3084             Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
3085             String yamlContent = new String(csarInfo.getCsar().get(yamlName));
3086             Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream()
3087                 .collect(toMap(Map.Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
3088             CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
3089             if (oldComplexVfc == null) {
3090                 handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
3091                     false, true, nodeName);
3092             } else {
3093                 handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA,
3094                     createdArtifacts, yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
3095             }
3096             return handleComplexVfcRes;
3097         } catch (Exception e) {
3098             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
3099             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3100         }
3101     }
3102
3103     protected Resource updateResourceFromYaml(Resource oldRresource, Resource newRresource, AuditingActionEnum actionEnum,
3104                                               List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent,
3105                                               CsarInfo csarInfo, Map<String, NodeTypeInfo> nodeTypesInfo,
3106                                               Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3107                                               String nodeName, boolean isNested) {
3108         boolean inTransaction = true;
3109         boolean shouldLock = false;
3110         Resource preparedResource = null;
3111         ParsedToscaYamlInfo uploadComponentInstanceInfoMap = null;
3112         try {
3113             uploadComponentInstanceInfoMap = csarBusinessLogic
3114                 .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldRresource);
3115             Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
3116             if (MapUtils.isEmpty(instances) && newRresource.getResourceType() != ResourceTypeEnum.PNF) {
3117                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
3118             }
3119             preparedResource = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(), inTransaction, shouldLock,
3120                 isNested).left;
3121             log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
3122             serviceImportParseLogic.handleResourceGenericType(preparedResource);
3123             handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
3124                 csarInfo, nodeName);
3125             preparedResource = serviceImportParseLogic.createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
3126             preparedResource = createResourceInstances(yamlFileName, preparedResource, instances, csarInfo.getCreatedNodes());
3127             preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances);
3128         } catch (ComponentException e) {
3129             ResponseFormat responseFormat =
3130                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3131             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3132             componentsUtils
3133                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3134             throw e;
3135         } catch (StorageException e) {
3136             ResponseFormat responseFormat = componentsUtils
3137                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3138             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3139             componentsUtils
3140                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3141             throw e;
3142         }
3143         Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3144             uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
3145         if (validateUpdateVfGroupNamesRes.isRight()) {
3146             throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3147         }
3148         Map<String, GroupDefinition> groups;
3149         if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3150             groups = validateUpdateVfGroupNamesRes.left().value();
3151         } else {
3152             groups = uploadComponentInstanceInfoMap.getGroups();
3153         }
3154         serviceImportParseLogic.handleGroupsProperties(preparedResource, groups);
3155         preparedResource = serviceImportParseLogic.updateGroupsOnResource(preparedResource, groups);
3156         NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
3157         Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE,
3158             createdArtifacts, yamlFileName, csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3159         if (updateArtifactsEither.isRight()) {
3160             log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
3161             throw new ComponentException(updateArtifactsEither.right().value());
3162         }
3163         preparedResource = serviceImportParseLogic.getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
3164         ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldRresource, preparedResource);
3165         if (mergingPropsAndInputsStatus != ActionStatus.OK) {
3166             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
3167             throw new ComponentException(responseFormat);
3168         }
3169         compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
3170         return preparedResource;
3171     }
3172
3173     protected Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName,
3174                                               Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3175                                               Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3176                                               boolean shouldLock, boolean inTransaction, String nodeName) {
3177         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
3178         Resource createdResource;
3179         try {
3180             ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
3181                 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
3182             if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
3183                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
3184             }
3185             log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
3186             createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
3187                 createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
3188             log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
3189         } catch (ComponentException e) {
3190             ResponseFormat responseFormat =
3191                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3192             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3193             throw e;
3194         } catch (StorageException e) {
3195             ResponseFormat responseFormat = componentsUtils
3196                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3197             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3198             throw e;
3199         }
3200         return createdResource;
3201     }
3202
3203     protected Resource createResourceAndRIsFromYaml(String yamlName, Resource resource, ParsedToscaYamlInfo parsedToscaYamlInfo,
3204                                                     AuditingActionEnum actionEnum, boolean isNormative, List<ArtifactDefinition> createdArtifacts,
3205                                                     String topologyTemplateYaml, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3206                                                     Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3207                                                     boolean shouldLock, boolean inTransaction, String nodeName) {
3208         List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
3209         if (shouldLock) {
3210             Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic
3211                 .lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
3212             if (lockResult.isRight()) {
3213                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3214                 throw new ComponentException(lockResult.right().value());
3215             }
3216             log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
3217         }
3218         try {
3219             log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
3220             Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource);
3221             resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
3222             log.trace("************* Going to add inputs from yaml {}", yamlName);
3223             Map<String, Object> yamlMap = ImportUtils.loadYamlAsStrictMap(csarInfo.getMainTemplateContent());
3224             Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata");
3225             String type = (String) metadata.get("type");
3226             if (resource.shouldGenerateInputs() && !"Service".equalsIgnoreCase(type)) {
3227                 serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
3228             }
3229             Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
3230             resource = serviceImportParseLogic.createInputsOnResource(resource, inputs);
3231             Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
3232             resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, topologyTemplateYaml,
3233                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
3234             log.trace("************* Finished to create nodes, RI and Relation  from yaml {}", yamlName);
3235             // validate update vf module group names
3236             Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3237                 parsedToscaYamlInfo.getGroups(), resource.getSystemName());
3238             if (validateUpdateVfGroupNamesRes.isRight()) {
3239                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3240                 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3241             }
3242             Map<String, GroupDefinition> groups;
3243             log.trace("************* Going to add groups from yaml {}", yamlName);
3244             if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3245                 groups = validateUpdateVfGroupNamesRes.left().value();
3246             } else {
3247                 groups = parsedToscaYamlInfo.getGroups();
3248             }
3249             Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
3250             if (createGroupsOnResource.isRight()) {
3251                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3252                 throw new ComponentException(createGroupsOnResource.right().value());
3253             }
3254             resource = createGroupsOnResource.left().value();
3255             log.trace("************* Going to add artifacts from yaml {}", yamlName);
3256             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate);
3257             Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE,
3258                 createdArtifacts, yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3259             if (createArtifactsEither.isRight()) {
3260                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3261                 throw new ComponentException(createArtifactsEither.right().value());
3262             }
3263             resource = serviceImportParseLogic.getResourceWithGroups(createArtifactsEither.left().value().getUniqueId());
3264             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
3265             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
3266             ASDCKpiApi.countCreatedResourcesKPI();
3267             return resource;
3268         } catch (ComponentException | StorageException e) {
3269             serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3270             throw e;
3271         } finally {
3272             if (!inTransaction) {
3273                 janusGraphDao.commit();
3274             }
3275             if (shouldLock) {
3276                 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
3277             }
3278         }
3279     }
3280
3281     protected Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
3282         if (groups != null && !groups.isEmpty()) {
3283             List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
3284             serviceImportParseLogic.handleGroupsProperties(resource, groups);
3285             serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
3286             Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
3287             if (createGroups.isRight()) {
3288                 return Either.right(createGroups.right().value());
3289             }
3290         } else {
3291             return Either.left(resource);
3292         }
3293         Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
3294         if (updatedResource.isRight()) {
3295             ResponseFormat responseFormat = componentsUtils
3296                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource);
3297             return Either.right(responseFormat);
3298         }
3299         return Either.left(updatedResource.left().value());
3300     }
3301
3302     protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
3303         List<GroupDefinition> result = new ArrayList<>();
3304         List<ComponentInstance> componentInstances = component.getComponentInstances();
3305         if (groups != null) {
3306             for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
3307                 String groupName = entry.getKey();
3308                 GroupDefinition groupDefinition = entry.getValue();
3309                 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
3310                 updatedGroupDefinition.setMembers(null);
3311                 Map<String, String> members = groupDefinition.getMembers();
3312                 if (members != null) {
3313                     updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
3314                 }
3315                 result.add(updatedGroupDefinition);
3316             }
3317         }
3318         return result;
3319     }
3320
3321     protected void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
3322                                       List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
3323         Set<String> compInstancesNames = members.keySet();
3324         if (CollectionUtils.isEmpty(componentInstances)) {
3325             String membersAstString = compInstancesNames.stream().collect(joining(","));
3326             log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
3327                 groupName, component.getNormalizedName());
3328             throw new ComponentException(componentsUtils
3329                 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3330                     serviceImportParseLogic.getComponentTypeForResponse(component)));
3331         }
3332         Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
3333         memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
3334         Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
3335             .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
3336         if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) {
3337             List<String> foundMembers = new ArrayList<>();
3338             if (relevantInstances != null) {
3339                 foundMembers = relevantInstances.keySet().stream().collect(toList());
3340             }
3341             compInstancesNames.removeAll(foundMembers);
3342             String membersAstString = compInstancesNames.stream().collect(joining(","));
3343             throw new ComponentException(componentsUtils
3344                 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3345                     serviceImportParseLogic.getComponentTypeForResponse(component)));
3346         }
3347         updatedGroupDefinition.setMembers(relevantInstances);
3348     }
3349
3350     protected Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
3351         Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
3352             .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
3353         if (eitherValidation.isRight()) {
3354             ResponseFormat errorResponse = componentsUtils
3355                 .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
3356             throw new ComponentException(errorResponse);
3357         }
3358         if (Boolean.TRUE.equals(eitherValidation.left().value())) {
3359             log.debug("resource with name: {}, already exists", resource.getName());
3360             ResponseFormat errorResponse = componentsUtils
3361                 .getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
3362             throw new ComponentException(errorResponse);
3363         }
3364         log.debug("send resource {} to dao for create", resource.getName());
3365         serviceImportParseLogic.createArtifactsPlaceHolderData(resource, user);
3366         if (!isNormative) {
3367             log.debug("enrich resource with creator, version and state");
3368             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
3369             resource.setVersion(INITIAL_VERSION);
3370             resource.setHighestVersion(true);
3371             if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.VF) {
3372                 resource.setAbstract(false);
3373             }
3374         }
3375         return toscaOperationFacade.createToscaComponent(resource).left()
3376             .on(r -> serviceImportParseLogic.throwComponentExceptionByResource(r, resource));
3377     }
3378
3379     protected ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
3380                                                                                    boolean inTransaction, boolean needLock, boolean isNested) {
3381         String lockedResourceId = oldResource.getUniqueId();
3382         log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
3383             oldResource.getLifecycleState());
3384         ImmutablePair<Resource, ActionStatus> resourcePair = null;
3385         try {
3386             serviceBusinessLogic.lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3387             oldResource = serviceImportParseLogic.prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3388             serviceImportParseLogic.mergeOldResourceMetadataWithNew(oldResource, newResource);
3389             serviceImportParseLogic.validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3390             serviceImportParseLogic.validateCapabilityTypesCreate(user, serviceImportParseLogic.getCapabilityTypeOperation(), newResource,
3391                 AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3392             createNewResourceToOldResource(newResource, oldResource, user);
3393             Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
3394             if (overrideResource.isRight()) {
3395                 ResponseFormat responseFormat = new ResponseFormat();
3396                 serviceBusinessLogic.throwComponentException(responseFormat);
3397             }
3398             log.debug("Resource updated successfully!!!");
3399             resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
3400             return resourcePair;
3401         } finally {
3402             if (resourcePair == null) {
3403                 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3404                 janusGraphDao.rollback();
3405             } else if (!inTransaction) {
3406                 janusGraphDao.commit();
3407             }
3408             if (needLock) {
3409                 log.debug("unlock resource {}", lockedResourceId);
3410                 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3411             }
3412         }
3413     }
3414
3415     protected void createNewResourceToOldResource(Resource newResource, Resource oldResource, User user) {
3416         newResource.setContactId(newResource.getContactId().toLowerCase());
3417         newResource.setCreatorUserId(user.getUserId());
3418         newResource.setCreatorFullName(user.getFullName());
3419         newResource.setLastUpdaterUserId(user.getUserId());
3420         newResource.setLastUpdaterFullName(user.getFullName());
3421         newResource.setUniqueId(oldResource.getUniqueId());
3422         newResource.setVersion(oldResource.getVersion());
3423         newResource.setInvariantUUID(oldResource.getInvariantUUID());
3424         newResource.setLifecycleState(oldResource.getLifecycleState());
3425         newResource.setUUID(oldResource.getUUID());
3426         newResource.setNormalizedName(oldResource.getNormalizedName());
3427         newResource.setSystemName(oldResource.getSystemName());
3428         if (oldResource.getCsarUUID() != null) {
3429             newResource.setCsarUUID(oldResource.getCsarUUID());
3430         }
3431         if (oldResource.getCsarVersionId() != null) {
3432             newResource.setCsarVersionId(oldResource.getCsarVersionId());
3433         }
3434         if (oldResource.getImportedToscaChecksum() != null) {
3435             newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3436         }
3437         if (newResource.getDerivedFromGenericType() == null || newResource.getDerivedFromGenericType().isEmpty()) {
3438             newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3439         }
3440         if (newResource.getDerivedFromGenericVersion() == null || newResource.getDerivedFromGenericVersion().isEmpty()) {
3441             newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3442         }
3443         if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) {
3444             serviceBusinessLogic.setToscaArtifactsPlaceHolders(newResource, user);
3445         }
3446         if (newResource.getInterfaces() == null || newResource.getInterfaces().isEmpty()) {
3447             newResource.setInterfaces(oldResource.getInterfaces());
3448         }
3449         if (CollectionUtils.isEmpty(newResource.getProperties())) {
3450             newResource.setProperties(oldResource.getProperties());
3451         }
3452         if (newResource.getModel() == null) {
3453             newResource.setModel(oldResource.getModel());
3454         }
3455     }
3456
3457     protected Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Service service, Map<String, Object> mappedToscaTemplate,
3458                                                                          boolean needLock,
3459                                                                          Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3460                                                                          List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
3461                                                                          Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
3462         try {
3463             Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
3464                 TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
3465             if (toscaVersion.isRight()) {
3466                 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
3467             }
3468             Map<String, Object> mapToConvert = new HashMap<>();
3469             mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
3470             Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
3471             createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3472                 mapToConvert, nodeTypes);
3473             return csarInfo.getCreatedNodes();
3474         } catch (Exception e) {
3475             log.debug("Exception occured when createResourcesFromYamlNodeTypesList,error is:{}", e.getMessage(), e);
3476             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3477         }
3478     }
3479
3480     protected void createNodeTypes(String yamlName, Service service, boolean needLock,
3481                                    Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3482                                    List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3483                                    Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
3484         Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
3485         Resource vfcCreated = null;
3486         while (nodesNameValueIter.hasNext()) {
3487             Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
3488             String nodeTypeKey = nodeType.getKey();
3489             Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
3490                 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeTypeKey);
3491             if (nodeTypesInfo.containsKey(nodeTypeKey)) {
3492                 vfcCreated = handleNestedVfc(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3493                     nodeTypeKey);
3494                 log.trace("************* Finished to handle nested vfc {}", nodeTypeKey);
3495             } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
3496                 .containsKey(nodeTypeKey)) {
3497                 ImmutablePair<Resource, ActionStatus> resourceCreated = serviceImportParseLogic
3498                     .createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, service, needLock,
3499                         nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
3500                 log.debug("************* Finished to create node {}", nodeTypeKey);
3501                 vfcCreated = resourceCreated.getLeft();
3502                 csarInfo.getCreatedNodesToscaResourceNames().put(nodeTypeKey, vfcCreated.getName());
3503             }
3504             if (vfcCreated != null) {
3505                 csarInfo.getCreatedNodes().put(nodeTypeKey, vfcCreated);
3506             }
3507             mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
3508         }
3509     }
3510 }