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