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