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