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