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