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