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