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