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