Unable to import service template with interface
[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 final InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
225     private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
226     private final ModelOperation modelOperation;
227     private ApplicationDataTypeCache applicationDataTypeCache;
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 (interfacesFromNodeType == null) {
2467             interfacesFromNodeType = new HashMap<>();
2468         }
2469         if (MapUtils.isEmpty(instanceInterfacesMap) && MapUtils.isEmpty(instanceInterfacesMap)) {
2470             log.debug("failed to find interfaces ");
2471             return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT);
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                 InterfaceDefinition currentInterfaceDef;
2485                 if (!currInterfacesMap.containsKey(interfaceName)) {
2486                     currentInterfaceDef = getInterfaceDef(interfaceName, component.getModel());
2487                     if (currentInterfaceDef == null) {
2488                         log.debug("failed to find interface {} ", interfaceName);
2489                         return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceName);
2490                     }
2491                 } else {
2492                     currentInterfaceDef = currInterfacesMap.get(interfaceName);
2493                 }
2494                 Map<String, OperationDataDefinition> operationsToAdd = new HashMap<>();
2495
2496                 Map<String, OperationDataDefinition> operations = uploadInterfaceInfo.getOperations();
2497                 for (Map.Entry<String, OperationDataDefinition> operation : operations.entrySet()) {
2498                     OperationDataDefinition instanceOperation = operation.getValue();
2499                     OperationDataDefinition templateOperation = currentInterfaceDef.getOperationsMap()
2500                         .getOrDefault(operation.getKey(), new Operation(instanceOperation));
2501                     //Inputs
2502                     ListDataDefinition<OperationInputDefinition> instanceInputs = instanceOperation.getInputs();
2503                     if (null != instanceInputs) {
2504                         mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs);
2505                         component.getProperties()
2506                             .forEach(property -> instanceInputs.getListToscaDataDefinition().stream()
2507                                 .filter(instanceInput ->
2508                                     instanceInput.getToscaFunction() instanceof ToscaGetFunctionDataDefinition &&
2509                                         property.getName().equals(instanceInput.getToscaFunction() != null ?
2510                                             ((ToscaGetFunctionDataDefinition) instanceInput.getToscaFunction()).getPropertyName() : null))
2511                                 .forEach(oldInput -> oldInput.setType(property.getType()))
2512                             );
2513                         templateOperation.setInputs(instanceInputs);
2514                     }
2515                     //Implementation
2516                     templateOperation.setImplementation(instanceOperation.getImplementation());
2517                     //Description
2518                     templateOperation.setDescription(instanceOperation.getDescription());
2519                     templateOperation.setMilestones(instanceOperation.getMilestones());
2520                     operationsToAdd.put(operation.getKey(), templateOperation);
2521                 }
2522                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
2523                 interfaceDef.setModel(component.getModel());
2524                 interfaceDef.setType(currentInterfaceDef.getType());
2525                 interfaceDef.setUniqueId(currentInterfaceDef.getType());
2526                 interfaceDef.setDescription(uploadInterfaceInfo.getDescription());
2527                 interfaceDef.setOperations(operationsToAdd);
2528                 instInterfacesMap.put(currentInterfaceDef.getType(), interfaceDef);
2529                 currInterfacesMap.remove(interfaceName);
2530             }
2531         }
2532         if (!currInterfacesMap.isEmpty()) {
2533             for (InterfaceDefinition value : currInterfacesMap.values()) {
2534                 instInterfacesMap.put(value.getUniqueId(), value);
2535             }
2536         }
2537         instInterfaces.put(currentCompInstance.getUniqueId(), instInterfacesMap);
2538         return componentsUtils.getResponseFormat(ActionStatus.OK);
2539     }
2540
2541     private InterfaceDefinition getInterfaceDef(String interfaceName, String model) {
2542         Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypesEither =
2543             interfaceLifecycleTypeOperation.getAllInterfaceLifecycleTypes(model);
2544         if (interfaceLifecycleTypesEither.isRight()) {
2545             return null;
2546         }
2547         Map<String, InterfaceDefinition> interfaceLifecycleTypes = interfaceLifecycleTypesEither.left().value();
2548         Optional<InterfaceDefinition> interfaceType =
2549             interfaceLifecycleTypes.values().stream().filter(interfaceDef -> interfaceDef.getUniqueId().contains(interfaceName)).findFirst();
2550         if (interfaceType.isEmpty()) {
2551             return null;
2552         }
2553         return interfaceType.get();
2554     }
2555
2556     private void mergeOperationInputDefinitions(ListDataDefinition<OperationInputDefinition> inputsFromNodeType,
2557                                                 ListDataDefinition<OperationInputDefinition> instanceInputs) {
2558         if (inputsFromNodeType == null || CollectionUtils.isEmpty(inputsFromNodeType.getListToscaDataDefinition()) || instanceInputs == null
2559             || CollectionUtils.isEmpty(instanceInputs.getListToscaDataDefinition())) {
2560             return;
2561         }
2562         instanceInputs.getListToscaDataDefinition().forEach(
2563             instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().filter(
2564                 templateInput -> templateInput.getName().equals(instanceInput.getName())
2565             ).forEach(
2566                 newInstanceInput -> {
2567                     instanceInput.setSourceProperty(newInstanceInput.getSourceProperty());
2568                     instanceInput.setSource(newInstanceInput.getSource());
2569                     instanceInput.setType(newInstanceInput.getType());
2570                 }
2571             )
2572         );
2573         instanceInputs.getListToscaDataDefinition().stream()
2574             .filter(instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().noneMatch(
2575                 inputFromNodeType -> inputFromNodeType.getName().equals(instanceInput.getName())
2576             ))
2577             .forEach(oldInput -> oldInput.setType("string"));
2578     }
2579
2580     protected void processComponentInstanceCapabilities(Map<String, DataTypeDefinition> allDataTypes,
2581                                                         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2582                                                         UploadComponentInstanceInfo uploadComponentInstanceInfo,
2583                                                         ComponentInstance currentCompInstance, Resource originResource) {
2584         log.debug("enter processComponentInstanceCapabilities");
2585         Map<String, List<CapabilityDefinition>> originCapabilities;
2586         if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2587             originCapabilities = new HashMap<>();
2588             Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2589             originResource.getCapabilities().forEach((k, v) -> serviceImportParseLogic.addCapabilities(originCapabilities, k, v));
2590             uploadComponentInstanceInfo.getCapabilities().values()
2591                 .forEach(l -> serviceImportParseLogic.addCapabilitiesProperties(newPropertiesMap, l));
2592             updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
2593         } else {
2594             originCapabilities = originResource.getCapabilities();
2595         }
2596         instCapabilties.put(currentCompInstance, originCapabilities);
2597     }
2598
2599     protected void updateCapabilityPropertiesValues(Map<String, DataTypeDefinition> allDataTypes,
2600                                                     Map<String, List<CapabilityDefinition>> originCapabilities,
2601                                                     Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
2602         originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
2603             .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes));
2604     }
2605
2606     protected void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2607                                         Map<String, DataTypeDefinition> allDataTypes) {
2608         properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2609     }
2610
2611     protected String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2612                                          Map<String, DataTypeDefinition> allDataTypes) {
2613         String value = null;
2614         List<GetInputValueDataDefinition> getInputs = null;
2615         boolean isValidate = true;
2616         if (null != propertyInfo && propertyInfo.getValue() != null) {
2617             getInputs = propertyInfo.getGet_input();
2618             isValidate = getInputs == null || getInputs.isEmpty();
2619             if (isValidate) {
2620                 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2621             } else {
2622                 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2623             }
2624         }
2625         property.setValue(value);
2626         return serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2627     }
2628
2629     protected Resource getOriginResource(String yamlName, Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2630         Resource originResource;
2631         log.debug("after enter ServiceImportBusinessLogic processComponentInstance, enter getOriginResource");
2632         if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2633             Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2634                 .getToscaFullElement(currentCompInstance.getComponentUid());
2635             if (getOriginResourceRes.isRight()) {
2636                 ResponseFormat responseFormat = componentsUtils
2637                     .getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName);
2638                 throw new ComponentException(responseFormat);
2639             }
2640             originResource = getOriginResourceRes.left().value();
2641             originCompMap.put(originResource.getUniqueId(), originResource);
2642         } else {
2643             originResource = originCompMap.get(currentCompInstance.getComponentUid());
2644         }
2645         return originResource;
2646     }
2647
2648     protected Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2649                                                                                                       Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2650         Either<Resource, StorageOperationStatus> updateRes = null;
2651         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2652         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2653         StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2654         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2655             log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2656                 resource.getUniqueId(), status);
2657             updateRes = Either.right(status);
2658         }
2659         if (updateRes == null) {
2660             fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
2661                 updatedInstRequirements);
2662             status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
2663             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2664                 updateRes = Either.right(status);
2665             }
2666         }
2667         if (updateRes == null) {
2668             updateRes = Either.left(resource);
2669         }
2670         return updateRes;
2671     }
2672
2673     protected void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2674                                                            Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2675                                                            Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2676                                                            Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2677         componentInstances.stream().forEach(i -> {
2678             fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2679             fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2680         });
2681     }
2682
2683     protected void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2684                                                ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2685         Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2686         Set<String> updatedCapNames = new HashSet<>();
2687         if (MapUtils.isNotEmpty(capabilitiesNamesToUpdate)) {
2688             for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2689                 updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
2690                         c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2691                     .map(c -> {
2692                         c.setParentName(c.getName());
2693                         c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2694                         updatedCapNames.add(c.getName());
2695                         return c;
2696                     }).collect(toList()));
2697             }
2698         }
2699         if (MapUtils.isNotEmpty(updatedCapabilities)) {
2700             updatedInstCapabilties.put(instance, updatedCapabilities);
2701         }
2702     }
2703
2704     protected void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2705                                                ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2706         Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2707         Set<String> updatedReqNames = new HashSet<>();
2708         if (MapUtils.isNotEmpty(requirementsNamesToUpdate)) {
2709             for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2710                 updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
2711                         r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2712                     .map(r -> {
2713                         r.setParentName(r.getName());
2714                         r.setName(requirementsNamesToUpdate.get(r.getName()));
2715                         updatedReqNames.add(r.getName());
2716                         return r;
2717                     }).collect(toList()));
2718             }
2719         }
2720         if (MapUtils.isNotEmpty(updatedRequirements)) {
2721             updatedInstRequirements.put(instance, updatedRequirements);
2722         }
2723     }
2724
2725     protected void addRelationsToRI(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2726                                     List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2727         for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2728             UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2729             ComponentInstance currentCompInstance = null;
2730             for (ComponentInstance compInstance : componentInstancesList) {
2731                 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2732                     currentCompInstance = compInstance;
2733                     break;
2734                 }
2735             }
2736             if (currentCompInstance == null) {
2737                 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), service.getUniqueId());
2738                 BeEcompErrorManager.getInstance()
2739                     .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, service.getUniqueId(),
2740                         BeEcompErrorManager.ErrorSeverity.ERROR);
2741                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2742                 throw new ComponentException(responseFormat);
2743             }
2744             ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, service, entry.getValue(), relations);
2745             if (addRelationToRiRes.getStatus() != 200) {
2746                 throw new ComponentException(addRelationToRiRes);
2747             }
2748         }
2749     }
2750
2751     protected ResponseFormat addRelationToRI(String yamlName, Service service, UploadComponentInstanceInfo nodesInfoValue,
2752                                              List<RequirementCapabilityRelDef> relations) {
2753         List<ComponentInstance> componentInstancesList = service.getComponentInstances();
2754         ComponentInstance currentCompInstance = null;
2755         for (ComponentInstance compInstance : componentInstancesList) {
2756             if (compInstance.getName().equals(nodesInfoValue.getName())) {
2757                 currentCompInstance = compInstance;
2758                 break;
2759             }
2760         }
2761         if (currentCompInstance == null) {
2762             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), service.getUniqueId());
2763             BeEcompErrorManager.getInstance()
2764                 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, service.getUniqueId(),
2765                     BeEcompErrorManager.ErrorSeverity.ERROR);
2766             return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2767         }
2768         String resourceInstanceId = currentCompInstance.getUniqueId();
2769         Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2770         if (regMap != null) {
2771             Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator();
2772             while (nodesRegValue.hasNext()) {
2773                 Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next();
2774                 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2775                 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2776                     log.debug("Going to create  relation {}", uploadRegInfo.getName());
2777                     String regName = uploadRegInfo.getName();
2778                     RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2779                     regCapRelDef.setFromNode(resourceInstanceId);
2780                     log.debug("try to find available requirement {} ", regName);
2781                     Either<RequirementDefinition, ResponseFormat> eitherReqStatus = serviceImportParseLogic
2782                         .findAvailableRequirement(regName, yamlName, nodesInfoValue, currentCompInstance, uploadRegInfo.getCapabilityName());
2783                     if (eitherReqStatus.isRight()) {
2784                         log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
2785                         return eitherReqStatus.right().value();
2786                     }
2787                     RequirementDefinition validReq = eitherReqStatus.left().value();
2788                     List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
2789                     if (reqAndRelationshipPairList == null) {
2790                         reqAndRelationshipPairList = new ArrayList<>();
2791                     }
2792                     RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2793                     reqAndRelationshipPair.setRequirement(regName);
2794                     reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2795                     reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2796                     RelationshipImpl relationship = new RelationshipImpl();
2797                     relationship.setType(validReq.getCapability());
2798                     reqAndRelationshipPair.setRelationships(relationship);
2799                     ComponentInstance currentCapCompInstance = null;
2800                     for (ComponentInstance compInstance : componentInstancesList) {
2801                         if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2802                             currentCapCompInstance = compInstance;
2803                             break;
2804                         }
2805                     }
2806                     if (currentCapCompInstance == null) {
2807                         log.debug("The component instance  with name {} not found on resource {} ", uploadRegInfo.getNode(), service.getUniqueId());
2808                         BeEcompErrorManager.getInstance()
2809                             .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, service.getUniqueId(),
2810                                 BeEcompErrorManager.ErrorSeverity.ERROR);
2811                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2812                     }
2813                     regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2814                     log.debug("try to find aviable Capability  req name is {} ", validReq.getName());
2815                     CapabilityDefinition aviableCapForRel = serviceImportParseLogic
2816                         .findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
2817                     if (aviableCapForRel == null) {
2818                         BeEcompErrorManager.getInstance().logInternalDataError(
2819                             "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
2820                                 .getUniqueId(), service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2821                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2822                     }
2823                     reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2824                     reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2825                     reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2826                     CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2827                     capReqRel.setRelation(reqAndRelationshipPair);
2828                     if (StringUtils.isNotEmpty(uploadRegInfo.getRelationshipTemplate())) {
2829                         capReqRel.setOperations(getOperations(nodesInfoValue.getOperations(), uploadRegInfo.getRelationshipTemplate()));
2830                     }
2831                     reqAndRelationshipPairList.add(capReqRel);
2832                     regCapRelDef.setRelationships(reqAndRelationshipPairList);
2833                     relations.add(regCapRelDef);
2834                 }
2835             }
2836         }
2837         return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2838     }
2839
2840     private List<OperationUi> getOperations(final Map<String, List<OperationUi>> operations, final String relationshipTemplate) {
2841         final List<OperationUi> operationUiList = new ArrayList<>();
2842         operations.forEach((operationKey, operationValues) -> {
2843             if (operationKey.equals(relationshipTemplate)) {
2844                 operationUiList.addAll(operationValues);
2845             }
2846         });
2847         return operationUiList;
2848     }
2849
2850     protected Service getResourceAfterCreateRelations(Service service) {
2851         ComponentParametersView parametersView = serviceImportParseLogic.getComponentFilterAfterCreateRelations();
2852         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId(), parametersView);
2853         if (eitherGetResource.isRight()) {
2854             serviceImportParseLogic.throwComponentExceptionByResource(eitherGetResource.right().value(), service);
2855         }
2856         return eitherGetResource.left().value();
2857     }
2858
2859     protected Service createServiceInstances(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2860                                              Map<String, Resource> nodeNamespaceMap) {
2861         Either<Resource, ResponseFormat> eitherResource = null;
2862         log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
2863         if (MapUtils.isEmpty(uploadResInstancesMap)) { // PNF can have no resource instances
2864             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
2865             throw new ComponentException(responseFormat);
2866         }
2867         Map<String, Resource> existingNodeTypeMap = new HashMap<>();
2868         if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
2869             nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
2870         }
2871         Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
2872         uploadResInstancesMap.values()
2873             .forEach(i -> createAndAddResourceInstance(i, yamlName, service, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
2874         if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
2875             try {
2876                 toscaOperationFacade.associateComponentInstancesToComponent(service, resourcesInstancesMap, false, false);
2877             } catch (StorageException exp) {
2878                 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
2879                     log.debug("Failed to add component instances to container component {}", service.getName());
2880                     ResponseFormat responseFormat = componentsUtils
2881                         .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
2882                     eitherResource = Either.right(responseFormat);
2883                     throw new ComponentException(eitherResource.right().value());
2884                 }
2885             }
2886         }
2887         Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade
2888             .getToscaElement(service.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
2889         log.debug("*************finished to get resource {}", service.getUniqueId());
2890         if (eitherGetResource.isRight()) {
2891             ResponseFormat responseFormat = componentsUtils
2892                 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service,
2893                     ComponentTypeEnum.SERVICE);
2894             throw new ComponentException(responseFormat);
2895         }
2896         if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances())) { // PNF can have no resource instances
2897             log.debug("Error when create resource instance from csar. ComponentInstances list empty");
2898             BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
2899             throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
2900         }
2901         return eitherGetResource.left().value();
2902     }
2903
2904     protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Component component,
2905                                                 Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
2906                                                 Map<ComponentInstance, Resource> resourcesInstancesMap) {
2907         log.debug("*************Going to create  resource instances {}", uploadComponentInstanceInfo.getName());
2908         try {
2909             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2910                 uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
2911             }
2912             Resource refResource =
2913                 validateResourceInstanceBeforeCreate(yamlName, component.getModel(), uploadComponentInstanceInfo, existingnodeTypeMap);
2914             ComponentInstance componentInstance = new ComponentInstance();
2915             componentInstance.setComponentUid(refResource.getUniqueId());
2916             Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
2917             if (directives != null && !directives.isEmpty()) {
2918                 componentInstance.setDirectives(new ArrayList<>(directives));
2919             }
2920             UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
2921             if (uploadNodeFilterInfo != null) {
2922                 componentInstance
2923                     .setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
2924             }
2925             ComponentTypeEnum containerComponentType = component.getComponentType();
2926             NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
2927             if (containerNodeType.equals(NodeTypeEnum.Resource) && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && MapUtils
2928                 .isNotEmpty(refResource.getCapabilities())) {
2929                 serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
2930                 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic
2931                     .getValidComponentInstanceCapabilities(refResource.getUniqueId(), refResource.getCapabilities(),
2932                         uploadComponentInstanceInfo.getCapabilities());
2933                 componentInstance.setCapabilities(validComponentInstanceCapabilities);
2934             }
2935             if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
2936                 ResponseFormat responseFormat = componentsUtils
2937                     .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2938                         uploadComponentInstanceInfo.getType());
2939                 throw new ComponentException(responseFormat);
2940             }
2941             Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
2942             componentInstance.setName(uploadComponentInstanceInfo.getName());
2943             componentInstance.setIcon(origResource.getIcon());
2944             componentInstance.setMinOccurrences(uploadComponentInstanceInfo.getMinOccurrences());
2945             componentInstance.setMaxOccurrences(uploadComponentInstanceInfo.getMaxOccurrences());
2946             componentInstance.setInstanceCount(uploadComponentInstanceInfo.getInstanceCount());
2947             resourcesInstancesMap.put(componentInstance, origResource);
2948         } catch (final ComponentException e) {
2949             throw e;
2950         } catch (final Exception e) {
2951             throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage());
2952         }
2953     }
2954
2955     protected Resource validateResourceInstanceBeforeCreate(String yamlName, String model, UploadComponentInstanceInfo uploadComponentInstanceInfo,
2956                                                             Map<String, Resource> nodeNamespaceMap) {
2957         Resource refResource;
2958         try {
2959             if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2960                 refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
2961             } else {
2962                 final Either<Component, StorageOperationStatus> resourceEither =
2963                     toscaOperationFacade.getLatestByToscaResourceName(uploadComponentInstanceInfo.getType(), model);
2964                 if (resourceEither.isRight()) {
2965                     ResponseFormat responseFormat = componentsUtils
2966                         .getResponseFormat(componentsUtils.convertFromStorageResponse(resourceEither.right().value()));
2967                     throw new ComponentException(responseFormat);
2968                 }
2969                 refResource = (Resource) resourceEither.left().value();
2970                 nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
2971             }
2972             String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
2973             if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
2974                 ResponseFormat responseFormat = componentsUtils
2975                     .getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(), refResource.getName(),
2976                         componentState);
2977                 throw new ComponentException(responseFormat);
2978             }
2979             if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.VF) {
2980                 log.debug("validateResourceInstanceBeforeCreate -  ref resource type is  ", refResource.getResourceType());
2981                 ResponseFormat responseFormat = componentsUtils
2982                     .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2983                         uploadComponentInstanceInfo.getType());
2984                 throw new ComponentException(responseFormat);
2985             }
2986             return refResource;
2987         } catch (final ComponentException e) {
2988             throw e;
2989         } catch (final Exception e) {
2990             throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage());
2991         }
2992     }
2993
2994     protected void handleServiceNodeTypes(String yamlName, Service service, String topologyTemplateYaml, boolean needLock,
2995                                           Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2996                                           List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
2997                                           CsarInfo csarInfo, String nodeName) {
2998         try {
2999             for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
3000                 boolean isResourceNotExisted = validateResourceNotExisted(nodeTypeEntry.getKey());
3001                 if (nodeTypeEntry.getValue().isNested() && isResourceNotExisted) {
3002                     handleNestedVF(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3003                         nodeTypeEntry.getKey());
3004                     log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
3005                 }
3006             }
3007             Map<String, Object> mappedToscaTemplate = null;
3008             if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) && nodeTypesInfo
3009                 .containsKey(nodeName)) {
3010                 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
3011             }
3012             if (MapUtils.isEmpty(mappedToscaTemplate)) {
3013                 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
3014             }
3015             createResourcesFromYamlNodeTypesList(yamlName, service, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
3016                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
3017         } catch (ComponentException | StorageException e) {
3018             throw e;
3019         } catch (Exception e) {
3020             log.debug("Exception occured when handleServiceNodeTypes, error is:{}", e.getMessage(), e);
3021             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3022         }
3023     }
3024
3025     protected boolean validateResourceNotExisted(String type) {
3026         try {
3027             Either<Resource, StorageOperationStatus> latestResource = toscaOperationFacade.getLatestResourceByToscaResourceName(type);
3028             return latestResource.isRight();
3029         } catch (Exception e) {
3030             log.debug("Exception occured when validateResourceNotExisted, error is:{}", e.getMessage(), e);
3031             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3032         }
3033     }
3034
3035     protected Resource handleNestedVF(Service service,
3036                                       Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3037                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
3038                                       String nodeName) {
3039         try {
3040             String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
3041             Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
3042             createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
3043                 csarInfo);
3044             log.debug("************* Finished to create node types from yaml {}", yamlName);
3045             if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
3046                 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
3047                 return handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
3048             }
3049             return new Resource();
3050         } catch (Exception e) {
3051             log.debug("Exception occured when handleNestedVF, error is:{}", e.getMessage(), e);
3052             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3053         }
3054     }
3055
3056     protected Resource handleComplexVfc(
3057         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3058         List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName) {
3059         try {
3060             Resource oldComplexVfc = null;
3061             Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(csarInfo, nodeName, nodesInfo);
3062             Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
3063                 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
3064             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
3065                 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
3066                     serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName)
3067                         .getRight());
3068             }
3069             if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
3070                 log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
3071                     oldComplexVfcRes.right().value());
3072                 throw new ComponentException(ActionStatus.GENERAL_ERROR);
3073             } else if (oldComplexVfcRes.isLeft()) {
3074                 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
3075                 Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic
3076                     .validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(), newComplexVfc,
3077                         ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
3078                 if (eitherValidation.isLeft()) {
3079                     oldComplexVfc = oldComplexVfcRes.left().value();
3080                 }
3081             }
3082             newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
3083                 newComplexVfc);
3084             csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
3085             LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
3086                 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
3087             log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
3088             final Resource result = serviceImportParseLogic
3089                 .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
3090             csarInfo.getCreatedNodes().put(nodeName, result);
3091             csarInfo.removeNodeFromQueue();
3092             return result;
3093         } catch (Exception e) {
3094             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
3095             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3096         }
3097     }
3098
3099     protected Resource handleComplexVfc(
3100         Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
3101         List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName,
3102         Resource oldComplexVfc, Resource newComplexVfc) {
3103         Resource handleComplexVfcRes;
3104         try {
3105             Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
3106             String yamlContent = new String(csarInfo.getCsar().get(yamlName));
3107             Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream()
3108                 .collect(toMap(Map.Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
3109             CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
3110             if (oldComplexVfc == null) {
3111                 handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
3112                     false, true, nodeName);
3113             } else {
3114                 handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA,
3115                     createdArtifacts, yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
3116             }
3117             return handleComplexVfcRes;
3118         } catch (Exception e) {
3119             log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
3120             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3121         }
3122     }
3123
3124     protected Resource updateResourceFromYaml(Resource oldRresource, Resource newRresource, AuditingActionEnum actionEnum,
3125                                               List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent,
3126                                               CsarInfo csarInfo, Map<String, NodeTypeInfo> nodeTypesInfo,
3127                                               Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3128                                               String nodeName, boolean isNested) {
3129         boolean inTransaction = true;
3130         boolean shouldLock = false;
3131         Resource preparedResource = null;
3132         ParsedToscaYamlInfo uploadComponentInstanceInfoMap = null;
3133         try {
3134             uploadComponentInstanceInfoMap = csarBusinessLogic
3135                 .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldRresource);
3136             Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
3137             if (MapUtils.isEmpty(instances) && newRresource.getResourceType() != ResourceTypeEnum.PNF) {
3138                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
3139             }
3140             preparedResource = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(), inTransaction, shouldLock,
3141                 isNested).left;
3142             log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
3143             serviceImportParseLogic.handleResourceGenericType(preparedResource);
3144             handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
3145                 csarInfo, nodeName);
3146             preparedResource = serviceImportParseLogic.createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
3147             preparedResource = createResourceInstances(yamlFileName, preparedResource, instances, csarInfo.getCreatedNodes());
3148             preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances);
3149         } catch (ComponentException e) {
3150             ResponseFormat responseFormat =
3151                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3152             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3153             componentsUtils
3154                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3155             throw e;
3156         } catch (StorageException e) {
3157             ResponseFormat responseFormat = componentsUtils
3158                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3159             log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3160             componentsUtils
3161                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3162             throw e;
3163         }
3164         Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3165             uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
3166         if (validateUpdateVfGroupNamesRes.isRight()) {
3167             throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3168         }
3169         Map<String, GroupDefinition> groups;
3170         if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3171             groups = validateUpdateVfGroupNamesRes.left().value();
3172         } else {
3173             groups = uploadComponentInstanceInfoMap.getGroups();
3174         }
3175         serviceImportParseLogic.handleGroupsProperties(preparedResource, groups);
3176         preparedResource = serviceImportParseLogic.updateGroupsOnResource(preparedResource, groups);
3177         NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
3178         Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE,
3179             createdArtifacts, yamlFileName, csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3180         if (updateArtifactsEither.isRight()) {
3181             log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
3182             throw new ComponentException(updateArtifactsEither.right().value());
3183         }
3184         preparedResource = serviceImportParseLogic.getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
3185         ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldRresource, preparedResource);
3186         if (mergingPropsAndInputsStatus != ActionStatus.OK) {
3187             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
3188             throw new ComponentException(responseFormat);
3189         }
3190         compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
3191         return preparedResource;
3192     }
3193
3194     protected Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName,
3195                                               Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3196                                               Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3197                                               boolean shouldLock, boolean inTransaction, String nodeName) {
3198         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
3199         Resource createdResource;
3200         try {
3201             ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
3202                 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
3203             if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
3204                 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
3205             }
3206             log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
3207             createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
3208                 createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
3209             log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
3210         } catch (ComponentException e) {
3211             ResponseFormat responseFormat =
3212                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3213             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3214             throw e;
3215         } catch (StorageException e) {
3216             ResponseFormat responseFormat = componentsUtils
3217                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3218             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3219             throw e;
3220         }
3221         return createdResource;
3222     }
3223
3224     protected Resource createResourceAndRIsFromYaml(String yamlName, Resource resource, ParsedToscaYamlInfo parsedToscaYamlInfo,
3225                                                     AuditingActionEnum actionEnum, boolean isNormative, List<ArtifactDefinition> createdArtifacts,
3226                                                     String topologyTemplateYaml, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3227                                                     Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3228                                                     boolean shouldLock, boolean inTransaction, String nodeName) {
3229         List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
3230         if (shouldLock) {
3231             Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic
3232                 .lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
3233             if (lockResult.isRight()) {
3234                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3235                 throw new ComponentException(lockResult.right().value());
3236             }
3237             log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
3238         }
3239         try {
3240             log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
3241             Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource);
3242             resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
3243             log.trace("************* Going to add inputs from yaml {}", yamlName);
3244             Map<String, Object> yamlMap = ImportUtils.loadYamlAsStrictMap(csarInfo.getMainTemplateContent());
3245             Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata");
3246             String type = (String) metadata.get("type");
3247             if (resource.shouldGenerateInputs() && !"Service".equalsIgnoreCase(type)) {
3248                 serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
3249             }
3250             Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
3251             resource = serviceImportParseLogic.createInputsOnResource(resource, inputs);
3252             Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
3253             resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, topologyTemplateYaml,
3254                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
3255             log.trace("************* Finished to create nodes, RI and Relation  from yaml {}", yamlName);
3256             // validate update vf module group names
3257             Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3258                 parsedToscaYamlInfo.getGroups(), resource.getSystemName());
3259             if (validateUpdateVfGroupNamesRes.isRight()) {
3260                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3261                 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3262             }
3263             Map<String, GroupDefinition> groups;
3264             log.trace("************* Going to add groups from yaml {}", yamlName);
3265             if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3266                 groups = validateUpdateVfGroupNamesRes.left().value();
3267             } else {
3268                 groups = parsedToscaYamlInfo.getGroups();
3269             }
3270             Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
3271             if (createGroupsOnResource.isRight()) {
3272                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3273                 throw new ComponentException(createGroupsOnResource.right().value());
3274             }
3275             resource = createGroupsOnResource.left().value();
3276             log.trace("************* Going to add artifacts from yaml {}", yamlName);
3277             NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate);
3278             Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE,
3279                 createdArtifacts, yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3280             if (createArtifactsEither.isRight()) {
3281                 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3282                 throw new ComponentException(createArtifactsEither.right().value());
3283             }
3284             resource = serviceImportParseLogic.getResourceWithGroups(createArtifactsEither.left().value().getUniqueId());
3285             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
3286             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
3287             ASDCKpiApi.countCreatedResourcesKPI();
3288             return resource;
3289         } catch (ComponentException | StorageException e) {
3290             serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3291             throw e;
3292         } finally {
3293             if (!inTransaction) {
3294                 janusGraphDao.commit();
3295             }
3296             if (shouldLock) {
3297                 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
3298             }
3299         }
3300     }
3301
3302     protected Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
3303         if (groups != null && !groups.isEmpty()) {
3304             List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
3305             serviceImportParseLogic.handleGroupsProperties(resource, groups);
3306             serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
3307             Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
3308             if (createGroups.isRight()) {
3309                 return Either.right(createGroups.right().value());
3310             }
3311         } else {
3312             return Either.left(resource);
3313         }
3314         Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
3315         if (updatedResource.isRight()) {
3316             ResponseFormat responseFormat = componentsUtils
3317                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource);
3318             return Either.right(responseFormat);
3319         }
3320         return Either.left(updatedResource.left().value());
3321     }
3322
3323     protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
3324         List<GroupDefinition> result = new ArrayList<>();
3325         List<ComponentInstance> componentInstances = component.getComponentInstances();
3326         if (groups != null) {
3327             for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
3328                 String groupName = entry.getKey();
3329                 GroupDefinition groupDefinition = entry.getValue();
3330                 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
3331                 updatedGroupDefinition.setMembers(null);
3332                 Map<String, String> members = groupDefinition.getMembers();
3333                 if (members != null) {
3334                     updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
3335                 }
3336                 result.add(updatedGroupDefinition);
3337             }
3338         }
3339         return result;
3340     }
3341
3342     protected void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
3343                                       List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
3344         Set<String> compInstancesNames = members.keySet();
3345         if (CollectionUtils.isEmpty(componentInstances)) {
3346             String membersAstString = compInstancesNames.stream().collect(joining(","));
3347             log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
3348                 groupName, component.getNormalizedName());
3349             throw new ComponentException(componentsUtils
3350                 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3351                     serviceImportParseLogic.getComponentTypeForResponse(component)));
3352         }
3353         Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
3354         memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
3355         Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
3356             .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
3357         if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) {
3358             List<String> foundMembers = new ArrayList<>();
3359             if (relevantInstances != null) {
3360                 foundMembers = relevantInstances.keySet().stream().collect(toList());
3361             }
3362             compInstancesNames.removeAll(foundMembers);
3363             String membersAstString = compInstancesNames.stream().collect(joining(","));
3364             throw new ComponentException(componentsUtils
3365                 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3366                     serviceImportParseLogic.getComponentTypeForResponse(component)));
3367         }
3368         updatedGroupDefinition.setMembers(relevantInstances);
3369     }
3370
3371     protected Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
3372         Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
3373             .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
3374         if (eitherValidation.isRight()) {
3375             ResponseFormat errorResponse = componentsUtils
3376                 .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
3377             throw new ComponentException(errorResponse);
3378         }
3379         if (Boolean.TRUE.equals(eitherValidation.left().value())) {
3380             log.debug("resource with name: {}, already exists", resource.getName());
3381             ResponseFormat errorResponse = componentsUtils
3382                 .getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
3383             throw new ComponentException(errorResponse);
3384         }
3385         log.debug("send resource {} to dao for create", resource.getName());
3386         serviceImportParseLogic.createArtifactsPlaceHolderData(resource, user);
3387         if (!isNormative) {
3388             log.debug("enrich resource with creator, version and state");
3389             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
3390             resource.setVersion(INITIAL_VERSION);
3391             resource.setHighestVersion(true);
3392             if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.VF) {
3393                 resource.setAbstract(false);
3394             }
3395         }
3396         return toscaOperationFacade.createToscaComponent(resource).left()
3397             .on(r -> serviceImportParseLogic.throwComponentExceptionByResource(r, resource));
3398     }
3399
3400     protected ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
3401                                                                                    boolean inTransaction, boolean needLock, boolean isNested) {
3402         String lockedResourceId = oldResource.getUniqueId();
3403         log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
3404             oldResource.getLifecycleState());
3405         ImmutablePair<Resource, ActionStatus> resourcePair = null;
3406         try {
3407             serviceBusinessLogic.lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3408             oldResource = serviceImportParseLogic.prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3409             serviceImportParseLogic.mergeOldResourceMetadataWithNew(oldResource, newResource);
3410             serviceImportParseLogic.validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3411             serviceImportParseLogic.validateCapabilityTypesCreate(user, serviceImportParseLogic.getCapabilityTypeOperation(), newResource,
3412                 AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3413             createNewResourceToOldResource(newResource, oldResource, user);
3414             Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
3415             if (overrideResource.isRight()) {
3416                 ResponseFormat responseFormat = new ResponseFormat();
3417                 serviceBusinessLogic.throwComponentException(responseFormat);
3418             }
3419             log.debug("Resource updated successfully!!!");
3420             resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
3421             return resourcePair;
3422         } finally {
3423             if (resourcePair == null) {
3424                 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3425                 janusGraphDao.rollback();
3426             } else if (!inTransaction) {
3427                 janusGraphDao.commit();
3428             }
3429             if (needLock) {
3430                 log.debug("unlock resource {}", lockedResourceId);
3431                 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3432             }
3433         }
3434     }
3435
3436     protected void createNewResourceToOldResource(Resource newResource, Resource oldResource, User user) {
3437         newResource.setContactId(newResource.getContactId().toLowerCase());
3438         newResource.setCreatorUserId(user.getUserId());
3439         newResource.setCreatorFullName(user.getFullName());
3440         newResource.setLastUpdaterUserId(user.getUserId());
3441         newResource.setLastUpdaterFullName(user.getFullName());
3442         newResource.setUniqueId(oldResource.getUniqueId());
3443         newResource.setVersion(oldResource.getVersion());
3444         newResource.setInvariantUUID(oldResource.getInvariantUUID());
3445         newResource.setLifecycleState(oldResource.getLifecycleState());
3446         newResource.setUUID(oldResource.getUUID());
3447         newResource.setNormalizedName(oldResource.getNormalizedName());
3448         newResource.setSystemName(oldResource.getSystemName());
3449         if (oldResource.getCsarUUID() != null) {
3450             newResource.setCsarUUID(oldResource.getCsarUUID());
3451         }
3452         if (oldResource.getCsarVersionId() != null) {
3453             newResource.setCsarVersionId(oldResource.getCsarVersionId());
3454         }
3455         if (oldResource.getImportedToscaChecksum() != null) {
3456             newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3457         }
3458         if (newResource.getDerivedFromGenericType() == null || newResource.getDerivedFromGenericType().isEmpty()) {
3459             newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3460         }
3461         if (newResource.getDerivedFromGenericVersion() == null || newResource.getDerivedFromGenericVersion().isEmpty()) {
3462             newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3463         }
3464         if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) {
3465             serviceBusinessLogic.setToscaArtifactsPlaceHolders(newResource, user);
3466         }
3467         if (newResource.getInterfaces() == null || newResource.getInterfaces().isEmpty()) {
3468             newResource.setInterfaces(oldResource.getInterfaces());
3469         }
3470         if (CollectionUtils.isEmpty(newResource.getProperties())) {
3471             newResource.setProperties(oldResource.getProperties());
3472         }
3473         if (newResource.getModel() == null) {
3474             newResource.setModel(oldResource.getModel());
3475         }
3476     }
3477
3478     protected Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Service service, Map<String, Object> mappedToscaTemplate,
3479                                                                          boolean needLock,
3480                                                                          Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3481                                                                          List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
3482                                                                          Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
3483         try {
3484             Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
3485                 TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
3486             if (toscaVersion.isRight()) {
3487                 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
3488             }
3489             Map<String, Object> mapToConvert = new HashMap<>();
3490             mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
3491             Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
3492             createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3493                 mapToConvert, nodeTypes);
3494             return csarInfo.getCreatedNodes();
3495         } catch (Exception e) {
3496             log.debug("Exception occured when createResourcesFromYamlNodeTypesList,error is:{}", e.getMessage(), e);
3497             throw new ComponentException(ActionStatus.GENERAL_ERROR);
3498         }
3499     }
3500
3501     protected void createNodeTypes(String yamlName, Service service, boolean needLock,
3502                                    Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3503                                    List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3504                                    Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
3505         Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
3506         Resource vfcCreated = null;
3507         while (nodesNameValueIter.hasNext()) {
3508             Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
3509             String nodeTypeKey = nodeType.getKey();
3510             Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
3511                 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeTypeKey);
3512             if (nodeTypesInfo.containsKey(nodeTypeKey)) {
3513                 vfcCreated = handleNestedVfc(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3514                     nodeTypeKey);
3515                 log.trace("************* Finished to handle nested vfc {}", nodeTypeKey);
3516             } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
3517                 .containsKey(nodeTypeKey)) {
3518                 ImmutablePair<Resource, ActionStatus> resourceCreated = serviceImportParseLogic
3519                     .createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, service, needLock,
3520                         nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
3521                 log.debug("************* Finished to create node {}", nodeTypeKey);
3522                 vfcCreated = resourceCreated.getLeft();
3523                 csarInfo.getCreatedNodesToscaResourceNames().put(nodeTypeKey, vfcCreated.getName());
3524             }
3525             if (vfcCreated != null) {
3526                 csarInfo.getCreatedNodes().put(nodeTypeKey, vfcCreated);
3527             }
3528             mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
3529         }
3530     }
3531 }