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