Support adding data types to model
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ResourceBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.impl;
21
22 import static java.util.stream.Collectors.joining;
23 import static java.util.stream.Collectors.toList;
24 import static java.util.stream.Collectors.toMap;
25 import static java.util.stream.Collectors.toSet;
26 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
27 import static org.apache.commons.collections.MapUtils.isEmpty;
28 import static org.apache.commons.collections.MapUtils.isNotEmpty;
29 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement;
30 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
31 import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN;
32 import static org.openecomp.sdc.common.api.Constants.DEFAULT_GROUP_VF_MODULE;
33
34 import com.google.common.annotations.VisibleForTesting;
35 import fj.data.Either;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.EnumMap;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.ListIterator;
45 import java.util.Map;
46 import java.util.Map.Entry;
47 import java.util.Optional;
48 import java.util.Set;
49 import java.util.function.Function;
50 import java.util.regex.Pattern;
51 import java.util.stream.Collectors;
52 import org.apache.commons.codec.binary.Base64;
53 import org.apache.commons.collections.CollectionUtils;
54 import org.apache.commons.collections.MapUtils;
55 import org.apache.commons.collections4.ListUtils;
56 import org.apache.commons.lang3.StringUtils;
57 import org.apache.commons.lang3.tuple.ImmutablePair;
58 import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
59 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
60 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
61 import org.openecomp.sdc.be.components.csar.CsarInfo;
62 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
63 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
64 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
65 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
66 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
67 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
68 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
69 import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils;
70 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
71 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
72 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
73 import org.openecomp.sdc.be.components.merge.TopologyComparator;
74 import org.openecomp.sdc.be.components.merge.property.PropertyDataValueMergeBusinessLogic;
75 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
76 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
77 import org.openecomp.sdc.be.components.property.PropertyConstraintsUtils;
78 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
79 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
80 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
81 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
82 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
83 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
84 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
85 import org.openecomp.sdc.be.config.BeEcompErrorManager;
86 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
87 import org.openecomp.sdc.be.config.ConfigurationManager;
88 import org.openecomp.sdc.be.dao.api.ActionStatus;
89 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
90 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
91 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
92 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
93 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
94 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
95 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
96 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
97 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
98 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
99 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
100 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
101 import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
102 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
103 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
104 import org.openecomp.sdc.be.datatypes.enums.CreatedFrom;
105 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
106 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
107 import org.openecomp.sdc.be.impl.ComponentsUtils;
108 import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
109 import org.openecomp.sdc.be.model.ArtifactDefinition;
110 import org.openecomp.sdc.be.model.AttributeDefinition;
111 import org.openecomp.sdc.be.model.CapabilityDefinition;
112 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
113 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
114 import org.openecomp.sdc.be.model.Component;
115 import org.openecomp.sdc.be.model.ComponentInstance;
116 import org.openecomp.sdc.be.model.ComponentInstanceInput;
117 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
118 import org.openecomp.sdc.be.model.ComponentParametersView;
119 import org.openecomp.sdc.be.model.DataTypeDefinition;
120 import org.openecomp.sdc.be.model.GroupDefinition;
121 import org.openecomp.sdc.be.model.InputDefinition;
122 import org.openecomp.sdc.be.model.InterfaceDefinition;
123 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
124 import org.openecomp.sdc.be.model.LifecycleStateEnum;
125 import org.openecomp.sdc.be.model.NodeTypeInfo;
126 import org.openecomp.sdc.be.model.Operation;
127 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
128 import org.openecomp.sdc.be.model.PolicyDefinition;
129 import org.openecomp.sdc.be.model.PropertyDefinition;
130 import org.openecomp.sdc.be.model.RelationshipImpl;
131 import org.openecomp.sdc.be.model.RelationshipInfo;
132 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
133 import org.openecomp.sdc.be.model.RequirementDefinition;
134 import org.openecomp.sdc.be.model.Resource;
135 import org.openecomp.sdc.be.model.UploadArtifactInfo;
136 import org.openecomp.sdc.be.model.UploadCapInfo;
137 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
138 import org.openecomp.sdc.be.model.UploadInfo;
139 import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
140 import org.openecomp.sdc.be.model.UploadPropInfo;
141 import org.openecomp.sdc.be.model.UploadReqInfo;
142 import org.openecomp.sdc.be.model.UploadResourceInfo;
143 import org.openecomp.sdc.be.model.User;
144 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
145 import org.openecomp.sdc.be.model.category.CategoryDefinition;
146 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
147 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
148 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
149 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
150 import org.openecomp.sdc.be.model.operations.StorageException;
151 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
152 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
153 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
154 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
155 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
156 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
157 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
158 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
159 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
160 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
161 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
162 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
163 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
164 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
165 import org.openecomp.sdc.be.tosca.CsarUtils;
166 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
167 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
168 import org.openecomp.sdc.be.user.UserBusinessLogic;
169 import org.openecomp.sdc.be.utils.CommonBeUtils;
170 import org.openecomp.sdc.be.utils.TypeUtils;
171 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
172 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
173 import org.openecomp.sdc.common.api.Constants;
174 import org.openecomp.sdc.common.datastructure.Wrapper;
175 import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
176 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
177 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
178 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
179 import org.openecomp.sdc.common.log.enums.StatusCode;
180 import org.openecomp.sdc.common.log.wrappers.Logger;
181 import org.openecomp.sdc.common.util.GeneralUtility;
182 import org.openecomp.sdc.common.util.ValidationUtils;
183 import org.openecomp.sdc.exception.ResponseFormat;
184 import org.springframework.beans.factory.annotation.Autowired;
185 import org.springframework.context.annotation.Lazy;
186 import org.yaml.snakeyaml.DumperOptions;
187 import org.yaml.snakeyaml.Yaml;
188
189 @org.springframework.stereotype.Component("resourceBusinessLogic")
190 public class ResourceBusinessLogic extends ComponentBusinessLogic {
191
192     private static final String DELETE_RESOURCE = "Delete Resource";
193     private static final String IN_RESOURCE = "  in resource {} ";
194     private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes";
195     private static final String INITIAL_VERSION = "0.1";
196     private static final Logger log = Logger.getLogger(ResourceBusinessLogic.class);
197     private static final String CERTIFICATION_ON_IMPORT = "certification on import";
198     private static final String CREATE_RESOURCE = "Create Resource";
199     private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update";
200     private static final String CATEGORY_IS_EMPTY = "Resource category is empty";
201     private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate";
202     private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name ";
203     private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {}  in resource {} ";
204     private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ResourceBusinessLogic.class.getName());
205     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
206     private final ResourceImportManager resourceImportManager;
207     private final InputsBusinessLogic inputsBusinessLogic;
208     private final OutputsBusinessLogic outputsBusinessLogic;
209     private final CompositionBusinessLogic compositionBusinessLogic;
210     private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
211     private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
212     private final MergeInstanceUtils mergeInstanceUtils;
213     private final UiComponentDataConverter uiComponentDataConverter;
214     private final CsarBusinessLogic csarBusinessLogic;
215     private final PropertyBusinessLogic propertyBusinessLogic;
216     private final PolicyBusinessLogic policyBusinessLogic;
217     private IInterfaceLifecycleOperation interfaceTypeOperation;
218     private LifecycleBusinessLogic lifecycleBusinessLogic;
219     @Autowired
220     private ICapabilityTypeOperation capabilityTypeOperation;
221     @Autowired
222     private TopologyComparator topologyComparator;
223     @Autowired
224     private ComponentValidator componentValidator;
225     @Autowired
226     private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogic;
227     @Autowired
228     private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
229
230     @Autowired
231     public ResourceBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation,
232                                  final IGroupInstanceOperation groupInstanceOperation, final IGroupTypeOperation groupTypeOperation,
233                                  final GroupBusinessLogic groupBusinessLogic, final InterfaceOperation interfaceOperation,
234                                  final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
235                                  final ArtifactsBusinessLogic artifactsBusinessLogic,
236                                  final ComponentInstanceBusinessLogic componentInstanceBusinessLogic,
237                                  final @Lazy ResourceImportManager resourceImportManager, final InputsBusinessLogic inputsBusinessLogic,
238                                  final OutputsBusinessLogic outputsBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic,
239                                  final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic,
240                                  final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic,
241                                  final MergeInstanceUtils mergeInstanceUtils, final UiComponentDataConverter uiComponentDataConverter,
242                                  final CsarBusinessLogic csarBusinessLogic, final ArtifactsOperations artifactToscaOperation,
243                                  final PropertyBusinessLogic propertyBusinessLogic, final ComponentContactIdValidator componentContactIdValidator,
244                                  final ComponentNameValidator componentNameValidator, final ComponentTagsValidator componentTagsValidator,
245                                  final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator,
246                                  final ComponentProjectCodeValidator componentProjectCodeValidator,
247                                  final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic) {
248         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation,
249             interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator,
250             componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
251         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
252         this.resourceImportManager = resourceImportManager;
253         this.inputsBusinessLogic = inputsBusinessLogic;
254         this.outputsBusinessLogic = outputsBusinessLogic;
255         this.compositionBusinessLogic = compositionBusinessLogic;
256         this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic;
257         this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic;
258         this.mergeInstanceUtils = mergeInstanceUtils;
259         this.uiComponentDataConverter = uiComponentDataConverter;
260         this.csarBusinessLogic = csarBusinessLogic;
261         this.propertyBusinessLogic = propertyBusinessLogic;
262         this.policyBusinessLogic = policyBusinessLogic;
263     }
264
265     static <T> Either<T, RuntimeException> rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus,
266                                                               final String... params) {
267         if (janusGraphDao != null) {
268             janusGraphDao.rollback();
269         }
270         return Either.right(new ByActionStatusComponentException(actionStatus, params));
271     }
272
273     public LifecycleBusinessLogic getLifecycleBusinessLogic() {
274         return lifecycleBusinessLogic;
275     }
276
277     @Autowired
278     public void setLifecycleManager(LifecycleBusinessLogic lifecycleBusinessLogic) {
279         this.lifecycleBusinessLogic = lifecycleBusinessLogic;
280     }
281
282     @VisibleForTesting
283     protected void setComponentValidator(ComponentValidator componentValidator) {
284         this.componentValidator = componentValidator;
285     }
286
287     public IElementOperation getElementDao() {
288         return elementDao;
289     }
290
291     public void setElementDao(IElementOperation elementDao) {
292         this.elementDao = elementDao;
293     }
294
295     public UserBusinessLogic getUserAdmin() {
296         return this.userAdmin;
297     }
298
299     @Autowired
300     @Override
301     public void setUserAdmin(UserBusinessLogic userAdmin) {
302         this.userAdmin = userAdmin;
303     }
304
305     public ComponentsUtils getComponentsUtils() {
306         return this.componentsUtils;
307     }
308
309     @Autowired
310     @Override
311     public void setComponentsUtils(ComponentsUtils componentsUtils) {
312         this.componentsUtils = componentsUtils;
313     }
314
315     public ArtifactsBusinessLogic getArtifactsManager() {
316         return artifactsBusinessLogic;
317     }
318
319     public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
320         this.artifactsBusinessLogic = artifactsManager;
321     }
322
323     public ApplicationDataTypeCache getApplicationDataTypeCache() {
324         return applicationDataTypeCache;
325     }
326
327     @Autowired
328     @Override
329     public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
330         this.applicationDataTypeCache = applicationDataTypeCache;
331     }
332
333     @Autowired
334     public void setInterfaceTypeOperation(IInterfaceLifecycleOperation interfaceTypeOperation) {
335         this.interfaceTypeOperation = interfaceTypeOperation;
336     }
337
338     /**
339      * the method returns a list of all the resources that are certified, the returned resources are only abstract or only none abstract according to
340      * the given param
341      *
342      * @param getAbstract
343      * @param userId      TODO
344      * @return
345      */
346     public List<Resource> getAllCertifiedResources(boolean getAbstract, HighestFilterEnum highestFilter, String userId) {
347         User user = validateUserExists(userId);
348         Boolean isHighest = null;
349         switch (highestFilter) {
350             case ALL:
351                 break;
352             case HIGHEST_ONLY:
353                 isHighest = true;
354                 break;
355             case NON_HIGHEST_ONLY:
356                 isHighest = false;
357                 break;
358             default:
359                 break;
360         }
361         Either<List<Resource>, StorageOperationStatus> getResponse = toscaOperationFacade.getAllCertifiedResources(getAbstract, isHighest);
362         if (getResponse.isRight()) {
363             throw new StorageException(getResponse.right().value());
364         }
365         return getResponse.left().value();
366     }
367
368     public Either<Map<String, Boolean>, ResponseFormat> validateResourceNameExists(String resourceName, ResourceTypeEnum resourceTypeEnum,
369                                                                                    String userId) {
370         validateUserExists(userId);
371         Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
372             .validateComponentNameUniqueness(resourceName, resourceTypeEnum, ComponentTypeEnum.RESOURCE);
373         // DE242223
374         janusGraphDao.commit();
375         if (dataModelResponse.isLeft()) {
376             Map<String, Boolean> result = new HashMap<>();
377             result.put("isValid", dataModelResponse.left().value());
378             log.debug("validation was successfully performed.");
379             return Either.left(result);
380         }
381         ResponseFormat responseFormat = componentsUtils
382             .getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()));
383         return Either.right(responseFormat);
384     }
385
386     public Resource createResource(Resource resource, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload,
387                                    String payloadName) {
388         validateResourceBeforeCreate(resource, user, false);
389         String csarUUID = payloadName == null ? resource.getCsarUUID() : payloadName;
390         loggerSupportability.log(LoggerSupportabilityActions.CREATE_RESOURCE, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
391             "Starting to create resource from CSAR by user {} ", user.getUserId());
392         if (StringUtils.isNotEmpty(csarUUID)) {
393             csarBusinessLogic.validateCsarBeforeCreate(resource, auditingAction, user, csarUUID);
394             log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID);
395             Resource createResourceFromCsar = createResourceFromCsar(resource, user, csarUIPayload, csarUUID);
396             return updateCatalog(createResourceFromCsar, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value();
397         }
398         final Resource createResourceByDao = createResourceByDao(resource, user, auditingAction, false, false);
399         return updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value();
400     }
401
402     public Resource validateAndUpdateResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String payloadName,
403                                                       String resourceUniqueId) {
404         String csarUUID = payloadName;
405         String csarVersion = null;
406         Resource updatedResource = null;
407         if (payloadName == null) {
408             csarUUID = resource.getCsarUUID();
409             csarVersion = resource.getCsarVersion();
410         }
411         if (csarUUID != null && !csarUUID.isEmpty()) {
412             Resource oldResource = getResourceByUniqueId(resourceUniqueId);
413             validateCsarUuidMatching(oldResource, resource, csarUUID, resourceUniqueId, user);
414             validateCsarIsNotAlreadyUsed(oldResource, resource, csarUUID, user);
415             if (oldResource != null && ValidationUtils.hasBeenCertified(oldResource.getVersion())) {
416                 overrideImmutableMetadata(oldResource, resource);
417             }
418             validateResourceBeforeCreate(resource, user, false);
419             String oldCsarVersion = oldResource != null ? oldResource.getCsarVersion() : null;
420             log.debug("CsarUUID is {} - going to update resource with UniqueId {} from CSAR", csarUUID, resourceUniqueId);
421             // (on boarding flow): If the update includes same csarUUID and
422
423             // same csarVersion as already in the VF - no need to import the
424
425             // csar (do only metadata changes if there are).
426             if (csarVersion != null && oldCsarVersion != null && oldCsarVersion.equals(csarVersion)) {
427                 updatedResource = updateResourceMetadata(resourceUniqueId, resource, oldResource, user, false);
428             } else {
429                 updatedResource = updateResourceFromCsar(oldResource, resource, user, AuditingActionEnum.UPDATE_RESOURCE_METADATA, false,
430                     csarUIPayload, csarUUID);
431             }
432         } else {
433             log.debug("Failed to update resource {}, csarUUID or payload name is missing", resource.getSystemName());
434             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CSAR_UUID, resource.getName());
435             componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.CREATE_RESOURCE);
436             throw new ByActionStatusComponentException(ActionStatus.MISSING_CSAR_UUID, resource.getName());
437         }
438         return updatedResource;
439     }
440
441     private void validateCsarIsNotAlreadyUsed(Resource oldResource, Resource resource, String csarUUID, User user) {
442         // (on boarding flow): If the update includes a csarUUID: verify this
443
444         // csarUUID is not in use by another VF, If it is - use same error as
445
446         // above:
447
448         // "Error: The VSP with UUID %1 was already imported for VF %2. Please
449
450         // select another or update the existing VF." %1 - csarUUID, %2 - VF
451
452         // name
453         Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = toscaOperationFacade
454             .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUUID, resource.getSystemName());
455         if (resourceLinkedToCsarRes.isRight()) {
456             if (StorageOperationStatus.NOT_FOUND != resourceLinkedToCsarRes.right().value()) {
457                 log.debug("Failed to find previous resource by CSAR {} and system name {}", csarUUID, resource.getSystemName());
458                 throw new StorageException(resourceLinkedToCsarRes.right().value());
459             }
460         } else if (!resourceLinkedToCsarRes.left().value().getUniqueId().equals(oldResource.getUniqueId()) && !resourceLinkedToCsarRes.left().value()
461             .getName().equals(oldResource.getName())) {
462             ResponseFormat errorResponse = componentsUtils
463                 .getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
464             componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
465             throw new ByActionStatusComponentException(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
466         }
467     }
468
469     private void validateCsarUuidMatching(Resource resource, Resource oldResource, String csarUUID, String resourceUniqueId, User user) {
470         // (on boarding flow): If the update includes csarUUID which is
471
472         // different from the csarUUID of the VF - fail with
473
474         // error: "Error: Resource %1 cannot be updated using since it is linked
475
476         // to a different VSP" %1 - VF name
477         String oldCsarUUID = oldResource.getCsarUUID();
478         if (oldCsarUUID != null && !oldCsarUUID.isEmpty() && !csarUUID.equals(oldCsarUUID)) {
479             log.debug("Failed to update resource with UniqueId {} using Csar {}, since the resource is linked to a different VSP {}",
480                 resourceUniqueId, csarUUID, oldCsarUUID);
481             ResponseFormat errorResponse = componentsUtils
482                 .getResponseFormat(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
483             componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
484             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
485         }
486     }
487
488     private Resource getResourceByUniqueId(String resourceUniqueId) {
489         Either<Resource, StorageOperationStatus> oldResourceRes = toscaOperationFacade.getToscaFullElement(resourceUniqueId);
490         if (oldResourceRes.isRight()) {
491             log.debug("Failed to find previous resource by UniqueId {}, status: {}", resourceUniqueId, oldResourceRes.right().value());
492             throw new StorageException(oldResourceRes.right().value());
493         }
494         return oldResourceRes.left().value();
495     }
496
497     private void overrideImmutableMetadata(Resource oldResource, Resource resource) {
498         resource.setName(oldResource.getName());
499         resource.setIcon(oldResource.getIcon());
500         resource.setTags(oldResource.getTags());
501         resource.setCategories(oldResource.getCategories());
502         resource.setDerivedFrom(oldResource.getDerivedFrom());
503     }
504
505     private Resource updateResourceFromCsar(Resource oldResource, Resource newResource, User user, AuditingActionEnum updateResource,
506                                             boolean inTransaction, Map<String, byte[]> csarUIPayload, String csarUUID) {
507         Resource updatedResource = null;
508         validateLifecycleState(oldResource, user);
509         String lockedResourceId = oldResource.getUniqueId();
510         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
511         CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID);
512         lockComponent(lockedResourceId, oldResource, "update Resource From Csar");
513         Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo();
514         Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
515             nodeTypesInfo, csarInfo, oldResource);
516         if (findNodeTypesArtifactsToHandleRes.isRight()) {
517             log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
518             throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
519         }
520         Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = findNodeTypesArtifactsToHandleRes.left()
521             .value();
522         try {
523             updatedResource = updateResourceFromYaml(oldResource, newResource, updateResource, createdArtifacts, csarInfo.getMainTemplateName(),
524                 csarInfo.getMainTemplateContent(), csarInfo, nodeTypesInfo, nodeTypesArtifactsToHandle, null, false);
525         } catch (ComponentException | StorageException e) {
526             rollback(inTransaction, newResource, createdArtifacts, null);
527             throw e;
528         } finally {
529             janusGraphDao.commit();
530             log.debug("unlock resource {}", lockedResourceId);
531             graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
532         }
533         return updatedResource;
534     }
535
536     private void validateLifecycleState(Resource oldResource, User user) {
537         if (LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == oldResource.getLifecycleState() && !oldResource.getLastUpdaterUserId()
538             .equals(user.getUserId())) {
539             log.debug("#validateLifecycleState - Current user is not last updater, last updater userId: {}, current user userId: {}",
540                 oldResource.getLastUpdaterUserId(), user.getUserId());
541             throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
542         }
543     }
544
545     private Resource updateResourceFromYaml(Resource oldResource, Resource newResource, AuditingActionEnum actionEnum,
546                                             List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent, CsarInfo csarInfo,
547                                             Map<String, NodeTypeInfo> nodeTypesInfo,
548                                             Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
549                                             String nodeName, boolean isNested) {
550         boolean inTransaction = true;
551         boolean shouldLock = false;
552         Resource preparedResource = null;
553         ParsedToscaYamlInfo uploadComponentInstanceInfoMap;
554         try {
555             uploadComponentInstanceInfoMap = csarBusinessLogic
556                 .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldResource);
557             Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
558             if (MapUtils.isEmpty(instances) && newResource.getResourceType() != ResourceTypeEnum.PNF) {
559                 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
560             }
561             preparedResource = updateExistingResourceByImport(newResource, oldResource, csarInfo.getModifier(), inTransaction, shouldLock,
562                 isNested).left;
563             log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
564             handleResourceGenericType(preparedResource);
565             handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
566                 csarInfo, nodeName);
567             preparedResource = createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
568             Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
569             preparedResource = createResourceInstances(yamlFileName, preparedResource, oldResource, instances, csarInfo.getCreatedNodes(),
570                 existingNodeTypesByResourceNames);
571             preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, oldResource, instances,
572                 existingNodeTypesByResourceNames);
573         } catch (ComponentException e) {
574             ResponseFormat responseFormat =
575                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
576             log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
577             componentsUtils
578                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
579             throw e;
580         } catch (StorageException e) {
581             ResponseFormat responseFormat = componentsUtils
582                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
583             log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
584             componentsUtils
585                 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
586             throw e;
587         }
588         Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
589             .validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
590         if (validateUpdateVfGroupNamesRes.isRight()) {
591             throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
592         }
593         // add groups to newResource
594         Map<String, GroupDefinition> groups;
595         if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
596             groups = validateUpdateVfGroupNamesRes.left().value();
597         } else {
598             groups = uploadComponentInstanceInfoMap.getGroups();
599         }
600         handleGroupsProperties(preparedResource, groups);
601         Either<Boolean, ActionStatus> isTopologyChanged = topologyComparator.isTopologyChanged(oldResource, preparedResource);
602         preparedResource = updateGroupsOnResource(preparedResource, groups);
603         NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
604         Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName,
605             csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
606         if (updateArtifactsEither.isRight()) {
607             log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
608             throw new ByResponseFormatComponentException(updateArtifactsEither.right().value());
609         }
610         preparedResource = getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
611         updateGroupsName(oldResource, preparedResource, isTopologyChanged.left().value());
612         updateResourceInstancesNames(oldResource, csarInfo, preparedResource, isTopologyChanged.left().value());
613         final String preparedResourceId = preparedResource != null ? preparedResource.getUniqueId() : "";
614         preparedResource = getResourceWithGroups(preparedResourceId);
615         updateVolumeGroup(preparedResource);
616         ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldResource, preparedResource);
617         if (mergingPropsAndInputsStatus != ActionStatus.OK) {
618             ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
619             throw new ByResponseFormatComponentException(responseFormat);
620         }
621         compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
622         return preparedResource;
623     }
624
625     protected void updateVolumeGroup(Resource preparedResource) {
626         List<GroupDefinition> groups = preparedResource.safeGetGroups();
627         for (GroupDefinition group : groups) {
628             Map<String, ArtifactDefinition> createdNewArtifacts = preparedResource.getDeploymentArtifacts();
629             if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
630                 List<PropertyDataDefinition> volumePropList = group.getProperties().stream().filter(p -> "volume_group".equals(p.getName()))
631                     .collect(Collectors.toList());
632                 if (!volumePropList.isEmpty()) {
633                     PropertyDataDefinition volumeProp = volumePropList.get(0);
634                     if (volumeProp != null) {
635                         boolean isVolumeGroup = isVolumeGroup(group.getArtifacts(), new ArrayList<>(createdNewArtifacts.values()));
636                         if (!volumePropList.get(0).getValue().equals(String.valueOf(isVolumeGroup))) {
637                             volumeProp.setValue(String.valueOf(isVolumeGroup));
638                             volumeProp.setDefaultValue(String.valueOf(isVolumeGroup));
639                         }
640                     }
641                 }
642             }
643         }
644     }
645
646     private void updateGroupsName(Resource oldResource, Resource preparedResource, boolean isTopologyChanged) {
647         if (oldResource == null || preparedResource == null) {
648             log.debug("Failed to update groups name : oldResource or preparedResource is null");
649         } else if (CollectionUtils.isNotEmpty(oldResource.getGroups()) && CollectionUtils.isNotEmpty(preparedResource.getGroups())) {
650             Map<String, String> oldGroups = oldResource.getGroups().stream()
651                 .collect(toMap(GroupDataDefinition::getInvariantName, GroupDataDefinition::getName));
652             List<GroupDefinition> updatedGroups = preparedResource.getGroups().stream()
653                 .filter(group -> oldGroups.containsKey(group.getInvariantName()) && !group.getName().equals(oldGroups.get(group.getInvariantName())))
654                 .collect(toList());
655             if (CollectionUtils.isNotEmpty(updatedGroups)) {
656                 if (isTopologyChanged) {
657                     updatedGroups.stream().filter(group -> !group.isVspOriginated())
658                         .forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
659                 } else {
660                     updatedGroups.forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
661                 }
662                 groupBusinessLogic.updateGroups(preparedResource, updatedGroups, false);
663             }
664         }
665     }
666
667     private void updateResourceInstancesNames(Resource oldResource, CsarInfo csarInfo, Resource preparedResource, boolean isTopologyChanged) {
668         if (oldResource == null || preparedResource == null) {
669             log.debug("Failed to update resource instances names : oldResource or preparedResource is null");
670         } else {
671             if (CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
672                 Map<String, String> oldInstances = oldResource.getComponentInstances().stream()
673                     .collect(toMap(ComponentInstance::getInvariantName, ComponentInstance::getName));
674                 List<ComponentInstance> updatedInstances = preparedResource.getComponentInstances().stream()
675                     .filter(i -> oldInstances.containsKey(i.getInvariantName()) && !i.getName().equals(oldInstances.get(i.getInvariantName())))
676                     .collect(toList());
677                 if (CollectionUtils.isNotEmpty(updatedInstances)) {
678                     if (isTopologyChanged) {
679                         updatedInstances.stream().filter(i -> !i.isCreatedFromCsar()).forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
680                     } else {
681                         updatedInstances.forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
682                     }
683                 }
684             }
685             componentInstanceBusinessLogic.updateComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, null, preparedResource.getUniqueId(),
686                 csarInfo.getModifier().getUserId(), preparedResource.getComponentInstances(), false);
687         }
688     }
689
690     private Either<Resource, ResponseFormat> createOrUpdateArtifacts(ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
691                                                                      String yamlFileName, CsarInfo csarInfo, Resource preparedResource,
692                                                                      NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts,
693                                                                      boolean inTransaction, boolean shouldLock) {
694         String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
695         Resource resource = preparedResource;
696         Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
697             .getNodeTypesArtifactsToHandle();
698         if (preparedResource.getResourceType() == ResourceTypeEnum.CVFC) {
699             if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
700                 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource,
701                     nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true);
702                 if (handleNodeTypeArtifactsRes.isRight()) {
703                     return Either.right(handleNodeTypeArtifactsRes.right().value());
704                 }
705             }
706         } else {
707             Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts,
708                 new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
709             log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
710             if (createdCsarArtifactsEither.isRight()) {
711                 return createdCsarArtifactsEither;
712             }
713             resource = createdCsarArtifactsEither.left().value();
714         }
715         return Either.left(resource);
716     }
717
718     private Resource handleResourceGenericType(Resource resource) {
719         Resource genericResource = fetchAndSetDerivedFromGenericType(resource);
720         if (resource.shouldGenerateInputs()) {
721             generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
722         }
723         return genericResource;
724     }
725
726     private Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandle(
727         final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo, final Resource oldResource) {
728         final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>();
729         Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> nodeTypesArtifactsToHandleRes = Either
730             .left(nodeTypesArtifactsToHandle);
731         try {
732             final Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar());
733             final Map<String, ImmutablePair<String, String>> extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, oldResource.getName(),
734                 csarInfo);
735             log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(),
736                 csarInfo.getCsarUUID());
737             extractedVfcToscaNames.forEach(
738                 (namespace, vfcToscaNames) -> findAddNodeTypeArtifactsToHandle(csarInfo, nodeTypesArtifactsToHandle, oldResource,
739                     extractedVfcsArtifacts, namespace, vfcToscaNames));
740         } catch (Exception e) {
741             final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
742             nodeTypesArtifactsToHandleRes = Either.right(responseFormat);
743             log.debug("Exception occurred when findNodeTypesUpdatedArtifacts, error is:{}", e.getMessage(), e);
744         }
745         return nodeTypesArtifactsToHandleRes;
746     }
747
748     private void findAddNodeTypeArtifactsToHandle(CsarInfo csarInfo,
749                                                   Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
750                                                   Resource resource, Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts, String namespace,
751                                                   ImmutablePair<String, String> vfcToscaNames) {
752         EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> curNodeTypeArtifactsToHandle = null;
753         log.debug("Going to fetch node type with tosca name {}. ", vfcToscaNames.getLeft());
754         Resource curNodeType = findVfcResource(csarInfo, resource, vfcToscaNames.getLeft(), vfcToscaNames.getRight(), null);
755         if (!isEmpty(extractedVfcsArtifacts)) {
756             List<ArtifactDefinition> currArtifacts = new ArrayList<>();
757             if (extractedVfcsArtifacts.containsKey(namespace)) {
758                 handleAndAddExtractedVfcsArtifacts(currArtifacts, extractedVfcsArtifacts.get(namespace));
759             }
760             curNodeTypeArtifactsToHandle = findNodeTypeArtifactsToHandle(curNodeType, currArtifacts);
761         } else if (curNodeType != null) {
762             // delete all artifacts if have not received artifacts from
763
764             // csar
765             curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
766             List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
767             // delete all informational artifacts
768             artifactsToDelete.addAll(
769                 curNodeType.getArtifacts().values().stream().filter(a -> a.getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
770                     .collect(toList()));
771             // delete all deployment artifacts
772             artifactsToDelete.addAll(curNodeType.getDeploymentArtifacts().values());
773             if (!artifactsToDelete.isEmpty()) {
774                 curNodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
775             }
776         }
777         if (isNotEmpty(curNodeTypeArtifactsToHandle)) {
778             nodeTypesArtifactsToHandle.put(namespace, curNodeTypeArtifactsToHandle);
779         }
780     }
781
782     private Resource findVfcResource(CsarInfo csarInfo, Resource resource, String currVfcToscaName, String previousVfcToscaName,
783                                      StorageOperationStatus status) {
784         if (status != null && status != StorageOperationStatus.NOT_FOUND) {
785             log.debug("Error occurred during fetching node type with tosca name {}, error: {}", currVfcToscaName, status);
786             ResponseFormat responseFormat = componentsUtils
787                 .getResponseFormat(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
788             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.CREATE_RESOURCE);
789             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
790         } else if (StringUtils.isNotEmpty(currVfcToscaName)) {
791             return (Resource) toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName).left()
792                 .on(st -> findVfcResource(csarInfo, resource, previousVfcToscaName, null, st));
793         }
794         return null;
795     }
796
797     private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> findNodeTypeArtifactsToHandle(Resource curNodeType,
798                                                                                                    List<ArtifactDefinition> extractedArtifacts) {
799         try {
800             List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(extractedArtifacts);
801             List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>();
802             List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
803             processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, artifactsToDelete,
804                 collectExistingArtifacts(curNodeType));
805             return putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete);
806         } catch (Exception e) {
807             log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
808             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
809         }
810     }
811
812     private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> putFoundArtifacts(List<ArtifactDefinition> artifactsToUpload,
813                                                                                        List<ArtifactDefinition> artifactsToUpdate,
814                                                                                        List<ArtifactDefinition> artifactsToDelete) {
815         EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = null;
816         if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) {
817             nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
818             if (!artifactsToUpload.isEmpty()) {
819                 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
820             }
821             if (!artifactsToUpdate.isEmpty()) {
822                 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
823             }
824             if (!artifactsToDelete.isEmpty()) {
825                 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
826             }
827         }
828         return nodeTypeArtifactsToHandle;
829     }
830
831     private void processExistingNodeTypeArtifacts(List<ArtifactDefinition> extractedArtifacts, List<ArtifactDefinition> artifactsToUpload,
832                                                   List<ArtifactDefinition> artifactsToUpdate, List<ArtifactDefinition> artifactsToDelete,
833                                                   Map<String, ArtifactDefinition> existingArtifacts) {
834         if (!existingArtifacts.isEmpty()) {
835             extractedArtifacts.forEach(a -> processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, a));
836             artifactsToDelete.addAll(existingArtifacts.values());
837         }
838     }
839
840     private void processNodeTypeArtifact(List<ArtifactDefinition> artifactsToUpload, List<ArtifactDefinition> artifactsToUpdate,
841                                          Map<String, ArtifactDefinition> existingArtifacts, ArtifactDefinition currNewArtifact) {
842         Optional<ArtifactDefinition> foundArtifact = existingArtifacts.values().stream()
843             .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst();
844         if (foundArtifact.isPresent()) {
845             if (foundArtifact.get().getArtifactType().equals(currNewArtifact.getArtifactType())) {
846                 updateFoundArtifact(artifactsToUpdate, currNewArtifact, foundArtifact.get());
847                 existingArtifacts.remove(foundArtifact.get().getArtifactLabel());
848                 artifactsToUpload.remove(currNewArtifact);
849             } else {
850                 log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
851                 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR,
852                     currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), foundArtifact.get().getArtifactType());
853             }
854         }
855     }
856
857     private void updateFoundArtifact(List<ArtifactDefinition> artifactsToUpdate, ArtifactDefinition currNewArtifact,
858                                      ArtifactDefinition foundArtifact) {
859         if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
860             foundArtifact.setPayload(currNewArtifact.getPayloadData());
861             foundArtifact.setPayloadData(Base64.encodeBase64String(currNewArtifact.getPayloadData()));
862             foundArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(currNewArtifact.getPayloadData()));
863             artifactsToUpdate.add(foundArtifact);
864         }
865     }
866
867     private Map<String, ArtifactDefinition> collectExistingArtifacts(Resource curNodeType) {
868         Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>();
869         if (curNodeType == null) {
870             return existingArtifacts;
871         }
872         if (MapUtils.isNotEmpty(curNodeType.getDeploymentArtifacts())) {
873             existingArtifacts.putAll(curNodeType.getDeploymentArtifacts());
874         }
875         if (MapUtils.isNotEmpty(curNodeType.getArtifacts())) {
876             existingArtifacts.putAll(
877                 curNodeType.getArtifacts().entrySet().stream().filter(e -> e.getValue().getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
878                     .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
879         }
880         return existingArtifacts;
881     }
882
883     /**
884      * Changes resource life cycle state to checked out
885      *
886      * @param resource
887      * @param user
888      * @param inTransaction
889      * @return
890      */
891     private Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) {
892         Either<Resource, ResponseFormat> checkoutResourceRes;
893         try {
894             if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState()
895                 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
896                 log.debug("************* Going to change life cycle state of resource {} to not certified checked out. ", resource.getName());
897                 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic
898                     .changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT,
899                         new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true);
900                 if (checkoutRes.isRight()) {
901                     log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ",
902                         resource.getComponentType().getNodeType(), resource.getUniqueId(), checkoutRes.right().value().getStatus());
903                     checkoutResourceRes = Either.right(checkoutRes.right().value());
904                 } else {
905                     checkoutResourceRes = Either.left((Resource) checkoutRes.left().value());
906                 }
907             } else {
908                 checkoutResourceRes = Either.left(resource);
909             }
910         } catch (Exception e) {
911             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
912             checkoutResourceRes = Either.right(responseFormat);
913             log.debug("Exception occurred when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), e);
914         }
915         return checkoutResourceRes;
916     }
917
918     /**
919      * Handles Artifacts of NodeType
920      *
921      * @param nodeTypeResource
922      * @param nodeTypeArtifactsToHandle
923      * @param user
924      * @param inTransaction
925      * @return
926      */
927     public Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource,
928                                                                                     Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
929                                                                                     List<ArtifactDefinition> createdArtifacts, User user,
930                                                                                     boolean inTransaction, boolean ignoreLifecycleState) {
931         List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes;
932         Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null;
933         Either<Resource, ResponseFormat> changeStateResponse;
934         try {
935             changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction);
936             if (changeStateResponse.isRight()) {
937                 return Either.right(changeStateResponse.right().value());
938             }
939             nodeTypeResource = changeStateResponse.left().value();
940             List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>();
941             log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName());
942             for (Entry<ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle.entrySet()) {
943                 ArtifactOperationEnum curOperation = curOperationEntry.getKey();
944                 List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue();
945                 if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) {
946                     log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName());
947                     handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic
948                         .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts,
949                             new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction);
950                     if (ArtifactOperationEnum.isCreateOrLink(curOperation)) {
951                         createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
952                     }
953                     handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
954                 }
955             }
956             handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts);
957         } catch (Exception e) {
958             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
959             handleNodeTypeArtifactsRes = Either.right(responseFormat);
960             log.debug("Exception occurred when handleVfcArtifacts, error is:{}", e.getMessage(), e);
961         }
962         return handleNodeTypeArtifactsRes;
963     }
964
965     private Map<String, ImmutablePair<String, String>> extractVfcToscaNames(final Map<String, NodeTypeInfo> nodeTypesInfo,
966                                                                             final String vfResourceName, final CsarInfo csarInfo) {
967         final Map<String, ImmutablePair<String, String>> vfcToscaNames = new HashMap<>();
968         final Map<String, Object> nodes = extractAllNodes(nodeTypesInfo, csarInfo);
969         if (!nodes.isEmpty()) {
970             for (Entry<String, Object> nodeType : nodes.entrySet()) {
971                 final ImmutablePair<String, String> toscaResourceName = buildNestedToscaResourceName(ResourceTypeEnum.VFC.name(), vfResourceName,
972                         nodeType.getKey());
973                 vfcToscaNames.put(nodeType.getKey(), toscaResourceName);
974             }
975         }
976         for (final NodeTypeInfo cvfc : nodeTypesInfo.values()) {
977             vfcToscaNames.put(cvfc.getType(), buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), vfResourceName, cvfc.getType()));
978         }
979         return vfcToscaNames;
980     }
981
982     private Map<String, Object> extractAllNodes(Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
983         Map<String, Object> nodes = new HashMap<>();
984         for (NodeTypeInfo nodeTypeInfo : nodeTypesInfo.values()) {
985             extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate());
986         }
987         extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate());
988         return nodes;
989     }
990
991     private void extractNodeTypes(Map<String, Object> nodes, Map<String, Object> mappedToscaTemplate) {
992         Either<Map<String, Object>, ResultStatusEnum> eitherNodeTypes = ImportUtils
993             .findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
994         if (eitherNodeTypes.isLeft()) {
995             nodes.putAll(eitherNodeTypes.left().value());
996         }
997     }
998
999     public Resource createResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String csarUUID) {
1000         log.trace("************* created successfully from YAML, resource TOSCA ");
1001         loggerSupportability
1002             .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}",
1003                 user.getUserId());
1004         CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID);
1005         Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo();
1006         Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
1007             nodeTypesInfo, csarInfo, resource);
1008         if (findNodeTypesArtifactsToHandleRes.isRight()) {
1009             log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
1010             loggerSupportability
1011                 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1012                     "error: {}", findNodeTypesArtifactsToHandleRes.right().value());
1013             throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
1014         }
1015         Resource vfResource = createResourceFromYaml(resource, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo,
1016             csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, null);
1017         log.trace("*************VF Resource created successfully from YAML, resource TOSCA name: {}", vfResource.getToscaResourceName());
1018         loggerSupportability
1019             .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.COMPLETE, "Ended create Resource From Csar by user {}",
1020                 user.getUserId());
1021         return vfResource;
1022     }
1023
1024     private Resource validateResourceBeforeCreate(Resource resource, User user, boolean inTransaction) {
1025         log.trace("validating resource before create");
1026         user.copyData(validateUser(user, CREATE_RESOURCE, resource, AuditingActionEnum.CREATE_RESOURCE, false));
1027         // validate user role
1028         validateUserRole(user, resource, new ArrayList<>(), AuditingActionEnum.CREATE_RESOURCE, null);
1029         // VF / PNF "derivedFrom" should be null (or ignored)
1030         if (ModelConverter.isAtomicComponent(resource)) {
1031             validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE);
1032         }
1033         return validateResourceBeforeCreate(resource, user, AuditingActionEnum.CREATE_RESOURCE, inTransaction, null);
1034     }
1035
1036     private Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
1037                                             CsarInfo csarInfo,
1038                                             Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1039                                             boolean shouldLock, boolean inTransaction, String nodeName) {
1040         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
1041         Resource createdResource;
1042         try {
1043             ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
1044                 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
1045             if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
1046                 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
1047             }
1048             log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
1049             loggerSupportability
1050                 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, "");
1051             createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
1052                 createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
1053             log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
1054             loggerSupportability
1055                 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1056                     "The resource has been created: {}", resource.getName());
1057         } catch (ComponentException e) {
1058             ResponseFormat responseFormat =
1059                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
1060             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1061             throw e;
1062         } catch (StorageException e) {
1063             ResponseFormat responseFormat = componentsUtils
1064                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
1065             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1066             throw e;
1067         }
1068         return createdResource;
1069     }
1070
1071     public Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate,
1072                                                                       boolean needLock,
1073                                                                       Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1074                                                                       List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1075                                                                       Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
1076         Either<String, ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
1077         if (toscaVersion.isRight()) {
1078             throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
1079         }
1080         Map<String, Object> mapToConvert = new HashMap<>();
1081         mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
1082         Map<String, Object> nodeTypes = getNodeTypesFromTemplate(mappedToscaTemplate);
1083         createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert,
1084             nodeTypes);
1085         return csarInfo.getCreatedNodes();
1086     }
1087
1088     private Map<String, Object> getNodeTypesFromTemplate(Map<String, Object> mappedToscaTemplate) {
1089         return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES).left().orValue(HashMap::new);
1090     }
1091
1092     private void createNodeTypes(String yamlName, Resource resource, boolean needLock,
1093                                  Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1094                                  List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1095                                  Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
1096         Iterator<Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
1097         Resource vfcCreated = null;
1098         while (nodesNameValueIter.hasNext()) {
1099             Entry<String, Object> nodeType = nodesNameValueIter.next();
1100             Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
1101                 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeType.getKey());
1102             if (nodeTypesInfo.containsKey(nodeType.getKey())) {
1103                 log.trace("************* Going to handle nested vfc {}", nodeType.getKey());
1104                 vfcCreated = handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1105                     nodeType.getKey());
1106                 log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
1107             } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
1108                 .containsKey(nodeType.getKey())) {
1109                 log.trace("************* Going to create node {}", nodeType.getKey());
1110                 ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(),
1111                     mapToConvert, resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
1112                 log.debug("************* Finished to create node {}", nodeType.getKey());
1113                 vfcCreated = resourceCreated.getLeft();
1114                 csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), vfcCreated.getToscaResourceName());
1115             }
1116             if (vfcCreated != null) {
1117                 csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
1118             }
1119             mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
1120         }
1121     }
1122
1123     private Resource handleNestedVfc(Resource resource, Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1124                                      List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1125                                      String nodeName) {
1126         String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
1127         Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
1128         log.debug("************* Going to create node types from yaml {}", yamlName);
1129         createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts,
1130             Collections.emptyMap(), csarInfo);
1131         log.debug("************* Finished to create node types from yaml {}", yamlName);
1132         if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
1133             log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
1134             resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
1135         }
1136         return resource;
1137     }
1138
1139     private Resource handleComplexVfc(final Resource resource,
1140                                       final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1141                                       final List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1142                                       final String nodeName, final String yamlName) {
1143         Resource oldComplexVfc = null;
1144         Resource newComplexVfc = buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo);
1145         Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
1146             .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
1147         if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
1148             oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
1149                 buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getRight());
1150         }
1151         if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
1152             log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
1153                 oldComplexVfcRes.right().value());
1154             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1155         } else if (oldComplexVfcRes.isLeft()) {
1156             log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
1157             final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(),
1158                 newComplexVfc, ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
1159             if (eitherValidation.isLeft()) {
1160                 oldComplexVfc = oldComplexVfcRes.left().value();
1161             }
1162         }
1163         newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
1164             newComplexVfc);
1165         csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
1166         final LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1167             LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1168         log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
1169         final Resource result = propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
1170         csarInfo.getCreatedNodes().put(nodeName, result);
1171         csarInfo.removeNodeFromQueue();
1172         return result;
1173     }
1174
1175     private Resource handleComplexVfc(Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1176                                       List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1177                                       String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) {
1178         Resource handleComplexVfcRes;
1179         Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
1180         String yamlContent = new String(csarInfo.getCsar().get(yamlName));
1181         Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream().collect(toMap(Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
1182         CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
1183         if (oldComplexVfc == null) {
1184             handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
1185                 false, true, nodeName);
1186         } else {
1187             handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts,
1188                 yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
1189         }
1190         return handleComplexVfcRes;
1191     }
1192
1193     private Resource buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, Map<String, NodeTypeInfo> nodesInfo) {
1194         Resource complexVfc = buildComplexVfcMetadata(resource, csarInfo, nodeName, nodesInfo);
1195         log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName());
1196         csarInfo.addNodeToQueue(nodeName);
1197         return validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo);
1198     }
1199
1200     private String getNodeTypeActualName(final String nodeTypefullName, final String nodeTypeNamePrefix) {
1201         final String nameWithouNamespacePrefix = nodeTypefullName.substring(nodeTypeNamePrefix.length());
1202         final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
1203         if (findTypes.length > 1) {
1204             final String resourceType = findTypes[0];
1205             return nameWithouNamespacePrefix.substring(resourceType.length());
1206         }
1207         return nameWithouNamespacePrefix;
1208     }
1209
1210     private ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml(final String yamlName, final Entry<String, Object> nodeNameValue,
1211                                                                                  User user, final Map<String, Object> mapToConvert,
1212                                                                                  final Resource resourceVf, final boolean needLock,
1213                                                                                  final Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1214                                                                                  final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1215                                                                                  final boolean forceCertificationAllowed, final CsarInfo csarInfo,
1216                                                                                  final boolean isNested) {
1217         final UploadResourceInfo resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user);
1218         final String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.getResourceType(), csarInfo);
1219         user = validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true);
1220         return createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, nodeTypeArtifactsToHandle,
1221             nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeNameValue.getKey(), isNested);
1222     }
1223
1224     private String buildNodeTypeYaml(final Entry<String, Object> nodeNameValue, final Map<String, Object> mapToConvert, final String nodeResourceType,
1225                                      final CsarInfo csarInfo) {
1226         // We need to create a Yaml from each node_types in order to create
1227
1228         // resource from each node type using import normative flow.
1229         final DumperOptions options = new DumperOptions();
1230         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
1231         final Yaml yaml = new Yaml(options);
1232         final Map<String, Object> node = new HashMap<>();
1233         node.put(buildNestedToscaResourceName(nodeResourceType, csarInfo.getVfResourceName(), nodeNameValue.getKey()).getLeft(),
1234             nodeNameValue.getValue());
1235         mapToConvert.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), node);
1236         return yaml.dumpAsMap(mapToConvert);
1237     }
1238
1239     public Boolean validateResourceCreationFromNodeType(Resource resource, User creator) {
1240         validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
1241         return true;
1242     }
1243
1244     public ImmutablePair<Resource, ActionStatus> createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator,
1245                                                                             boolean isInTransaction, boolean needLock,
1246                                                                             Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1247                                                                             List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1248                                                                             boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
1249                                                                             boolean isNested) {
1250         LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1251             LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1252         Function<Resource, Boolean> validator = resource -> validateResourceCreationFromNodeType(resource, creator);
1253         return resourceImportManager
1254             .importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock,
1255                 nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested);
1256     }
1257
1258     /**
1259      * Validates if a given node type name has a valid prefix.
1260      *
1261      * @param nodeName                     node name from definition file
1262      * @param definedResourceNamespaceList is a list of all node type name prefix allowed
1263      * @return a valid node type name prefix if it`s found
1264      */
1265     public Optional<String> validateNodeTypeNamePrefix(final String nodeName, final List<String> definedResourceNamespaceList) {
1266         for (final String validNamespace : definedResourceNamespaceList) {
1267             if (nodeName.startsWith(validNamespace)) {
1268                 return Optional.of(validNamespace);
1269             }
1270         }
1271         return Optional.empty();
1272     }
1273
1274     private List<String> getDefinedNodeTypeNamespaceList() {
1275         return ConfigurationManager.getConfigurationManager().getConfiguration().getDefinedResourceNamespace();
1276     }
1277
1278     private UploadResourceInfo fillResourceMetadata(final String yamlName, final Resource resourceVf, final String nodeName, final User user) {
1279         final UploadResourceInfo resourceMetaData = new UploadResourceInfo();
1280         final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeName);
1281         log.debug("Node type Name prefix {}", nodeTypeNamePrefix);
1282         if (!nodeName.startsWith(nodeTypeNamePrefix)) {
1283             log.debug("invalid nodeName:{} does not start with {}.", nodeName, getDefinedNodeTypeNamespaceList());
1284             throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
1285         }
1286         final String actualName = this.getNodeTypeActualName(nodeName, nodeTypeNamePrefix);
1287         final String namePrefix = nodeName.replace(actualName, "");
1288         String resourceType = namePrefix.substring(nodeTypeNamePrefix.length());
1289         log.debug("initial  namePrefix:{} resourceType {}. nodeName {} , actualName {} prefix {}", namePrefix, resourceType, nodeName, actualName,
1290             nodeTypeNamePrefix);
1291         // if we import from csar, the node_type name can be
1292
1293         // org.openecomp.resource.abstract.node_name - in this case we always
1294
1295         // create a vfc
1296         if (resourceType.equals(Constants.ABSTRACT)) {
1297             resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
1298         }
1299         if (!ResourceTypeEnum.containsIgnoreCase(resourceType)) {
1300             resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
1301         }
1302         // validating type
1303         if (!ResourceTypeEnum.containsName(resourceType.toUpperCase())) {
1304             log.debug("invalid resourceType:{} the type is not one of the valide types:{}.", resourceType.toUpperCase(), ResourceTypeEnum.values());
1305             throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
1306         }
1307         // Setting name
1308         resourceMetaData.setName(resourceVf.getSystemName() + actualName);
1309         // Setting type from name
1310         final String type = resourceType.toUpperCase();
1311         resourceMetaData.setResourceType(type);
1312         resourceMetaData.setDescription(ImportUtils.Constants.INNER_VFC_DESCRIPTION);
1313         resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON);
1314         resourceMetaData.setContactId(user.getUserId());
1315         resourceMetaData.setVendorName(resourceVf.getVendorName());
1316         resourceMetaData.setVendorRelease(resourceVf.getVendorRelease());
1317         // Setting tag
1318         final List<String> tags = new ArrayList<>();
1319         tags.add(resourceMetaData.getName());
1320         resourceMetaData.setTags(tags);
1321         // Setting category
1322         final CategoryDefinition category = new CategoryDefinition();
1323         category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
1324         final SubCategoryDefinition subCategory = new SubCategoryDefinition();
1325         subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
1326         category.addSubCategory(subCategory);
1327         final List<CategoryDefinition> categories = new ArrayList<>();
1328         categories.add(category);
1329         resourceMetaData.setCategories(categories);
1330         return resourceMetaData;
1331     }
1332
1333     private Resource buildComplexVfcMetadata(final Resource resourceVf, final CsarInfo csarInfo, final String nodeName,
1334                                              final Map<String, NodeTypeInfo> nodesInfo) {
1335         final Resource cvfc = new Resource();
1336         final NodeTypeInfo nodeTypeInfo = nodesInfo.get(nodeName);
1337         cvfc.setName(buildCvfcName(csarInfo.getVfResourceName(), nodeName));
1338         cvfc.setNormalizedName(ValidationUtils.normaliseComponentName(cvfc.getName()));
1339         cvfc.setSystemName(ValidationUtils.convertToSystemName(cvfc.getName()));
1340         cvfc.setResourceType(ResourceTypeEnum.CVFC);
1341         cvfc.setAbstract(true);
1342         cvfc.setDerivedFrom(nodeTypeInfo.getDerivedFrom());
1343         cvfc.setDescription(ImportUtils.Constants.CVFC_DESCRIPTION);
1344         cvfc.setIcon(ImportUtils.Constants.DEFAULT_ICON);
1345         cvfc.setContactId(csarInfo.getModifier().getUserId());
1346         cvfc.setCreatorUserId(csarInfo.getModifier().getUserId());
1347         cvfc.setVendorName(resourceVf.getVendorName());
1348         cvfc.setVendorRelease(resourceVf.getVendorRelease());
1349         cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber());
1350         cvfc.setToscaResourceName(buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getLeft());
1351         cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1352         final List<String> tags = new ArrayList<>();
1353         tags.add(cvfc.getName());
1354         cvfc.setTags(tags);
1355         final CategoryDefinition category = new CategoryDefinition();
1356         category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
1357         SubCategoryDefinition subCategory = new SubCategoryDefinition();
1358         subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
1359         category.addSubCategory(subCategory);
1360         final List<CategoryDefinition> categories = new ArrayList<>();
1361         categories.add(category);
1362         cvfc.setCategories(categories);
1363         cvfc.setVersion(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION);
1364         cvfc.setLifecycleState(ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT);
1365         cvfc.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
1366         return cvfc;
1367     }
1368
1369     private String buildCvfcName(final String resourceVfName, final String nodeName) {
1370         String nameWithouNamespacePrefix = nodeName.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length());
1371         String[] findTypes = nameWithouNamespacePrefix.split("\\.");
1372         String resourceType = findTypes[0];
1373         String resourceName = resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1);
1374         return addCvfcSuffixToResourceName(resourceName);
1375     }
1376
1377     private Resource createResourceAndRIsFromYaml(final String yamlName, Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo,
1378                                                   final AuditingActionEnum actionEnum, final boolean isNormative,
1379                                                   final List<ArtifactDefinition> createdArtifacts, final String topologyTemplateYaml,
1380                                                   final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo,
1381                                                   final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1382                                                   final boolean shouldLock, final boolean inTransaction, final String nodeName) {
1383         final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
1384         if (shouldLock) {
1385             final Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
1386             if (lockResult.isRight()) {
1387                 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1388                 throw new ByResponseFormatComponentException(lockResult.right().value());
1389             }
1390             log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
1391         }
1392         try {
1393             log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
1394             loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1395                 "Starting to add inputs from yaml: {}", yamlName);
1396             final Resource genericResource = fetchAndSetDerivedFromGenericType(resource);
1397             resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
1398             log.trace("************* createResourceFromYaml after full create resource {}", yamlName);
1399             log.trace("************* Going to add inputs from yaml {}", yamlName);
1400             if (resource.shouldGenerateInputs()) {
1401                 generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
1402             }
1403             final Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
1404             resource = createInputsOnResource(resource, inputs);
1405             log.trace("************* Finish to add inputs from yaml {}", yamlName);
1406             loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1407                 "Finish to add inputs from yaml: {}", yamlName);
1408             if (resource.getResourceType() == ResourceTypeEnum.PNF) {
1409                 log.trace("************* Adding generic properties to PNF");
1410                 resource = (Resource) propertyBusinessLogic.copyPropertyToComponent(resource, genericResource.getProperties());
1411                 log.trace("************* Adding software information to PNF");
1412                 softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
1413                 log.trace("************* Removing non-mano software information file from PNF");
1414                 if (csarInfo.getSoftwareInformationPath().isPresent() && !softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo)) {
1415                     log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), "catalog-be",
1416                         "Could not remove the software information file.");
1417                 }
1418             }
1419             final Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
1420             log.trace("************* Going to create nodes, RI's and Relations  from yaml {}", yamlName);
1421             loggerSupportability
1422                 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1423                     "Start create nodes, RI and Relations  from yaml: {}", yamlName);
1424             resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, topologyTemplateYaml,
1425                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
1426             log.trace("************* Finished to create nodes, RI and Relation  from yaml {}", yamlName);
1427             loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1428                 "Finished to create nodes, RI and Relation  from yaml: {}", yamlName);
1429             // validate update vf module group names
1430             final Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
1431                 .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName());
1432             if (validateUpdateVfGroupNamesRes.isRight()) {
1433                 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1434                 throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
1435             }
1436             // add groups to resource
1437             final Map<String, GroupDefinition> groups;
1438             log.trace("************* Going to add groups from yaml {}", yamlName);
1439             loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1440                 "Start to add groups from yaml: {}", yamlName);
1441             if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
1442                 groups = validateUpdateVfGroupNamesRes.left().value();
1443             } else {
1444                 groups = parsedToscaYamlInfo.getGroups();
1445             }
1446             final Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
1447             if (createGroupsOnResource.isRight()) {
1448                 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1449                 loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1450                     "ERROR while adding groups from yaml: {}", yamlName);
1451                 throw new ByResponseFormatComponentException(createGroupsOnResource.right().value());
1452             }
1453             resource = createGroupsOnResource.left().value();
1454             log.trace("************* Finished to add groups from yaml {}", yamlName);
1455             loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1456                 "Finished to add groups from yaml: {}", yamlName);
1457             log.trace("************* Going to add artifacts from yaml {}", yamlName);
1458             loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1459                 "Started to add artifacts from yaml: {}", yamlName);
1460             log.trace("************* Starting to add policies from yaml {}", yamlName);
1461             Map<String, PolicyDefinition> policies = parsedToscaYamlInfo.getPolicies();
1462             if (MapUtils.isNotEmpty(policies)) {
1463                 resource = createPoliciesOnResource(resource, policies);
1464             }
1465             log.trace("************* Finished to add policies from yaml {}", yamlName);
1466             final NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,
1467                 nodeTypesArtifactsToCreate);
1468             final Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.CREATE, createdArtifacts,
1469                 yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
1470             if (createArtifactsEither.isRight()) {
1471                 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1472                 loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1473                     "error happened {}", createArtifactsEither.right().value());
1474                 throw new ByResponseFormatComponentException(createArtifactsEither.right().value());
1475             }
1476             loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1477                 "Finished to add artifacts from yaml: " + resource.getToscaResourceName());
1478             final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
1479             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
1480             ASDCKpiApi.countCreatedResourcesKPI();
1481             return resource;
1482         } catch (final BusinessLogicException e) {
1483             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
1484                 "An error has occurred during resource and resource instance creation", e);
1485             rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1486             throw new ByResponseFormatComponentException(e.getResponseFormat());
1487         } catch (final ComponentException e) {
1488                         log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ResourceBusinessLogic.class.getName(),
1489                                 "An error has occurred during resource and resource instance creation", e);
1490                         rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1491                         throw new ByResponseFormatComponentException(e.getResponseFormat());
1492                 } catch (final Exception e) {
1493             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
1494                 "An error has occurred during resource and resource instance creation", e);
1495             rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1496             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1497         } finally {
1498             if (!inTransaction) {
1499                 janusGraphDao.commit();
1500             }
1501             if (shouldLock) {
1502                 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
1503             }
1504         }
1505     }
1506
1507     private void rollback(boolean inTransaction, Resource resource, List<ArtifactDefinition> createdArtifacts,
1508                           List<ArtifactDefinition> nodeTypesNewCreatedArtifacts) {
1509         if (!inTransaction) {
1510             janusGraphDao.rollback();
1511         }
1512         if (isNotEmpty(createdArtifacts) && isNotEmpty(nodeTypesNewCreatedArtifacts)) {
1513             createdArtifacts.addAll(nodeTypesNewCreatedArtifacts);
1514             log.debug("Found {} newly created artifacts to deleted, the component name: {}", createdArtifacts.size(), resource.getName());
1515         }
1516     }
1517
1518     private Resource getResourceWithGroups(String resourceId) {
1519         ComponentParametersView filter = new ComponentParametersView();
1520         filter.setIgnoreGroups(false);
1521         Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter);
1522         if (updatedResource.isRight()) {
1523             rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId);
1524         }
1525         return updatedResource.left().value();
1526     }
1527
1528     private Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
1529         if (groups != null && !groups.isEmpty()) {
1530             List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
1531             handleGroupsProperties(resource, groups);
1532             fillGroupsFinalFields(groupsAsList);
1533             Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
1534             if (createGroups.isRight()) {
1535                 return Either.right(createGroups.right().value());
1536             }
1537         }
1538         return Either.left(resource);
1539     }
1540
1541     private void handleGroupsProperties(Resource resource, Map<String, GroupDefinition> groups) {
1542         List<InputDefinition> inputs = resource.getInputs();
1543         if (MapUtils.isNotEmpty(groups)) {
1544             groups.values().stream().filter(g -> isNotEmpty(g.getProperties())).flatMap(g -> g.getProperties().stream())
1545                 .forEach(p -> handleGetInputs(p, inputs));
1546         }
1547     }
1548
1549     private Resource createPoliciesOnResource(Resource resource, Map<String, PolicyDefinition> policies) {
1550         policyBusinessLogic.createPoliciesFromParsedCsar(resource, policies);
1551         return resource;
1552     }
1553
1554     private void handleGetInputs(PropertyDataDefinition property, List<InputDefinition> inputs) {
1555         if (isNotEmpty(property.getGetInputValues())) {
1556             if (inputs == null || inputs.isEmpty()) {
1557                 log.debug("Failed to add property {} to group. Inputs list is empty ", property);
1558                 rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
1559                     property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
1560             }
1561             ListIterator<GetInputValueDataDefinition> getInputValuesIter = property.getGetInputValues().listIterator();
1562             while (getInputValuesIter.hasNext()) {
1563                 GetInputValueDataDefinition getInput = getInputValuesIter.next();
1564                 Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
1565                 if (inputEither.isRight()) {
1566                     throw inputEither.right().value();
1567                 } else {
1568                     InputDefinition input = inputEither.left().value();
1569                     getInput.setInputId(input.getUniqueId());
1570                     if (getInput.getGetInputIndex() != null) {
1571                         GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1572                         Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
1573                         if (newInputEither.isRight()) {
1574                             throw newInputEither.right().value();
1575                         } else {
1576                             InputDefinition newInput = newInputEither.left().value();
1577                             getInputIndex.setInputId(newInput.getUniqueId());
1578                         }
1579                         getInputValuesIter.add(getInputIndex);
1580                     }
1581                 }
1582             }
1583         }
1584     }
1585
1586     <T> Either<T, RuntimeException> rollbackWithEither(final ActionStatus actionStatus, final String... params) {
1587         return rollbackWithEither(janusGraphDao, actionStatus, params);
1588     }
1589
1590     private Either<InputDefinition, RuntimeException> findInputByName(List<InputDefinition> inputs, GetInputValueDataDefinition getInput) {
1591         final String inputName = getInput != null ? getInput.getInputName() : "";
1592         if (inputs == null || inputs.isEmpty()) {
1593             log.debug("#findInputByName - Inputs list is empty");
1594             return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
1595         } else {
1596             Optional<InputDefinition> inputOpt = inputs.stream().filter(p -> p.getName().equals(inputName)).findFirst();
1597             if (inputOpt.isEmpty()) {
1598                 log.debug("#findInputByName - Failed to find the input {} ", inputName);
1599                 return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
1600             } else {
1601                 return Either.left(inputOpt.get());
1602             }
1603         }
1604     }
1605
1606     private void fillGroupsFinalFields(List<GroupDefinition> groupsAsList) {
1607         groupsAsList.forEach(groupDefinition -> {
1608             groupDefinition.setInvariantName(groupDefinition.getName());
1609             groupDefinition.setCreatedFrom(CreatedFrom.CSAR);
1610         });
1611     }
1612
1613     private Resource updateGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
1614         if (isEmpty(groups)) {
1615             return resource;
1616         }
1617         return updateOrCreateGroups(resource, groups);
1618     }
1619
1620     private Resource updateOrCreateGroups(Resource resource, Map<String, GroupDefinition> groups) {
1621         List<GroupDefinition> groupsFromResource = resource.getGroups();
1622         List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
1623         List<GroupDefinition> groupsToUpdate = new ArrayList<>();
1624         List<GroupDefinition> groupsToDelete = new ArrayList<>();
1625         List<GroupDefinition> groupsToCreate = new ArrayList<>();
1626         if (isNotEmpty(groupsFromResource)) {
1627             addGroupsToCreateOrUpdate(groupsFromResource, groupsAsList, groupsToUpdate, groupsToCreate);
1628             addGroupsToDelete(groupsFromResource, groupsAsList, groupsToDelete);
1629         } else {
1630             groupsToCreate.addAll(groupsAsList);
1631         }
1632         if (isNotEmpty(groupsToCreate)) {
1633             fillGroupsFinalFields(groupsToCreate);
1634             if (isNotEmpty(groupsFromResource)) {
1635                 groupBusinessLogic.addGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
1636             } else {
1637                 groupBusinessLogic.createGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
1638             }
1639         }
1640         if (isNotEmpty(groupsToDelete)) {
1641             groupBusinessLogic.deleteGroups(resource, groupsToDelete).left().on(this::throwComponentException);
1642         }
1643         if (isNotEmpty(groupsToUpdate)) {
1644             groupBusinessLogic.updateGroups(resource, groupsToUpdate, true).left().on(this::throwComponentException);
1645         }
1646         return resource;
1647     }
1648
1649     private void addGroupsToDelete(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
1650                                    List<GroupDefinition> groupsToDelete) {
1651         for (GroupDefinition group : groupsFromResource) {
1652             Optional<GroupDefinition> op = groupsAsList.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
1653                 .findAny();
1654             if (op.isEmpty() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
1655                 groupsToDelete.add(group);
1656             }
1657         }
1658     }
1659
1660     private void addGroupsToCreateOrUpdate(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
1661                                            List<GroupDefinition> groupsToUpdate, List<GroupDefinition> groupsToCreate) {
1662         for (GroupDefinition group : groupsAsList) {
1663             Optional<GroupDefinition> op = groupsFromResource.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
1664                 .findAny();
1665             if (op.isPresent()) {
1666                 GroupDefinition groupToUpdate = op.get();
1667                 groupToUpdate.setMembers(group.getMembers());
1668                 groupToUpdate.setCapabilities(group.getCapabilities());
1669                 groupToUpdate.setProperties(group.getProperties());
1670                 groupsToUpdate.add(groupToUpdate);
1671             } else {
1672                 groupsToCreate.add(group);
1673             }
1674         }
1675     }
1676
1677     private Resource createInputsOnResource(Resource resource, Map<String, InputDefinition> inputs) {
1678         List<InputDefinition> resourceProperties = resource.getInputs();
1679         if (MapUtils.isNotEmpty(inputs) || isNotEmpty(resourceProperties)) {
1680             Either<List<InputDefinition>, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, resource);
1681             if (createInputs.isRight()) {
1682                 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1683                     "failed to add inputs from yaml: {}", createInputs.right().value());
1684                 throw new ByResponseFormatComponentException(createInputs.right().value());
1685             }
1686             resource.setInputs(createInputs.left().value());
1687         }
1688         return resource;
1689     }
1690
1691     private List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
1692         List<GroupDefinition> result = new ArrayList<>();
1693         List<ComponentInstance> componentInstances = component.getComponentInstances();
1694         if (groups != null) {
1695             Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = validateCyclicGroupsDependencies(groups);
1696             if (validateCyclicGroupsDependencies.isRight()) {
1697                 throw new ByResponseFormatComponentException(validateCyclicGroupsDependencies.right().value());
1698             }
1699             for (Entry<String, GroupDefinition> entry : groups.entrySet()) {
1700                 String groupName = entry.getKey();
1701                 GroupDefinition groupDefinition = entry.getValue();
1702                 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
1703                 updatedGroupDefinition.setMembers(null);
1704                 Map<String, String> members = groupDefinition.getMembers();
1705                 if (members != null) {
1706                     updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
1707                 }
1708                 result.add(updatedGroupDefinition);
1709             }
1710         }
1711         return result;
1712     }
1713
1714     private void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
1715                                     List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
1716         Set<String> compInstancesNames = members.keySet();
1717         if (CollectionUtils.isEmpty(componentInstances)) {
1718             String membersAstString = String.join(",", compInstancesNames);
1719             log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
1720                 groupName, component.getNormalizedName());
1721             throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
1722                 component.getNormalizedName(), getComponentTypeForResponse(component));
1723         }
1724         // Find all component instances with the member names
1725         Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
1726         memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
1727         Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
1728             .collect(toMap(Entry::getKey, Entry::getValue));
1729         if (relevantInstances.size() != compInstancesNames.size()) {
1730             List<String> foundMembers = new ArrayList<>(relevantInstances.keySet());
1731             foundMembers.forEach(compInstancesNames::remove);
1732             String membersAstString = String.join(",", compInstancesNames);
1733             log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, groupName, component.getNormalizedName());
1734             throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
1735                 component.getNormalizedName(), getComponentTypeForResponse(component));
1736         }
1737         updatedGroupDefinition.setMembers(relevantInstances);
1738     }
1739
1740     /**
1741      * This Method validates that there is no cyclic group dependencies. meaning group A as member in group B which is member in group A
1742      *
1743      * @param allGroups
1744      * @return
1745      */
1746     private Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies(Map<String, GroupDefinition> allGroups) {
1747         Either<Boolean, ResponseFormat> result = Either.left(true);
1748         try {
1749             Iterator<Entry<String, GroupDefinition>> allGroupsItr = allGroups.entrySet().iterator();
1750             while (allGroupsItr.hasNext() && result.isLeft()) {
1751                 Entry<String, GroupDefinition> groupAEntry = allGroupsItr.next();
1752                 // Fetches a group member A
1753                 String groupAName = groupAEntry.getKey();
1754                 // Finds all group members in group A
1755                 Set<String> allGroupAMembersNames = new HashSet<>();
1756                 fillAllGroupMemebersRecursivly(groupAEntry.getKey(), allGroups, allGroupAMembersNames);
1757                 // If A is a group member of itself found cyclic dependency
1758                 if (allGroupAMembersNames.contains(groupAName)) {
1759                     ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_HAS_CYCLIC_DEPENDENCY, groupAName);
1760                     result = Either.right(responseFormat);
1761                 }
1762             }
1763         } catch (Exception e) {
1764             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1765             result = Either.right(responseFormat);
1766             log.debug("Exception occurred when validateCyclicGroupsDependencies, error is:{}", e.getMessage(), e);
1767         }
1768         return result;
1769     }
1770
1771     /**
1772      * This Method fills recursively the set groupMembers with all the members of the given group which are also of type group.
1773      *
1774      * @param groupName
1775      * @param allGroups
1776      * @param allGroupMembers
1777      * @return
1778      */
1779     private void fillAllGroupMemebersRecursivly(String groupName, Map<String, GroupDefinition> allGroups, Set<String> allGroupMembers) {
1780         // Found Cyclic dependency
1781         if (isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers)) {
1782             return;
1783         }
1784         GroupDefinition groupDefinition = allGroups.get(groupName);
1785         // All Members Of Current Group Resource Instances & Other Groups
1786         Set<String> currGroupMembers = groupDefinition.getMembers().keySet();
1787         // Filtered Members Of Current Group containing only members which
1788
1789         // are groups
1790         List<String> currGroupFilteredMembers = currGroupMembers.stream().
1791             // Keep Only Elements of type group and not Resource Instances
1792                 filter(allGroups::containsKey).
1793             // Add Filtered Elements to main Set
1794                 peek(allGroupMembers::add).
1795             // Collect results
1796                 collect(toList());
1797         // Recursively call the method for all the filtered group members
1798         for (String innerGroupName : currGroupFilteredMembers) {
1799             fillAllGroupMemebersRecursivly(innerGroupName, allGroups, allGroupMembers);
1800         }
1801     }
1802
1803     private boolean isfillGroupMemebersRecursivlyStopCondition(String groupName, Map<String, GroupDefinition> allGroups,
1804                                                                Set<String> allGroupMembers) {
1805         boolean stop = !allGroups.containsKey(groupName);
1806         // In Case Not Group Stop
1807         // In Case Group Has no members stop
1808         if (!stop) {
1809             GroupDefinition groupDefinition = allGroups.get(groupName);
1810             stop = isEmpty(groupDefinition.getMembers());
1811         }
1812         // In Case all group members already contained stop
1813         if (!stop) {
1814             final Set<String> allMembers = allGroups.get(groupName).getMembers().keySet();
1815             Set<String> membersOfTypeGroup = allMembers.stream().
1816                 // Filter In Only Group members
1817                     filter(allGroups::containsKey).
1818                 // Collect
1819                     collect(toSet());
1820             stop = allGroupMembers.containsAll(membersOfTypeGroup);
1821         }
1822         return stop;
1823     }
1824
1825     private Resource createRIAndRelationsFromYaml(String yamlName, Resource resource,
1826                                                   Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
1827                                                   String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1828                                                   Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1829                                                   Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1830                                                   String nodeName) {
1831         log.debug("************* Going to create all nodes {}", yamlName);
1832         handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo,
1833             csarInfo, nodeName);
1834         log.debug("************* Finished to create all nodes {}", yamlName);
1835         log.debug("************* Going to create all resource instances {}", yamlName);
1836         Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
1837         resource = createResourceInstances(yamlName, resource, null, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes(),
1838             existingNodeTypesByResourceNames);
1839         log.debug("************* Finished to create all resource instances {}", yamlName);
1840         log.debug("************* Going to create all relations {}", yamlName);
1841         resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, null, uploadComponentInstanceInfoMap,
1842             existingNodeTypesByResourceNames);
1843         log.debug("************* Finished to create all relations {}", yamlName);
1844         log.debug("************* Going to create positions {}", yamlName);
1845         compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId());
1846         log.debug("************* Finished to set positions {}", yamlName);
1847         return resource;
1848     }
1849
1850     private void handleAndAddExtractedVfcsArtifacts(List<ArtifactDefinition> vfcArtifacts, List<ArtifactDefinition> artifactsToAdd) {
1851         List<String> vfcArtifactNames = vfcArtifacts.stream().map(ArtifactDataDefinition::getArtifactName).collect(toList());
1852         artifactsToAdd.forEach(a -> {
1853             if (!vfcArtifactNames.contains(a.getArtifactName())) {
1854                 vfcArtifacts.add(a);
1855             } else {
1856                 log.debug("Can't upload two artifact with the same name {}. ", a.getArtifactName());
1857             }
1858         });
1859     }
1860
1861     @SuppressWarnings("unchecked")
1862     private void handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock,
1863                                  Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1864                                  List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1865                                  String nodeName) {
1866         try {
1867             for (Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
1868                 if (nodeTypeEntry.getValue().isNested() && !nodeTypeAlreadyExists(nodeTypeEntry.getKey())) {
1869                     handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1870                         nodeTypeEntry.getKey());
1871                     log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
1872                 }
1873             }
1874             Map<String, Object> mappedToscaTemplate = null;
1875             if (StringUtils.isNotEmpty(nodeName) && isNotEmpty(nodeTypesInfo) && nodeTypesInfo.containsKey(nodeName)) {
1876                 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
1877             }
1878             if (isEmpty(mappedToscaTemplate)) {
1879                 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
1880             }
1881             createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
1882                 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
1883         } catch (ComponentException e) {
1884             ResponseFormat responseFormat =
1885                 e.getResponseFormat() != null ? e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1886             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1887             throw e;
1888         } catch (StorageException e) {
1889             ResponseFormat responseFormat = componentsUtils
1890                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
1891             componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1892             throw e;
1893         }
1894     }
1895     
1896     private boolean nodeTypeAlreadyExists(final String toscaResourceName) {
1897         return toscaOperationFacade.getLatestByToscaResourceName(toscaResourceName).isLeft();
1898     }
1899
1900     private Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts,
1901                                                                    ArtifactOperationInfo artifactOperation, boolean shouldLock,
1902                                                                    boolean inTransaction) {
1903         if (csarInfo.getCsar() != null) {
1904             String vendorLicenseModelId = null;
1905             String vfLicenseModelId = null;
1906             if (artifactOperation.isUpdate()) {
1907                 Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts();
1908                 if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
1909                     for (Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
1910                         if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
1911                             vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
1912                         }
1913                         if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
1914                             vfLicenseModelId = artifactEntry.getValue().getUniqueId();
1915                         }
1916                     }
1917                 }
1918             }
1919             // Specific Behavior for license artifacts
1920             createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL,
1921                 Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
1922                 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
1923                 artifactOperation, null, true, shouldLock, inTransaction);
1924             createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
1925                 ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
1926                 Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock,
1927                 inTransaction);
1928             Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock,
1929                 inTransaction, artifactOperation);
1930             if (eitherCreateResult.isRight()) {
1931                 return Either.right(eitherCreateResult.right().value());
1932             }
1933             Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils
1934                 .getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
1935             if (artifacsMetaCsarStatus.isLeft()) {
1936                 String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
1937                 String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
1938                 Either<Resource, ResponseFormat> createArtifactsFromCsar;
1939                 if (artifactOperation.isCreateOrLink()) {
1940                     createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
1941                         .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts);
1942                 } else {
1943                     Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
1944                         .updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock,
1945                             inTransaction);
1946                     if ((result.left().value() instanceof Resource) && result.isLeft()) {
1947                         Resource service1 = (Resource) result.left().value();
1948                         createArtifactsFromCsar = Either.left(service1);
1949                     } else {
1950                         createArtifactsFromCsar = Either.right(result.right().value());
1951                     }
1952                 }
1953                 if (createArtifactsFromCsar.isRight()) {
1954                     log.debug("Couldn't create artifacts from artifacts.meta");
1955                     return Either.right(createArtifactsFromCsar.right().value());
1956                 }
1957                 return Either.left(createArtifactsFromCsar.left().value());
1958             } else {
1959                 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction);
1960             }
1961         }
1962         return Either.left(resource);
1963     }
1964
1965     private Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, String artifactPath,
1966                                                                                 String artifactFileName, String artifactType,
1967                                                                                 ArtifactGroupTypeEnum artifactGroupType, String artifactLabel,
1968                                                                                 String artifactDisplayName, String artifactDescription,
1969                                                                                 String artifactId, ArtifactOperationInfo operation,
1970                                                                                 List<ArtifactDefinition> createdArtifacts, boolean isFromCsar,
1971                                                                                 boolean shouldLock, boolean inTransaction) {
1972         byte[] artifactFileBytes = null;
1973         if (csarInfo.getCsar().containsKey(artifactPath)) {
1974             artifactFileBytes = csarInfo.getCsar().get(artifactPath);
1975         }
1976         Either<Boolean, ResponseFormat> result = Either.left(true);
1977         if (operation.isUpdate() || operation.isDelete()) {
1978             if (isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
1979                 Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
1980                     .handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, shouldLock, inTransaction);
1981                 if (handleDelete.isRight()) {
1982                     result = Either.right(handleDelete.right().value());
1983                 } else {
1984                     ArtifactDefinition value = handleDelete.left().value();
1985                     String updatedArtifactId = value.getUniqueId();
1986                     if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
1987                         resource.getDeploymentArtifacts().remove(updatedArtifactId);
1988                     } else {
1989                         resource.getArtifacts().remove(updatedArtifactId);
1990                     }
1991                 }
1992                 return result;
1993             }
1994             if (StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
1995                 operation = new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE);
1996             }
1997         }
1998         if (artifactFileBytes != null) {
1999             Map<String, Object> vendorLicenseModelJson = ArtifactUtils
2000                 .buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName,
2001                     artifactDescription, artifactFileBytes, null, isFromCsar);
2002             Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic
2003                 .createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), vendorLicenseModelJson, operation);
2004             addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
2005             if (eitherNonMetaArtifacts.isRight()) {
2006                 BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact",
2007                     "Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), ErrorSeverity.WARNING);
2008                 return Either.right(eitherNonMetaArtifacts.right().value());
2009             }
2010             ArtifactDefinition artifactDefinition = eitherNonMetaArtifacts.left().value().left().value();
2011             createOrUpdateResourceWithUpdatedArtifact(artifactDefinition, resource, artifactGroupType);
2012         }
2013         return result;
2014     }
2015
2016     private void createOrUpdateResourceWithUpdatedArtifact(ArtifactDefinition artifact, Resource resource, ArtifactGroupTypeEnum groupTypeEnum) {
2017         if (groupTypeEnum == ArtifactGroupTypeEnum.DEPLOYMENT) {
2018             resource.getDeploymentArtifacts().put(artifact.getArtifactLabel(), artifact);
2019         } else {
2020             resource.getArtifacts().put(artifact.getArtifactLabel(), artifact);
2021         }
2022     }
2023
2024     private boolean isArtifactDeletionRequired(String artifactId, byte[] artifactFileBytes, boolean isFromCsar) {
2025         return !StringUtils.isEmpty(artifactId) && artifactFileBytes == null && isFromCsar;
2026     }
2027
2028     private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts,
2029                                                              Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts) {
2030         if (operation.isCreateOrLink() && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) {
2031             Either<ArtifactDefinition, Operation> eitherResult = eitherNonMetaArtifacts.left().value();
2032             if (eitherResult.isLeft()) {
2033                 createdArtifacts.add(eitherResult.left().value());
2034             }
2035         }
2036     }
2037
2038     private Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource,
2039                                                                             List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
2040                                                                             boolean inTransaction, ArtifactOperationInfo artifactOperation) {
2041         Either<Resource, ResponseFormat> resStatus = null;
2042         Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
2043         try {
2044             Either<List<NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
2045             if (artifactPathAndNameList.isRight()) {
2046                 return Either.right(
2047                     getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value()));
2048             }
2049             EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
2050             if (artifactOperation.isCreateOrLink()) {
2051                 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
2052                 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
2053             } else {
2054                 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
2055                     resource, artifactPathAndNameList.left().value(), csarInfo.getModifier());
2056                 if (findVfCsarArtifactsToHandleRes.isRight()) {
2057                     resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
2058                 }
2059                 if (resStatus == null) {
2060                     vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
2061                 }
2062             }
2063             if (resStatus == null && vfCsarArtifactsToHandle != null) {
2064                 resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle);
2065             }
2066             if (resStatus == null) {
2067                 resStatus = Either.left(resource);
2068             }
2069         } catch (Exception e) {
2070             resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
2071             log.debug("Exception occurred in createNonMetaArtifacts, message:{}", e.getMessage(), e);
2072         } finally {
2073             CsarUtils.handleWarningMessages(collectedWarningMessages);
2074         }
2075         return resStatus;
2076     }
2077
2078     private Either<Resource, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Resource resource, List<ArtifactDefinition> createdArtifacts,
2079                                                                   boolean shouldLock, boolean inTransaction,
2080                                                                   Either<Resource, ResponseFormat> resStatus,
2081                                                                   EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
2082         for (Entry<ArtifactOperationEnum, List<NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle.entrySet()) {
2083             Optional<ResponseFormat> optionalCreateInDBError =
2084                 // Stream of artifacts to be created
2085                 currArtifactOperationPair.getValue().stream()
2086                     // create each artifact
2087                     .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), e.getArtifactName(), e.getArtifactType(),
2088                         e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), CsarUtils.ARTIFACT_CREATED_FROM_CSAR,
2089                         e.getArtifactUniqueId(), new ArtifactOperationInfo(false, false, currArtifactOperationPair.getKey()), createdArtifacts,
2090                         e.isFromCsar(), shouldLock, inTransaction))
2091                     // filter in only error
2092                     .filter(Either::isRight).
2093                     // Convert the error from either to
2094
2095                     // ResponseFormat
2096                         map(e -> e.right().value()).
2097                     // Check if an error occurred
2098                         findAny();
2099             // Error found on artifact Creation
2100             if (optionalCreateInDBError.isPresent()) {
2101                 resStatus = Either.right(optionalCreateInDBError.get());
2102                 break;
2103             }
2104         }
2105         return resStatus;
2106     }
2107
2108     private Either<List<NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo,
2109                                                                             Map<String, Set<List<String>>> collectedWarningMessages) {
2110         List<NonMetaArtifactInfo> artifactPathAndNameList =
2111             // Stream of file paths contained in csar
2112             csarInfo.getCsar().entrySet().stream()
2113                 // Filter in only VF artifact path location
2114                 .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
2115                 // Validate and add warnings
2116                 .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages))
2117                 // Filter in Non Warnings
2118                 .filter(Either::isLeft)
2119                 // Convert from Either to NonMetaArtifactInfo
2120                 .map(e -> e.left().value())
2121                 // collect to List
2122                 .collect(toList());
2123         Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME);
2124         for (NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) {
2125             if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) {
2126                 return Either.right(nonMetaArtifactInfo.getArtifactName());
2127             }
2128         }
2129         return Either.left(artifactPathAndNameList);
2130     }
2131
2132     private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(Resource resource,
2133                                                                                                                           List<NonMetaArtifactInfo> artifactPathAndNameList,
2134                                                                                                                           User user) {
2135         List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
2136         // collect all Deployment and Informational artifacts of VF
2137         if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) {
2138             existingArtifacts.addAll(resource.getDeploymentArtifacts().values());
2139         }
2140         if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) {
2141             existingArtifacts.addAll(resource.getArtifacts().values());
2142         }
2143         existingArtifacts = existingArtifacts.stream()
2144             // filter MANDATORY artifacts, LICENSE artifacts and artifacts
2145
2146             // was created from HEAT.meta
2147             .filter(this::isNonMetaArtifact).collect(toList());
2148         List<String> artifactsToIgnore = new ArrayList<>();
2149         // collect IDs of Artifacts of VF which belongs to any group
2150         if (resource.getGroups() != null) {
2151             resource.getGroups().forEach(g -> {
2152                 if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
2153                     artifactsToIgnore.addAll(g.getArtifacts());
2154                 }
2155             });
2156         }
2157         existingArtifacts = existingArtifacts.stream()
2158             // filter artifacts which belongs to any group
2159             .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
2160         return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user);
2161     }
2162
2163     private boolean isNonMetaArtifact(ArtifactDefinition artifact) {
2164         return !artifact.getMandatory() && artifact.getArtifactName() != null && isValidArtifactType(artifact);
2165     }
2166
2167     private boolean isValidArtifactType(ArtifactDefinition artifact) {
2168         return artifact.getArtifactType() != null && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VENDOR_LICENSE
2169                 && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VF_LICENSE;
2170     }
2171
2172     private Resource createResourceInstancesRelations(User user, String yamlName, Resource resource, Resource oldResource,
2173                                                       Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2174                                                       Map<String, Resource> existingNodeTypesByResourceNames) {
2175         log.debug("#createResourceInstancesRelations - Going to create relations ");
2176         loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
2177             "Start to create relations");
2178         List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
2179         if (isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList) &&
2180             resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances {
2181             log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ",
2182                 resource.getUniqueId(), yamlName);
2183             loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2184                 "No instances found in the resource: {}, is empty, yaml template file name: {}", resource.getName(), yamlName);
2185             BeEcompErrorManager.getInstance()
2186                 .logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ",
2187                     ErrorSeverity.ERROR);
2188             throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2189         }
2190         Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
2191         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
2192         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
2193         Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
2194         Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
2195         Map<String, List<AttributeDefinition>> instAttributes = new HashMap<>();
2196         List<RequirementCapabilityRelDef> relations = new ArrayList<>();
2197         Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
2198         log.debug("#createResourceInstancesRelations - Before get all datatypes. ");
2199         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll();
2200         if (allDataTypes.isRight()) {
2201             JanusGraphOperationStatus status = allDataTypes.right().value();
2202             BeEcompErrorManager.getInstance()
2203                 .logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status,
2204                     ErrorSeverity.ERROR);
2205             loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2206                 "ERROR while update property value on instance. Status is: " + status);
2207             throw new ByActionStatusComponentException(
2208                 componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName);
2209         }
2210         Resource finalResource = resource;
2211         uploadResInstancesMap.values().forEach(
2212             i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes, instProperties, instCapabilities,
2213                 instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, existingNodeTypesByResourceNames, instInputs, i));
2214         resource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar()).forEach(
2215             i -> processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts,
2216                 instProperties, instInputs, instAttributes));
2217         associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties);
2218         associateComponentInstanceInputsToComponent(yamlName, resource, instInputs);
2219         associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts);
2220         associateArtifactsToInstances(yamlName, resource, instArtifacts);
2221         associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements);
2222         associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes);
2223         addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations);
2224         associateResourceInstances(yamlName, resource, relations);
2225         handleSubstitutionMappings(resource, uploadResInstancesMap);
2226         log.debug("************* in create relations, getResource start");
2227         loggerSupportability
2228             .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, "create relations");
2229         Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaFullElement(resource.getUniqueId());
2230         log.debug("************* in create relations, getResource end");
2231         if (eitherGetResource.isRight()) {
2232             loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2233                 "ERROR while create relations");
2234             throw new ByResponseFormatComponentException(
2235                 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource));
2236         }
2237         return eitherGetResource.left().value();
2238     }
2239
2240     private void processUiComponentInstance(Resource oldResource, ComponentInstance instance,
2241                                             Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
2242                                             Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
2243                                             Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
2244                                             Map<String, Map<String, ArtifactDefinition>> instArtifacts,
2245                                             Map<String, List<ComponentInstanceProperty>> instProperties,
2246                                             Map<String, List<ComponentInstanceInput>> instInputs,
2247                                             Map<String, List<AttributeDefinition>> instAttributes) {
2248         Optional<ComponentInstance> foundInstance = findInstance(oldResource, instance);
2249         if (foundInstance.isPresent()) {
2250             if (MapUtils.isNotEmpty(foundInstance.get().getCapabilities())) {
2251                 instCapabilities.put(instance, foundInstance.get().getCapabilities());
2252             }
2253             if (MapUtils.isNotEmpty(foundInstance.get().getRequirements())) {
2254                 instRequirements.put(instance, foundInstance.get().getRequirements());
2255             }
2256             if (MapUtils.isNotEmpty(foundInstance.get().getDeploymentArtifacts())) {
2257                 instDeploymentArtifacts.put(instance.getUniqueId(), foundInstance.get().getDeploymentArtifacts());
2258             }
2259             if (MapUtils.isNotEmpty(foundInstance.get().getArtifacts())) {
2260                 instArtifacts.put(instance.getUniqueId(), foundInstance.get().getArtifacts());
2261             }
2262             if (MapUtils.isNotEmpty(oldResource.getComponentInstancesProperties()) && CollectionUtils
2263                 .isNotEmpty(oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()))) {
2264                 instProperties.put(instance.getUniqueId(), oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()));
2265             }
2266             if (MapUtils.isNotEmpty(oldResource.getComponentInstancesInputs()) && CollectionUtils
2267                 .isNotEmpty(oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()))) {
2268                 instInputs.put(instance.getUniqueId(), oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()));
2269             }
2270             if (MapUtils.isNotEmpty(oldResource.getComponentInstancesAttributes()) && CollectionUtils
2271                 .isNotEmpty(oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()))) {
2272                 instAttributes.put(instance.getUniqueId(),
2273                     oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()).stream().map(AttributeDefinition::new)
2274                         .collect(toList()));
2275             }
2276         }
2277     }
2278
2279     private Optional<ComponentInstance> findInstance(Resource oldResource, ComponentInstance instance) {
2280         if (oldResource != null && CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
2281             return oldResource.getComponentInstances().stream().filter(i -> i.getName().equals(instance.getName())).findFirst();
2282         }
2283         return Optional.empty();
2284     }
2285
2286     private void associateResourceInstances(String yamlName, Resource resource, List<RequirementCapabilityRelDef> relations) {
2287         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> relationsEither = toscaOperationFacade
2288             .associateResourceInstances(resource, resource.getUniqueId(), relations);
2289         if (relationsEither.isRight() && relationsEither.right().value() != StorageOperationStatus.NOT_FOUND) {
2290             StorageOperationStatus status = relationsEither.right().value();
2291             log.debug("failed to associate instances of resource {} status is {}", resource.getUniqueId(), status);
2292             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), yamlName);
2293         } else {
2294             setResourceInstanceRelationsOnComponent(resource, relationsEither.left().value());
2295         }
2296     }
2297
2298     private void associateInstAttributeToComponentToInstances(String yamlName, Resource resource,
2299                                                               Map<String, List<AttributeDefinition>> instAttributes) {
2300         StorageOperationStatus addArtToInst;
2301         addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, resource);
2302         if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2303             log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2304             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2305         }
2306     }
2307
2308     private void associateOrAddCalculatedCapReq(String yamlName, Resource resource,
2309                                                 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
2310                                                 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements) {
2311         StorageOperationStatus addArtToInst;
2312         addArtToInst = toscaOperationFacade.associateOrAddCalculatedCapReq(instCapabilities, instRequirements, resource);
2313         if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2314             log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2315             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2316         }
2317     }
2318
2319     private void associateArtifactsToInstances(String yamlName, Resource resource, Map<String, Map<String, ArtifactDefinition>> instArtifacts) {
2320         StorageOperationStatus addArtToInst;
2321         addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource);
2322         if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2323             log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2324             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2325         }
2326     }
2327
2328     private void associateDeploymentArtifactsToInstances(User user, String yamlName, Resource resource,
2329                                                          Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts) {
2330         StorageOperationStatus addArtToInst = toscaOperationFacade.associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource, user);
2331         if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2332             log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2333             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2334         }
2335     }
2336
2337     private void associateComponentInstanceInputsToComponent(String yamlName, Resource resource,
2338                                                              Map<String, List<ComponentInstanceInput>> instInputs) {
2339         if (MapUtils.isNotEmpty(instInputs)) {
2340             Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addInputToInst = toscaOperationFacade
2341                 .associateComponentInstanceInputsToComponent(instInputs, resource.getUniqueId());
2342             if (addInputToInst.isRight()) {
2343                 StorageOperationStatus addInputToInstError = addInputToInst.right().value();
2344                 log.debug("failed to associate inputs value of resource {} status is {}", resource.getUniqueId(), addInputToInstError);
2345                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addInputToInstError), yamlName);
2346             }
2347             setComponentInstanceInputsOnComponent(resource, instInputs);
2348         }
2349     }
2350
2351     private void setComponentInstanceInputsOnComponent(Resource resource, Map<String, List<ComponentInstanceInput>> instInputs) {
2352         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = resource.getComponentInstancesInputs();
2353         if (componentInstancesInputs == null) {
2354             componentInstancesInputs = new HashMap<>();
2355         }
2356         componentInstancesInputs.putAll(instInputs);
2357         resource.setComponentInstancesInputs(componentInstancesInputs);
2358     }
2359
2360     private void associateComponentInstancePropertiesToComponent(String yamlName, Resource resource,
2361                                                                  Map<String, List<ComponentInstanceProperty>> instProperties) {
2362         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addPropToInst = toscaOperationFacade
2363             .associateComponentInstancePropertiesToComponent(instProperties, resource.getUniqueId());
2364         if (addPropToInst.isRight()) {
2365             loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2366                 "ERROR while  associate compnent insatnce properties of resource: {} status is: {}", resource.getName(),
2367                 addPropToInst.right().value());
2368             StorageOperationStatus storageOperationStatus = addPropToInst.right().value();
2369             log.debug("failed to associate properties of resource {} status is {}", resource.getUniqueId(), storageOperationStatus);
2370             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageOperationStatus), yamlName);
2371         }
2372         setComponentInstancePropertiesOnComponent(resource, instProperties);
2373     }
2374
2375     private void setComponentInstancePropertiesOnComponent(Resource resource, Map<String, List<ComponentInstanceProperty>> instProperties) {
2376         Map<String, List<ComponentInstanceProperty>> componentInstanceProps = resource.getComponentInstancesProperties();
2377         if (componentInstanceProps == null) {
2378             componentInstanceProps = new HashMap<>();
2379         }
2380         componentInstanceProps.putAll(instProperties);
2381         resource.setComponentInstancesProperties(componentInstanceProps);
2382     }
2383
2384     private void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2385         if (resource.getResourceType() == ResourceTypeEnum.CVFC) {
2386             Either<Resource, StorageOperationStatus> getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap);
2387             if (getResourceRes.isRight()) {
2388                 ResponseFormat responseFormat = componentsUtils
2389                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
2390                 throw new ByResponseFormatComponentException(responseFormat);
2391             }
2392         }
2393     }
2394
2395     private void addRelationsToRI(String yamlName, Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2396                                   List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2397         for (Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2398             UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2399             ComponentInstance currentCompInstance = null;
2400             for (ComponentInstance compInstance : componentInstancesList) {
2401                 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2402                     currentCompInstance = compInstance;
2403                     break;
2404                 }
2405             }
2406             if (currentCompInstance == null) {
2407                 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
2408                 BeEcompErrorManager.getInstance()
2409                     .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
2410                         ErrorSeverity.ERROR);
2411                 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2412             }
2413             ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations);
2414             if (addRelationToRiRes.getStatus() != 200) {
2415                 throw new ByResponseFormatComponentException(addRelationToRiRes);
2416             }
2417         }
2418     }
2419
2420     private void setResourceInstanceRelationsOnComponent(Resource resource, List<RequirementCapabilityRelDef> relations) {
2421         if (resource.getComponentInstancesRelations() != null) {
2422             resource.getComponentInstancesRelations().addAll(relations);
2423         } else {
2424             resource.setComponentInstancesRelations(relations);
2425         }
2426     }
2427
2428     private void processComponentInstance(String yamlName, Resource resource, List<ComponentInstance> componentInstancesList,
2429                                           Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
2430                                           Map<String, List<ComponentInstanceProperty>> instProperties,
2431                                           Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2432                                           Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
2433                                           Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
2434                                           Map<String, Map<String, ArtifactDefinition>> instArtifacts,
2435                                           Map<String, List<AttributeDefinition>> instAttributes, Map<String, Resource> originCompMap,
2436                                           Map<String, List<ComponentInstanceInput>> instInputs,
2437                                           UploadComponentInstanceInfo uploadComponentInstanceInfo) {
2438         Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
2439             .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())).findFirst();
2440         if (currentCompInstanceOpt.isEmpty()) {
2441             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
2442             BeEcompErrorManager.getInstance()
2443                 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
2444                     ErrorSeverity.ERROR);
2445             throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2446         }
2447         ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
2448         String resourceInstanceId = currentCompInstance.getUniqueId();
2449         Resource originResource = getOriginResource(originCompMap, currentCompInstance);
2450         if (isNotEmpty(originResource.getRequirements())) {
2451             instRequirements.put(currentCompInstance, originResource.getRequirements());
2452         }
2453         if (isNotEmpty(originResource.getCapabilities())) {
2454             processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, currentCompInstance, originResource);
2455         }
2456         if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
2457             instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
2458         }
2459         if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
2460             instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
2461         }
2462         if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
2463             instAttributes.put(resourceInstanceId, originResource.getAttributes());
2464         }
2465         if (originResource.getResourceType() != ResourceTypeEnum.CVFC) {
2466             ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource,
2467                 currentCompInstance, instProperties, allDataTypes.left().value());
2468             if (addPropertiesValueToRiRes.getStatus() != 200) {
2469                 throw new ByResponseFormatComponentException(addPropertiesValueToRiRes);
2470             }
2471         } else {
2472             addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, instInputs, allDataTypes.left().value());
2473         }
2474     }
2475
2476     private Resource getOriginResource(Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2477         Resource originResource;
2478         if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2479             Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2480                 .getToscaFullElement(currentCompInstance.getComponentUid());
2481             if (getOriginResourceRes.isRight()) {
2482                 log.debug("failed to fetch resource with uniqueId {} and tosca component name {} status is {}", currentCompInstance.getComponentUid(),
2483                     currentCompInstance.getToscaComponentName(), getOriginResourceRes);
2484                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()),
2485                     currentCompInstance.getComponentUid());
2486             }
2487             originResource = getOriginResourceRes.left().value();
2488             originCompMap.put(originResource.getUniqueId(), originResource);
2489         } else {
2490             originResource = originCompMap.get(currentCompInstance.getComponentUid());
2491         }
2492         return originResource;
2493     }
2494
2495     private void processComponentInstanceCapabilities(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
2496                                                       Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2497                                                       UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance,
2498                                                       Resource originResource) {
2499         Map<String, List<CapabilityDefinition>> originCapabilities;
2500         if (isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2501             originCapabilities = new HashMap<>();
2502             Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2503             originResource.getCapabilities().forEach((k, v) -> addCapabilities(originCapabilities, k, v));
2504             uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> addCapabilitiesProperties(newPropertiesMap, l));
2505             updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
2506         } else {
2507             originCapabilities = originResource.getCapabilities();
2508         }
2509         instCapabilties.put(currentCompInstance, originCapabilities);
2510     }
2511
2512     private void updateCapabilityPropertiesValues(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
2513                                                   Map<String, List<CapabilityDefinition>> originCapabilities,
2514                                                   Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
2515         originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
2516             .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes.left().value()));
2517     }
2518
2519     private void addCapabilitiesProperties(Map<String, Map<String, UploadPropInfo>> newPropertiesMap, List<UploadCapInfo> capabilities) {
2520         for (UploadCapInfo capability : capabilities) {
2521             if (isNotEmpty(capability.getProperties())) {
2522                 newPropertiesMap.put(capability.getName(), capability.getProperties().stream().collect(toMap(UploadInfo::getName, p -> p)));
2523             }
2524         }
2525     }
2526
2527     private void addCapabilities(Map<String, List<CapabilityDefinition>> originCapabilities, String type, List<CapabilityDefinition> capabilities) {
2528         List<CapabilityDefinition> list = capabilities.stream().map(CapabilityDefinition::new).collect(toList());
2529         originCapabilities.put(type, list);
2530     }
2531
2532     private void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2533                                       Map<String, DataTypeDefinition> allDataTypes) {
2534         properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2535     }
2536
2537     private String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2538                                        Map<String, DataTypeDefinition> allDataTypes) {
2539         String value = null;
2540         List<GetInputValueDataDefinition> getInputs = null;
2541         boolean isValidate = true;
2542         if (null != propertyInfo && propertyInfo.getValue() != null) {
2543             getInputs = propertyInfo.getGet_input();
2544             isValidate = getInputs == null || getInputs.isEmpty();
2545             if (isValidate) {
2546                 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2547             } else {
2548                 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2549             }
2550         }
2551         property.setValue(value);
2552         return validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2553     }
2554
2555     private Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2556                                                                                                     Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2557         Either<Resource, StorageOperationStatus> updateRes = null;
2558         Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2559         Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2560         StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2561         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2562             log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2563                 resource.getUniqueId(), status);
2564             updateRes = Either.right(status);
2565         }
2566         if (updateRes == null) {
2567             fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
2568                 updatedInstRequirements);
2569             status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
2570             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2571                 log.debug(
2572                     "Failed to associate capabilities and requirementss of resource {}, updated according to a substitution mapping. Status is {}",
2573                     resource.getUniqueId(), status);
2574                 updateRes = Either.right(status);
2575             }
2576         }
2577         if (updateRes == null) {
2578             updateRes = Either.left(resource);
2579         }
2580         return updateRes;
2581     }
2582
2583     private void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2584                                                          Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2585                                                          Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2586                                                          Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2587         componentInstances.forEach(i -> {
2588             fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2589             fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2590         });
2591     }
2592
2593     private void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2594                                              ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2595         Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2596         Set<String> updatedReqNames = new HashSet<>();
2597         if (isNotEmpty(requirementsNamesToUpdate)) {
2598             for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2599                 updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
2600                     r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2601                     .map(r -> {
2602                         r.setParentName(r.getName());
2603                         r.setName(requirementsNamesToUpdate.get(r.getName()));
2604                         updatedReqNames.add(r.getName());
2605                         return r;
2606                     }).collect(toList()));
2607             }
2608         }
2609         if (isNotEmpty(updatedRequirements)) {
2610             updatedInstRequirements.put(instance, updatedRequirements);
2611         }
2612     }
2613
2614     private void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2615                                              ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2616         Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2617         Set<String> updatedCapNames = new HashSet<>();
2618         if (isNotEmpty(capabilitiesNamesToUpdate)) {
2619             for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2620                 updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
2621                     c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2622                     .map(c -> {
2623                         c.setParentName(c.getName());
2624                         c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2625                         updatedCapNames.add(c.getName());
2626                         return c;
2627                     }).collect(toList()));
2628             }
2629         }
2630         if (isNotEmpty(updatedCapabilities)) {
2631             updatedInstCapabilties.put(instance, updatedCapabilities);
2632         }
2633     }
2634
2635     private ResponseFormat addRelationToRI(String yamlName, Resource resource, UploadComponentInstanceInfo nodesInfoValue,
2636                                            List<RequirementCapabilityRelDef> relations) {
2637         List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
2638         ComponentInstance currentCompInstance = null;
2639         for (ComponentInstance compInstance : componentInstancesList) {
2640             if (compInstance.getName().equals(nodesInfoValue.getName())) {
2641                 currentCompInstance = compInstance;
2642                 break;
2643             }
2644         }
2645         if (currentCompInstance == null) {
2646             log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), resource.getUniqueId());
2647             BeEcompErrorManager.getInstance()
2648                 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, resource.getUniqueId(),
2649                     ErrorSeverity.ERROR);
2650             return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2651         }
2652         String resourceInstanceId = currentCompInstance.getUniqueId();
2653         Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2654         if (regMap != null) {
2655             for (Entry<String, List<UploadReqInfo>> nodesRegInfoEntry : regMap.entrySet()) {
2656                 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2657                 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2658                     log.debug("Going to create  relation {}", uploadRegInfo.getName());
2659                     loggerSupportability
2660                             .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
2661                                     "Started to create relations on instance: {}", uploadRegInfo.getName());
2662                     String regName = uploadRegInfo.getName();
2663                     RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2664                     regCapRelDef.setFromNode(resourceInstanceId);
2665                     log.debug("try to find available requirement {} ", regName);
2666                     Either<RequirementDefinition, ResponseFormat> eitherReqStatus = findAviableRequiremen(regName, yamlName, nodesInfoValue,
2667                             currentCompInstance, uploadRegInfo.getCapabilityName());
2668                     if (eitherReqStatus.isRight()) {
2669                         log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
2670                         loggerSupportability
2671                                 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2672                                         "ERROR while search available requirement {} status is: {}", regName, eitherReqStatus.right().value());
2673                         return eitherReqStatus.right().value();
2674                     }
2675                     RequirementDefinition validReq = eitherReqStatus.left().value();
2676                     List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
2677                     if (reqAndRelationshipPairList == null) {
2678                         reqAndRelationshipPairList = new ArrayList<>();
2679                     }
2680                     RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2681                     reqAndRelationshipPair.setRequirement(regName);
2682                     reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2683                     reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2684                     RelationshipImpl relationship = new RelationshipImpl();
2685                     relationship.setType(validReq.getCapability());
2686                     reqAndRelationshipPair.setRelationships(relationship);
2687                     ComponentInstance currentCapCompInstance = null;
2688                     for (ComponentInstance compInstance : componentInstancesList) {
2689                         if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2690                             currentCapCompInstance = compInstance;
2691                             break;
2692                         }
2693                     }
2694                     if (currentCapCompInstance == null) {
2695                         log.debug("The component instance  with name {} not found on resource {} ", uploadRegInfo.getNode(), resource.getUniqueId());
2696                         loggerSupportability
2697                                 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2698                                         "ERROR component instance  with name: {} not found on resource: {}", uploadRegInfo.getNode(), resource.getUniqueId());
2699                         BeEcompErrorManager.getInstance()
2700                                 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, resource.getUniqueId(),
2701                                         ErrorSeverity.ERROR);
2702                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2703                     }
2704                     regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2705                     log.debug("try to find aviable Capability  req name is {} ", validReq.getName());
2706                     CapabilityDefinition aviableCapForRel = findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
2707                     if (aviableCapForRel == null) {
2708                         log.debug("aviable capability was not found. req name is {} component instance is {}", validReq.getName(),
2709                                 currentCapCompInstance.getUniqueId());
2710                         loggerSupportability
2711                                 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2712                                         "ERROR available capability was not found. req name is: {} component instance is: {}", validReq.getName(),
2713                                         currentCapCompInstance.getUniqueId());
2714                         BeEcompErrorManager.getInstance().logInternalDataError(
2715                                 "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
2716                                         .getUniqueId(), resource.getUniqueId(), ErrorSeverity.ERROR);
2717                         return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2718                     }
2719                     reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2720                     reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2721                     reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2722                     CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2723                     capReqRel.setRelation(reqAndRelationshipPair);
2724                     reqAndRelationshipPairList.add(capReqRel);
2725                     regCapRelDef.setRelationships(reqAndRelationshipPairList);
2726                     relations.add(regCapRelDef);
2727                 }
2728             }
2729         } else if (resource.getResourceType() != ResourceTypeEnum.CVFC) {
2730             return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2731         }
2732         return componentsUtils.getResponseFormat(ActionStatus.OK);
2733     }
2734
2735     private void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
2736                                      ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
2737                                      Map<String, DataTypeDefinition> allDataTypes) {
2738         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2739         if (MapUtils.isNotEmpty(propMap)) {
2740             Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
2741             List<ComponentInstanceInput> instPropList = new ArrayList<>();
2742             if (CollectionUtils.isEmpty(originResource.getInputs())) {
2743                 log.debug("failed to find properties ");
2744                 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2745                     "ERROR while try to find properties");
2746                 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
2747             }
2748             originResource.getInputs().forEach(p -> addInput(currPropertiesMap, p));
2749             for (List<UploadPropInfo> propertyList : propMap.values()) {
2750                 processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
2751             }
2752             currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
2753             instInputs.put(currentCompInstance.getUniqueId(), instPropList);
2754         }
2755     }
2756
2757     private void processProperty(Resource resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes,
2758                                  Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
2759                                  List<UploadPropInfo> propertyList) {
2760         UploadPropInfo propertyInfo = propertyList.get(0);
2761         String propName = propertyInfo.getName();
2762         if (!currPropertiesMap.containsKey(propName)) {
2763             loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2764                 "ERROR failed to find property: {}", propName);
2765             log.debug("failed to find property {} ", propName);
2766             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, propName);
2767         }
2768         InputDefinition curPropertyDef = currPropertiesMap.get(propName);
2769         ComponentInstanceInput property = null;
2770         String value = null;
2771         List<GetInputValueDataDefinition> getInputs = null;
2772         boolean isValidate = true;
2773         if (propertyInfo.getValue() != null) {
2774             getInputs = propertyInfo.getGet_input();
2775             isValidate = getInputs == null || getInputs.isEmpty();
2776             if (isValidate) {
2777                 value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
2778             } else {
2779                 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2780             }
2781         }
2782         property = new ComponentInstanceInput(curPropertyDef, value, null);
2783         String validPropertyVAlue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2784         property.setValue(validPropertyVAlue);
2785         if (isNotEmpty(getInputs)) {
2786             List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
2787             for (GetInputValueDataDefinition getInput : getInputs) {
2788                 List<InputDefinition> inputs = resource.getInputs();
2789                 if (CollectionUtils.isEmpty(inputs)) {
2790                     loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2791                         "ERROR Failed to add property: " + propName + " to resource instance: {}. Inputs list is empty ",
2792                         currentCompInstance.getUniqueId());
2793                     log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property,
2794                         currentCompInstance.getUniqueId());
2795                     throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2796                 }
2797                 Optional<InputDefinition> optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny();
2798                 if (optional.isEmpty()) {
2799                     loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2800                         "ERROR Failed to find input: " + getInput.getInputName());
2801                     log.debug("Failed to find input {} ", getInput.getInputName());
2802                     // @@TODO error message
2803                     throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2804                 }
2805                 InputDefinition input = optional.get();
2806                 getInput.setInputId(input.getUniqueId());
2807                 getInputValues.add(getInput);
2808                 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
2809                 processGetInput(getInputValues, inputs, getInputIndex);
2810             }
2811             property.setGetInputValues(getInputValues);
2812         }
2813         instPropList.add(property);
2814         // delete overriden property
2815         currPropertiesMap.remove(property.getName());
2816     }
2817
2818     private void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs,
2819                                  GetInputValueDataDefinition getInputIndex) {
2820         Optional<InputDefinition> optional;
2821         if (getInputIndex != null) {
2822             optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny();
2823             if (optional.isEmpty()) {
2824                 log.debug("Failed to find input {} ", getInputIndex.getInputName());
2825                 // @@TODO error message
2826                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2827             }
2828             InputDefinition inputIndex = optional.get();
2829             getInputIndex.setInputId(inputIndex.getUniqueId());
2830             getInputValues.add(getInputIndex);
2831         }
2832     }
2833
2834     private void addInput(Map<String, InputDefinition> currPropertiesMap, InputDefinition prop) {
2835         String propName = prop.getName();
2836         if (!currPropertiesMap.containsKey(propName)) {
2837             currPropertiesMap.put(propName, prop);
2838         }
2839     }
2840
2841     private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
2842                                                  ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceProperty>> instProperties,
2843                                                  Map<String, DataTypeDefinition> allDataTypes) {
2844         Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2845         Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
2846         List<PropertyDefinition> listFromMap = originResource.getProperties();
2847         if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) {
2848             loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2849                 "ERROR Failed to find properties");
2850             log.debug("failed to find properties");
2851             return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
2852         }
2853         if (listFromMap == null || listFromMap.isEmpty()) {
2854             return componentsUtils.getResponseFormat(ActionStatus.OK);
2855         }
2856         for (PropertyDefinition prop : listFromMap) {
2857             String propName = prop.getName();
2858             if (!currPropertiesMap.containsKey(propName)) {
2859                 currPropertiesMap.put(propName, prop);
2860             }
2861         }
2862         List<ComponentInstanceProperty> instPropList = new ArrayList<>();
2863         if (propMap != null && propMap.size() > 0) {
2864             for (List<UploadPropInfo> propertyList : propMap.values()) {
2865                 UploadPropInfo propertyInfo = propertyList.get(0);
2866                 String propName = propertyInfo.getName();
2867                 if (!currPropertiesMap.containsKey(propName)) {
2868                     log.debug("failed to find property {} ", propName);
2869                     loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2870                         "ERROR Failed to find property: {}", propName);
2871                     return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName);
2872                 }
2873                 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
2874                 ComponentInstanceProperty property = null;
2875                 String value = null;
2876                 List<GetInputValueDataDefinition> getInputs = null;
2877                 boolean isValidate = true;
2878                 if (propertyInfo.getValue() != null) {
2879                     getInputs = propertyInfo.getGet_input();
2880                     isValidate = getInputs == null || getInputs.isEmpty();
2881                     if (isValidate) {
2882                         value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
2883                     } else {
2884                         value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2885                     }
2886                 }
2887                 property = new ComponentInstanceProperty(curPropertyDef, value, null);
2888                 String validatePropValue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2889                 property.setValue(validatePropValue);
2890                 if (getInputs != null && !getInputs.isEmpty()) {
2891                     List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
2892                     for (GetInputValueDataDefinition getInput : getInputs) {
2893                         List<InputDefinition> inputs = resource.getInputs();
2894                         if (inputs == null || inputs.isEmpty()) {
2895                             log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
2896                             loggerSupportability
2897                                 .log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2898                                     "Failed to add property: {} to instance. Inputs list is empty", propName);
2899                             rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
2900                                 property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
2901                         }
2902                         Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
2903                         if (inputEither.isRight()) {
2904                             throw inputEither.right().value();
2905                         } else {
2906                             InputDefinition input = inputEither.left().value();
2907                             getInput.setInputId(input.getUniqueId());
2908                             getInputValues.add(getInput);
2909                             GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
2910                             if (getInputIndex != null) {
2911                                 Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
2912                                 if (inputEither.isRight()) {
2913                                     throw newInputEither.right().value();
2914                                 } else {
2915                                     InputDefinition newInput = newInputEither.left().value();
2916                                     getInputIndex.setInputId(newInput.getUniqueId());
2917                                 }
2918                                 getInputValues.add(getInputIndex);
2919                             }
2920                         }
2921                     }
2922                     property.setGetInputValues(getInputValues);
2923                 }
2924                 instPropList.add(property);
2925                 // delete overriden property
2926                 currPropertiesMap.remove(property.getName());
2927             }
2928         }
2929         // add rest of properties
2930         if (!currPropertiesMap.isEmpty()) {
2931             for (PropertyDefinition value : currPropertiesMap.values()) {
2932                 instPropList.add(new ComponentInstanceProperty(value));
2933             }
2934         }
2935         instProperties.put(currentCompInstance.getUniqueId(), instPropList);
2936         return componentsUtils.getResponseFormat(ActionStatus.OK);
2937     }
2938
2939     // US740820 Relate RIs according to capability name
2940     private CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
2941                                                                      UploadReqInfo uploadReqInfo) {
2942         if (null == uploadReqInfo.getCapabilityName() || validReq.getCapability()
2943             .equals(uploadReqInfo.getCapabilityName())) {// get
2944
2945             // by
2946
2947             // capability
2948
2949             // type
2950             return findAvailableCapability(validReq, currentCapCompInstance);
2951         }
2952         return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo);
2953     }
2954
2955     private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
2956                                                          UploadReqInfo uploadReqInfo) {
2957         CapabilityDefinition cap = null;
2958         Map<String, List<CapabilityDefinition>> capMap = currentCapCompInstance.getCapabilities();
2959         if (!capMap.containsKey(validReq.getCapability())) {
2960             return null;
2961         }
2962         Optional<CapabilityDefinition> capByName = capMap.get(validReq.getCapability()).stream()
2963             .filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny();
2964         if (capByName.isEmpty()) {
2965             return null;
2966         }
2967         cap = capByName.get();
2968         if (isBoundedByOccurrences(cap)) {
2969             String leftOccurrences = cap.getLeftOccurrences();
2970             int left = Integer.parseInt(leftOccurrences);
2971             if (left > 0) {
2972                 --left;
2973                 cap.setLeftOccurrences(String.valueOf(left));
2974             }
2975         }
2976         return cap;
2977     }
2978
2979     private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance instance) {
2980         Map<String, List<CapabilityDefinition>> capMap = instance.getCapabilities();
2981         if (capMap.containsKey(validReq.getCapability())) {
2982             List<CapabilityDefinition> capList = capMap.get(validReq.getCapability());
2983             for (CapabilityDefinition cap : capList) {
2984                 if (isBoundedByOccurrences(cap)) {
2985                     String leftOccurrences = cap.getLeftOccurrences() != null ? cap.getLeftOccurrences() : cap.getMaxOccurrences();
2986                     int left = Integer.parseInt(leftOccurrences);
2987                     if (left > 0) {
2988                         --left;
2989                         cap.setLeftOccurrences(String.valueOf(left));
2990                         return cap;
2991                     }
2992                 } else {
2993                     return cap;
2994                 }
2995             }
2996         }
2997         return null;
2998     }
2999
3000     private boolean isBoundedByOccurrences(CapabilityDefinition cap) {
3001         return cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES);
3002     }
3003
3004     private Either<RequirementDefinition, ResponseFormat> findAviableRequiremen(String regName, String yamlName,
3005                                                                                 UploadComponentInstanceInfo uploadComponentInstanceInfo,
3006                                                                                 ComponentInstance currentCompInstance, String capName) {
3007         Map<String, List<RequirementDefinition>> comInstRegDefMap = currentCompInstance.getRequirements();
3008         List<RequirementDefinition> list = comInstRegDefMap.get(capName);
3009         RequirementDefinition validRegDef = null;
3010         if (list == null) {
3011             for (Entry<String, List<RequirementDefinition>> entry : comInstRegDefMap.entrySet()) {
3012                 for (RequirementDefinition reqDef : entry.getValue()) {
3013                     if (reqDef.getName().equals(regName)) {
3014                         if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
3015                             String leftOccurrences = reqDef.getLeftOccurrences();
3016                             if (leftOccurrences == null) {
3017                                 leftOccurrences = reqDef.getMaxOccurrences();
3018                             }
3019                             int left = Integer.parseInt(leftOccurrences);
3020                             if (left > 0) {
3021                                 --left;
3022                                 reqDef.setLeftOccurrences(String.valueOf(left));
3023                                 validRegDef = reqDef;
3024                                 break;
3025                             } else {
3026                                 continue;
3027                             }
3028                         } else {
3029                             validRegDef = reqDef;
3030                             break;
3031                         }
3032                     }
3033                 }
3034                 if (validRegDef != null) {
3035                     break;
3036                 }
3037             }
3038         } else {
3039             for (RequirementDefinition reqDef : list) {
3040                 if (reqDef.getName().equals(regName)) {
3041                     if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
3042                         String leftOccurrences = reqDef.getLeftOccurrences();
3043                         if (leftOccurrences == null) {
3044                             leftOccurrences = reqDef.getMaxOccurrences();
3045                         }
3046                         int left = Integer.parseInt(leftOccurrences);
3047                         if (left > 0) {
3048                             --left;
3049                             reqDef.setLeftOccurrences(String.valueOf(left));
3050                             validRegDef = reqDef;
3051                             break;
3052                         } else {
3053                             continue;
3054                         }
3055                     } else {
3056                         validRegDef = reqDef;
3057                         break;
3058                     }
3059                 }
3060             }
3061         }
3062         if (validRegDef == null) {
3063             ResponseFormat responseFormat = componentsUtils
3064                 .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3065                     uploadComponentInstanceInfo.getType());
3066             return Either.right(responseFormat);
3067         }
3068         return Either.left(validRegDef);
3069     }
3070
3071     private Resource createResourceInstances(String yamlName, Resource resource, Resource oldResource,
3072                                              Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, Map<String, Resource> nodeNamespaceMap,
3073                                              Map<String, Resource> existingNodeTypesByResourceNames) {
3074         Either<Resource, ResponseFormat> eitherResource;
3075         log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
3076         if (isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
3077             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
3078             throw new ByResponseFormatComponentException(responseFormat);
3079         }
3080         if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
3081             nodeNamespaceMap.forEach((k, v) -> existingNodeTypesByResourceNames.put(v.getToscaResourceName(), v));
3082         }
3083         Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
3084         uploadResInstancesMap.values().forEach(
3085             i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypesByResourceNames, resourcesInstancesMap));
3086         if (oldResource != null && oldResource.getResourceType() != ResourceTypeEnum.CVFC && oldResource.getComponentInstances() != null) {
3087             Map<String, Resource> existingNodeTypesByUids = existingNodeTypesByResourceNames.values().stream()
3088                 .collect(toMap(Resource::getUniqueId, r -> r));
3089             oldResource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar())
3090                 .forEach(uiInst -> resourcesInstancesMap.put(uiInst, getOriginResource(existingNodeTypesByUids, uiInst)));
3091         }
3092         if (isNotEmpty(resourcesInstancesMap)) {
3093             try {
3094                 toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false, oldResource != null);
3095             } catch (StorageException exp) {
3096                 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
3097                     log.debug("Failed to add component instances to container component {}", resource.getName());
3098                     ResponseFormat responseFormat = componentsUtils
3099                         .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
3100                     eitherResource = Either.right(responseFormat);
3101                     throw new ByResponseFormatComponentException(eitherResource.right().value());
3102                 }
3103             }
3104         }
3105         if (CollectionUtils.isEmpty(resource.getComponentInstances()) &&
3106             resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
3107             log.debug("Error when create resource instance from csar. ComponentInstances list empty");
3108             BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
3109             throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
3110         }
3111         return resource;
3112     }
3113
3114     private void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Resource resource,
3115                                               Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
3116                                               Map<ComponentInstance, Resource> resourcesInstancesMap) {
3117         Either<Resource, ResponseFormat> eitherResource;
3118         log.debug("*************Going to create  resource instances {}", yamlName);
3119         // updating type if the type is node type name - we need to take the
3120
3121         // updated name
3122         log.debug("*************Going to create  resource instances {}", uploadComponentInstanceInfo.getName());
3123         if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
3124             uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
3125         }
3126         Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap, resource);
3127         ComponentInstance componentInstance = new ComponentInstance();
3128         componentInstance.setComponentUid(refResource.getUniqueId());
3129         Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
3130         if (directives != null && !directives.isEmpty()) {
3131             componentInstance.setDirectives(new ArrayList<>(directives));
3132         }
3133         UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
3134         if (uploadNodeFilterInfo != null) {
3135             componentInstance
3136                 .setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
3137         }
3138         ComponentTypeEnum containerComponentType = resource.getComponentType();
3139         NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
3140         if (containerNodeType == NodeTypeEnum.Resource && isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && isNotEmpty(
3141             refResource.getCapabilities())) {
3142             setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
3143             Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = getValidComponentInstanceCapabilities(
3144                 refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
3145             componentInstance.setCapabilities(validComponentInstanceCapabilities);
3146         }
3147         if (isNotEmpty(uploadComponentInstanceInfo.getArtifacts())) {
3148             Map<String, Map<String, UploadArtifactInfo>> artifacts = uploadComponentInstanceInfo.getArtifacts();
3149             Map<String, ToscaArtifactDataDefinition> toscaArtifacts = new HashMap<>();
3150             Map<String, Map<String, UploadArtifactInfo>> arts = artifacts.entrySet().stream()
3151                 .filter(e -> e.getKey().contains(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName()))
3152                 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
3153             Map<String, UploadArtifactInfo> artifact = arts.get(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName());
3154             for (Map.Entry<String, UploadArtifactInfo> entry : artifact.entrySet()) {
3155                 ToscaArtifactDataDefinition to = new ToscaArtifactDataDefinition();
3156                 to.setFile(entry.getValue().getFile());
3157                 to.setType(entry.getValue().getType());
3158                 toscaArtifacts.put(entry.getKey(), to);
3159             }
3160             componentInstance.setToscaArtifacts(toscaArtifacts);
3161         }
3162         if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
3163             log.debug("createResourceInstances - not found lates version for resource instance with name {} and type {}",
3164                 uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3165             throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3166                 uploadComponentInstanceInfo.getType());
3167         }
3168         Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
3169         componentInstance.setName(uploadComponentInstanceInfo.getName());
3170         componentInstance.setIcon(origResource.getIcon());
3171         componentInstance.setCreatedFrom(CreatedFrom.CSAR);
3172         resourcesInstancesMap.put(componentInstance, origResource);
3173     }
3174
3175     private void setCapabilityNamesTypes(Map<String, List<CapabilityDefinition>> originCapabilities,
3176                                          Map<String, List<UploadCapInfo>> uploadedCapabilities) {
3177         for (Entry<String, List<UploadCapInfo>> currEntry : uploadedCapabilities.entrySet()) {
3178             if (originCapabilities.containsKey(currEntry.getKey())) {
3179                 currEntry.getValue().forEach(cap -> cap.setType(currEntry.getKey()));
3180             }
3181         }
3182         for (Map.Entry<String, List<CapabilityDefinition>> capabilities : originCapabilities.entrySet()) {
3183             capabilities.getValue().forEach(cap -> {
3184                 if (uploadedCapabilities.containsKey(cap.getName())) {
3185                     uploadedCapabilities.get(cap.getName()).forEach(c -> {
3186                         c.setName(cap.getName());
3187                         c.setType(cap.getType());
3188                     });
3189                 }
3190             });
3191         }
3192     }
3193
3194     private Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo,
3195                                                           Map<String, Resource> nodeNamespaceMap, Resource resource) {
3196         log.debug("validateResourceInstanceBeforeCreate - going to validate resource instance with name {} and type {} before create",
3197             uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3198         Resource refResource;
3199         if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
3200             refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
3201         } else {
3202             Either<Resource, StorageOperationStatus> findResourceEither = toscaOperationFacade
3203                 .getByToscaResourceNameMatchingVendorRelease(uploadComponentInstanceInfo.getType(),
3204                     ((ResourceMetadataDataDefinition) resource.getComponentMetadataDefinition().getMetadataDataDefinition()).getVendorRelease());
3205             if (findResourceEither.isRight()) {
3206                 log.debug("validateResourceInstanceBeforeCreate - not found latest version for resource instance with name {} and type {}",
3207                     uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3208                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(findResourceEither.right().value()));
3209             }
3210             refResource = findResourceEither.left().value();
3211             nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
3212         }
3213         String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
3214         if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
3215             log.debug(
3216                 "validateResourceInstanceBeforeCreate - component instance of component {} can not be created because the component is in an illegal state {}.",
3217                 refResource.getName(), componentState);
3218             throw new ByActionStatusComponentException(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(),
3219                 refResource.getName(), componentState);
3220         }
3221         if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) {
3222             log.debug("validateResourceInstanceBeforeCreate -  ref resource type is {} ", refResource.getResourceType());
3223             throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3224                 uploadComponentInstanceInfo.getType());
3225         }
3226         return refResource;
3227     }
3228
3229     public Resource propagateStateToCertified(User user, Resource resource, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3230                                               boolean needLock, boolean forceCertificationAllowed) {
3231         boolean failed = false;
3232         try {
3233             if (resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed && lifecycleBusinessLogic
3234                 .isFirstCertification(resource.getVersion())) {
3235                 nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
3236             }
3237             if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) {
3238                 Either<ArtifactDefinition, Operation> eitherPopulated = populateToscaArtifacts(resource, user, false, inTransaction, needLock, false);
3239                 return resource;
3240             }
3241             return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock);
3242         } catch (ComponentException e) {
3243             failed = true;
3244             log.debug("The exception has occurred upon certification of resource {}. ", resource.getName(), e);
3245             throw e;
3246         } finally {
3247             if (failed) {
3248                 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3249                 if (!inTransaction) {
3250                     janusGraphDao.rollback();
3251                 }
3252             } else if (!inTransaction) {
3253                 janusGraphDao.commit();
3254             }
3255         }
3256     }
3257
3258     private Resource nodeFullCertification(String uniqueId, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3259                                            boolean needLock) {
3260         Either<Resource, ResponseFormat> resourceResponse = lifecycleBusinessLogic
3261             .changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, inTransaction, needLock);
3262         if (resourceResponse.isRight()) {
3263             throw new ByResponseFormatComponentException(resourceResponse.right().value());
3264         }
3265         return resourceResponse.left().value();
3266     }
3267
3268     private Resource nodeForceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3269                                             boolean needLock) {
3270         return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
3271     }
3272
3273     public ImmutablePair<Resource, ActionStatus> createOrUpdateResourceByImport(final Resource resource, final User user, final boolean isNormative,
3274                                                                                 final boolean isInTransaction, final boolean needLock,
3275                                                                                 final CsarInfo csarInfo, final String nodeName,
3276                                                                                 final boolean isNested) {
3277         ImmutablePair<Resource, ActionStatus> result = null;
3278         // check if resource already exists (search by tosca name = type)
3279         final boolean isNestedResource = isNestedResourceUpdate(csarInfo, nodeName);
3280         final Either<Resource, StorageOperationStatus> latestByToscaName = toscaOperationFacade
3281             .getLatestByToscaResourceName(resource.getToscaResourceName());
3282         if (latestByToscaName.isLeft()) {
3283             Resource foundResource = latestByToscaName.left().value();
3284             // we don't allow updating names of top level types
3285             if (!isNestedResource && !StringUtils.equals(resource.getName(), foundResource.getName())) {
3286                 BeEcompErrorManager.getInstance()
3287                     .logBeComponentMissingError("Create / Update resource by import", ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
3288                 log.debug("resource already exist new name={} old name={} same type={}", resource.getName(), foundResource.getName(),
3289                     resource.getToscaResourceName());
3290                 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS);
3291                 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
3292                 throwComponentException(responseFormat);
3293             }
3294             result = updateExistingResourceByImport(resource, foundResource, user, isNormative, needLock, isNested);
3295         } else if (isNotFound(latestByToscaName)) {
3296             if (isNestedResource) {
3297                 result = createOrUpdateNestedResource(resource, user, isNormative, isInTransaction, needLock, csarInfo, isNested, nodeName);
3298             } else {
3299                 result = createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3300             }
3301         } else {
3302             StorageOperationStatus status = latestByToscaName.right().value();
3303             log.debug("failed to get latest version of resource {}. status={}", resource.getName(), status);
3304             ResponseFormat responseFormat = componentsUtils
3305                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(latestByToscaName.right().value()), resource);
3306             componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
3307             throwComponentException(responseFormat);
3308         }
3309         return result;
3310     }
3311
3312     private boolean isNestedResourceUpdate(CsarInfo csarInfo, String nodeName) {
3313         return csarInfo != null && csarInfo.isUpdate() && nodeName != null;
3314     }
3315
3316     private ImmutablePair<Resource, ActionStatus> createOrUpdateNestedResource(final Resource resource, final User user, final boolean isNormative,
3317                                                                                final boolean isInTransaction, final boolean needLock,
3318                                                                                final CsarInfo csarInfo, final boolean isNested,
3319                                                                                final String nodeName) {
3320         final Either<Component, StorageOperationStatus> latestByToscaName = toscaOperationFacade.getLatestByToscaResourceName(
3321             buildNestedToscaResourceName(resource.getResourceType().name(), csarInfo.getVfResourceName(), nodeName).getRight());
3322         if (latestByToscaName.isLeft()) {
3323             final Resource nestedResource = (Resource) latestByToscaName.left().value();
3324             log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
3325             final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(nestedResource, resource,
3326                 ValidationUtils.hasBeenCertified(nestedResource.getVersion()));
3327             if (eitherValidation.isRight()) {
3328                 return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3329             }
3330             return updateExistingResourceByImport(resource, nestedResource, user, isNormative, needLock, isNested);
3331         } else {
3332             return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3333         }
3334     }
3335
3336     private boolean isNotFound(Either<Resource, StorageOperationStatus> getResourceEither) {
3337         return getResourceEither.isRight() && getResourceEither.right().value() == StorageOperationStatus.NOT_FOUND;
3338     }
3339
3340     private ImmutablePair<Resource, ActionStatus> createResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction,
3341                                                                          CsarInfo csarInfo) {
3342         log.debug("resource with name {} does not exist. create new resource", resource.getName());
3343         validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isInTransaction, csarInfo);
3344         final Resource createResourceByDao = createResourceByDao(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isNormative, isInTransaction);
3345         Resource createdResource = updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value();
3346         ImmutablePair<Resource, ActionStatus> resourcePair = new ImmutablePair<>(createdResource, ActionStatus.CREATED);
3347         ASDCKpiApi.countImportResourcesKPI();
3348         return resourcePair;
3349     }
3350
3351     public boolean isResourceExist(String resourceName) {
3352         Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resourceName);
3353         return latestByName.isLeft();
3354     }
3355
3356     private ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
3357                                                                                  boolean inTransaction, boolean needLock, boolean isNested) {
3358         String lockedResourceId = oldResource.getUniqueId();
3359         log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
3360             oldResource.getLifecycleState());
3361         ImmutablePair<Resource, ActionStatus> resourcePair = null;
3362         try {
3363             lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3364             oldResource = prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3365             mergeOldResourceMetadataWithNew(oldResource, newResource);
3366             validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3367             validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3368             // contact info normalization
3369             newResource.setContactId(newResource.getContactId().toLowerCase());
3370             PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
3371             // non-updatable fields
3372             newResource.setCreatorUserId(user.getUserId());
3373             newResource.setCreatorFullName(user.getFullName());
3374             newResource.setLastUpdaterUserId(user.getUserId());
3375             newResource.setLastUpdaterFullName(user.getFullName());
3376             newResource.setUniqueId(oldResource.getUniqueId());
3377             newResource.setVersion(oldResource.getVersion());
3378             newResource.setInvariantUUID(oldResource.getInvariantUUID());
3379             newResource.setLifecycleState(oldResource.getLifecycleState());
3380             newResource.setUUID(oldResource.getUUID());
3381             newResource.setNormalizedName(oldResource.getNormalizedName());
3382             newResource.setSystemName(oldResource.getSystemName());
3383             if (oldResource.getCsarUUID() != null) {
3384                 newResource.setCsarUUID(oldResource.getCsarUUID());
3385             }
3386             if (oldResource.getImportedToscaChecksum() != null) {
3387                 newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3388             }
3389             newResource.setAbstract(oldResource.isAbstract());
3390             if (CollectionUtils.isEmpty(newResource.getDerivedFrom())) {
3391                 newResource.setDerivedFrom(oldResource.getDerivedFrom());
3392             }
3393             if (CollectionUtils.isEmpty(newResource.getDataTypes())) {
3394                 newResource.setDataTypes(oldResource.getDataTypes());
3395             }
3396             if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())) {
3397                 newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3398             }
3399             if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())) {
3400                 newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3401             }
3402             // add for new)
3403
3404             // created without tosca artifacts - add the placeholders
3405             if (MapUtils.isEmpty(newResource.getToscaArtifacts())) {
3406                 setToscaArtifactsPlaceHolders(newResource, user);
3407             }
3408             if (MapUtils.isEmpty(newResource.getInterfaces())) {
3409                 newResource.setInterfaces(oldResource.getInterfaces());
3410             }
3411             if (CollectionUtils.isEmpty(newResource.getAttributes())) {
3412                 newResource.setAttributes(oldResource.getAttributes());
3413             }
3414             if (CollectionUtils.isEmpty(newResource.getProperties())) {
3415                 newResource.setProperties(oldResource.getProperties());
3416             }
3417             Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
3418             if (overrideResource.isRight()) {
3419                 ResponseFormat responseFormat = componentsUtils
3420                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(overrideResource.right().value()), newResource);
3421                 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE);
3422                 throwComponentException(responseFormat);
3423             }
3424             updateCatalog(overrideResource.left().value(), ChangeTypeEnum.LIFECYCLE);
3425             log.debug("Resource updated successfully!!!");
3426             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3427             componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
3428                 ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
3429             resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
3430             return resourcePair;
3431         } finally {
3432             if (resourcePair == null) {
3433                 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3434                 janusGraphDao.rollback();
3435             } else if (!inTransaction) {
3436                 janusGraphDao.commit();
3437             }
3438             if (needLock) {
3439                 log.debug("unlock resource {}", lockedResourceId);
3440                 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3441             }
3442         }
3443     }
3444
3445     /**
3446      * Merge old resource with new. Keep old category and vendor name without change
3447      *
3448      * @param oldResource
3449      * @param newResource
3450      */
3451     private void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) {
3452         // keep old category and vendor name without change
3453
3454         // merge the rest of the resource metadata
3455         if (newResource.getTags() == null || newResource.getTags().isEmpty()) {
3456             newResource.setTags(oldResource.getTags());
3457         }
3458         if (newResource.getDescription() == null) {
3459             newResource.setDescription(oldResource.getDescription());
3460         }
3461         if (newResource.getVendorRelease() == null) {
3462             newResource.setVendorRelease(oldResource.getVendorRelease());
3463         }
3464         if (newResource.getResourceVendorModelNumber() == null) {
3465             newResource.setResourceVendorModelNumber(oldResource.getResourceVendorModelNumber());
3466         }
3467         if (newResource.getContactId() == null) {
3468             newResource.setContactId(oldResource.getContactId());
3469         }
3470         newResource.setCategories(oldResource.getCategories());
3471         if (newResource.getVendorName() == null) {
3472             newResource.setVendorName(oldResource.getVendorName());
3473         }
3474         List<GroupDefinition> oldForUpdate = oldResource.getGroups();
3475         if (CollectionUtils.isNotEmpty(oldForUpdate)) {
3476             List<GroupDefinition> groupForUpdate = oldForUpdate.stream().map(GroupDefinition::new).collect(Collectors.toList());
3477             groupForUpdate.stream().filter(GroupDataDefinition::isVspOriginated).forEach(group -> group.setName(group.getInvariantName()));
3478             newResource.setGroups(groupForUpdate);
3479         }
3480         if (newResource.getResourceType().isAtomicType() && !newResource.getName().equals("Root")
3481             && newResource.getResourceType() != ResourceTypeEnum.CVFC) {
3482             ResourceTypeEnum updatedResourceType = newResource.getResourceType();
3483             Component derivedFromResource = getParentComponent(newResource);
3484             if (derivedFromResource.getComponentType() == ComponentTypeEnum.RESOURCE) {
3485                 Resource parentResource = (Resource) derivedFromResource;
3486                 if (!(parentResource.isAbstract() && (ResourceTypeEnum.VFC == parentResource.getResourceType()
3487                     || ResourceTypeEnum.ABSTRACT == parentResource.getResourceType())) && parentResource.getResourceType() != updatedResourceType
3488                     && oldResource.getResourceType() != updatedResourceType) {
3489                     BeEcompErrorManager.getInstance().logInternalDataError("mergeOldResourceMetadataWithNew",
3490                         "resource type of the resource does not match to derived from resource type", ErrorSeverity.ERROR);
3491                     log.debug(
3492                         "#mergeOldResourceMetadataWithNew - resource type {} of the resource {} does not match to derived from resource type {}",
3493                         newResource.getResourceType(), newResource.getToscaResourceName(), parentResource.getResourceType());
3494                     throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_TYPE);
3495                 }
3496             }
3497         }
3498     }
3499
3500     private Component getParentComponent(Resource newResource) {
3501         String toscaResourceNameDerivedFrom = newResource.getDerivedFrom().get(0);
3502         Either<Component, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade
3503             .getLatestByToscaResourceName(toscaResourceNameDerivedFrom);
3504         if (latestByToscaResourceName.isRight()) {
3505             BeEcompErrorManager.getInstance()
3506                 .logInternalDataError("mergeOldResourceMetadataWithNew", "derived from resource not found", ErrorSeverity.ERROR);
3507             log.debug("#mergeOldResourceMetadataWithNew - derived from resource {} not found", toscaResourceNameDerivedFrom);
3508             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, toscaResourceNameDerivedFrom);
3509         }
3510         return latestByToscaResourceName.left().value();
3511     }
3512
3513     private Resource prepareResourceForUpdate(Resource oldResource, Resource newResource, User user, boolean inTransaction, boolean needLock) {
3514         if (!ComponentValidationUtils.canWorkOnResource(oldResource, user.getUserId())) {
3515             // checkout
3516             return lifecycleBusinessLogic
3517                 .changeState(oldResource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, new LifecycleChangeInfoWithAction("update by import"),
3518                     inTransaction, needLock).left().on(response -> failOnChangeState(response, user, oldResource, newResource));
3519         }
3520         return oldResource;
3521     }
3522
3523     private Resource failOnChangeState(ResponseFormat response, User user, Resource oldResource, Resource newResource) {
3524         log.info("resource {} cannot be updated. reason={}", oldResource.getUniqueId(), response.getFormattedMessage());
3525         componentsUtils.auditResource(response, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
3526             ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
3527         throw new ByResponseFormatComponentException(response);
3528     }
3529
3530     public Resource validateResourceBeforeCreate(Resource resource, User user, AuditingActionEnum actionEnum, boolean inTransaction,
3531                                                  CsarInfo csarInfo) {
3532         validateResourceFieldsBeforeCreate(user, resource, actionEnum, inTransaction);
3533         validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), resource, actionEnum, inTransaction);
3534         validateLifecycleTypesCreate(user, resource, actionEnum);
3535         validateResourceType(user, resource, actionEnum);
3536         resource.setCreatorUserId(user.getUserId());
3537         resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
3538         resource.setContactId(resource.getContactId().toLowerCase());
3539         if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) {
3540             String resourceSystemName;
3541             if (csarInfo != null && StringUtils.isNotEmpty(csarInfo.getVfResourceName())) {
3542                 resourceSystemName = ValidationUtils.convertToSystemName(csarInfo.getVfResourceName());
3543             } else {
3544                 resourceSystemName = resource.getSystemName();
3545             }
3546             resource
3547                 .setToscaResourceName(CommonBeUtils.generateToscaResourceName(resource.getResourceType().name().toLowerCase(), resourceSystemName));
3548         }
3549         // Generate invariant UUID - must be here and not in operation since it
3550
3551         // should stay constant during clone
3552
3553         // TODO
3554         String invariantUUID = UniqueIdBuilder.buildInvariantUUID();
3555         resource.setInvariantUUID(invariantUUID);
3556         return resource;
3557     }
3558
3559     private Either<Boolean, ResponseFormat> validateResourceType(User user, Resource resource, AuditingActionEnum actionEnum) {
3560         Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3561         if (resource.getResourceType() == null) {
3562             log.debug("Invalid resource type for resource");
3563             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
3564             eitherResult = Either.right(errorResponse);
3565             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3566         }
3567         return eitherResult;
3568     }
3569
3570     private Either<Boolean, ResponseFormat> validateLifecycleTypesCreate(User user, Resource resource, AuditingActionEnum actionEnum) {
3571         Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3572         if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) {
3573             log.debug("validate interface lifecycle Types Exist");
3574             Iterator<InterfaceDefinition> intItr = resource.getInterfaces().values().iterator();
3575             while (intItr.hasNext() && eitherResult.isLeft()) {
3576                 InterfaceDefinition interfaceDefinition = intItr.next();
3577                 String intType = interfaceDefinition.getUniqueId();
3578                 Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation.getInterface(intType);
3579                 if (eitherCapTypeFound.isRight()) {
3580                     if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3581                         BeEcompErrorManager.getInstance()
3582                             .logBeGraphObjectMissingError("Create Resource - validateLifecycleTypesCreate", "Interface", intType);
3583                         log.debug("Lifecycle Type: {} is required by resource: {} but does not exist in the DB", intType, resource.getName());
3584                         BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateLifecycleTypesCreate");
3585                         log.debug("request to data model failed with error: {}", eitherCapTypeFound.right().value().name());
3586                     }
3587                     ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_LIFECYCLE_TYPE, intType);
3588                     eitherResult = Either.right(errorResponse);
3589                     componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3590                 }
3591             }
3592         }
3593         return eitherResult;
3594     }
3595
3596     private Either<Boolean, ResponseFormat> validateCapabilityTypesCreate(User user, ICapabilityTypeOperation capabilityTypeOperation,
3597                                                                           Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
3598         Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3599         if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) {
3600             log.debug("validate capability Types Exist - capabilities section");
3601             for (Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) {
3602                 eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, actionEnum, eitherResult, typeEntry,
3603                     inTransaction);
3604                 if (eitherResult.isRight()) {
3605                     return Either.right(eitherResult.right().value());
3606                 }
3607             }
3608         }
3609         if (resource.getRequirements() != null && resource.getRequirements().size() > 0) {
3610             log.debug("validate capability Types Exist - requirements section");
3611             for (String type : resource.getRequirements().keySet()) {
3612                 eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, resource.getRequirements().get(type), actionEnum,
3613                     eitherResult, type, inTransaction);
3614                 if (eitherResult.isRight()) {
3615                     return Either.right(eitherResult.right().value());
3616                 }
3617             }
3618         }
3619         return eitherResult;
3620     }
3621
3622     // @param typeObject- the object to which the validation is done
3623     private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
3624                                                                          Resource resource, List<?> validationObjects, AuditingActionEnum actionEnum,
3625                                                                          Either<Boolean, ResponseFormat> eitherResult, String type,
3626                                                                          boolean inTransaction) {
3627         Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation.getCapabilityType(type, inTransaction);
3628         if (eitherCapTypeFound.isRight()) {
3629             if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3630                 BeEcompErrorManager.getInstance().logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", type);
3631                 log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", type, resource.getName());
3632                 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
3633             }
3634             log.debug("Trying to get capability type {} failed with error: {}", type, eitherCapTypeFound.right().value().name());
3635             ResponseFormat errorResponse = null;
3636             if (type != null) {
3637                 errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, type);
3638             } else {
3639                 errorResponse = componentsUtils.getResponseFormatByElement(ActionStatus.MISSING_CAPABILITY_TYPE, validationObjects);
3640             }
3641             eitherResult = Either.right(errorResponse);
3642             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3643         }
3644         return eitherResult;
3645     }
3646
3647     private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
3648                                                                          Resource resource, AuditingActionEnum actionEnum,
3649                                                                          Either<Boolean, ResponseFormat> eitherResult,
3650                                                                          Entry<String, List<CapabilityDefinition>> typeEntry, boolean inTransaction) {
3651         Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation
3652             .getCapabilityType(typeEntry.getKey(), inTransaction);
3653         if (eitherCapTypeFound.isRight()) {
3654             if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3655                 BeEcompErrorManager.getInstance()
3656                     .logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", typeEntry.getKey());
3657                 log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", typeEntry.getKey(), resource.getName());
3658                 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
3659             }
3660             log.debug("Trying to get capability type {} failed with error: {}", typeEntry.getKey(), eitherCapTypeFound.right().value().name());
3661             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, typeEntry.getKey());
3662             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3663             return Either.right(errorResponse);
3664         }
3665         CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value();
3666         if (capabilityTypeDefinition.getProperties() != null) {
3667             for (CapabilityDefinition capDef : typeEntry.getValue()) {
3668                 List<ComponentInstanceProperty> properties = capDef.getProperties();
3669                 List<ComponentInstanceProperty> changedProperties = new ArrayList<>();
3670                 if (properties == null || properties.isEmpty()) {
3671                     for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
3672                         ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue());
3673                         changedProperties.add(newProp);
3674                     }
3675                 } else {
3676                     List<ComponentInstanceProperty> propsToAdd = new ArrayList<>();
3677                     for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
3678                         PropertyDefinition propFromDef = prop.getValue();
3679                         boolean propFound = false;
3680                         for (ComponentInstanceProperty cip : properties) {
3681                             if (propFromDef.getName().equals(cip.getName())) {
3682                                 //merge property value and property description only, ignore other fields
3683                                 if (cip.getDescription() != null && !cip.getDescription().equals(propFromDef.getDescription())) {
3684                                     propFromDef.setDescription(cip.getDescription());
3685                                 }
3686                                 propertyDataValueMergeBusinessLogic.mergePropertyValue(propFromDef, cip, new ArrayList<>());
3687                                 if (cip.getValue() != null) {
3688                                     propFromDef.setValue(cip.getValue());
3689                                 }
3690                                 propsToAdd.add(new ComponentInstanceProperty(propFromDef));
3691                                 propFound = true;
3692                                 properties.remove(cip);
3693                                 break;
3694                             }
3695                         }
3696                         if (!propFound) {
3697                             propsToAdd.add(new ComponentInstanceProperty(propFromDef));
3698                         }
3699                     }
3700                     if (!propsToAdd.isEmpty()) {
3701                         changedProperties.addAll(propsToAdd);
3702                     }
3703                 }
3704                 capDef.setProperties(changedProperties);
3705             }
3706         }
3707         return eitherResult;
3708     }
3709
3710     public Resource createResourceByDao(Resource resource, User user, AuditingActionEnum actionEnum, boolean isNormative, boolean inTransaction) {
3711         // create resource
3712
3713         // lock new resource name in order to avoid creation resource with same
3714
3715         // name
3716         Resource createdResource = null;
3717         if (!inTransaction) {
3718             Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
3719             if (lockResult.isRight()) {
3720                 ResponseFormat responseFormat = lockResult.right().value();
3721                 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
3722                 throw new ByResponseFormatComponentException(responseFormat);
3723             }
3724             log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
3725         }
3726         try {
3727             if (resource.deriveFromGeneric()) {
3728                 handleResourceGenericType(resource);
3729             }
3730             createdResource = createResourceTransaction(resource, user, isNormative);
3731             componentsUtils.auditResource(componentsUtils.getResponseFormat(ActionStatus.CREATED), user, createdResource, actionEnum);
3732             ASDCKpiApi.countCreatedResourcesKPI();
3733         } catch (ComponentException e) {
3734             ResponseFormat responseFormat =
3735                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3736             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
3737             throw e;
3738         } catch (StorageException e) {
3739             ResponseFormat responseFormat = componentsUtils
3740                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3741             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
3742             throw e;
3743         } finally {
3744             if (!inTransaction) {
3745                 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
3746             }
3747         }
3748         return createdResource;
3749     }
3750
3751     private Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
3752         // validate resource name uniqueness
3753         log.debug("validate resource name");
3754         Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
3755             .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
3756         if (eitherValidation.isRight()) {
3757             loggerSupportability.log(LoggerSupportabilityActions.VALIDATE_NAME, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3758                 "ERROR while validate component name {} Status is: {}", resource.getName(), eitherValidation.right().value());
3759             log.debug("Failed to validate component name {}. Status is {}. ", resource.getName(), eitherValidation.right().value());
3760             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
3761         }
3762         if (eitherValidation.left().value()) {
3763             log.debug("resource with name: {}, already exists", resource.getName());
3764             loggerSupportability
3765                 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3766                     "resource with name: {} already exists", resource.getName());
3767             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
3768                 resource.getName());
3769         }
3770         log.debug("send resource {} to dao for create", resource.getName());
3771         createArtifactsPlaceHolderData(resource, user);
3772         // enrich object
3773         if (!isNormative) {
3774             log.debug("enrich resource with creator, version and state");
3775             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
3776             resource.setVersion(INITIAL_VERSION);
3777             resource.setHighestVersion(true);
3778             if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.CVFC) {
3779                 resource.setAbstract(false);
3780             }
3781         }
3782         return toscaOperationFacade.createToscaComponent(resource).left().on(r -> throwComponentExceptionByResource(r, resource));
3783     }
3784
3785     private Resource throwComponentExceptionByResource(StorageOperationStatus status, Resource resource) {
3786         ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(status), resource);
3787         throw new ByResponseFormatComponentException(responseFormat);
3788     }
3789
3790     private void createArtifactsPlaceHolderData(Resource resource, User user) {
3791         // create mandatory artifacts
3792
3793         // TODO it must be removed after that artifact uniqueId creation will be
3794
3795         // moved to ArtifactOperation
3796         setInformationalArtifactsPlaceHolder(resource, user);
3797         setDeploymentArtifactsPlaceHolder(resource, user);
3798         setToscaArtifactsPlaceHolders(resource, user);
3799     }
3800
3801     @SuppressWarnings("unchecked")
3802     @Override
3803     public void setDeploymentArtifactsPlaceHolder(Component component, User user) {
3804         Resource resource = (Resource) component;
3805         Map<String, ArtifactDefinition> artifactMap = resource.getDeploymentArtifacts();
3806         if (artifactMap == null) {
3807             artifactMap = new HashMap<>();
3808         }
3809         Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
3810             .getDeploymentResourceArtifacts();
3811         if (deploymentResourceArtifacts != null) {
3812             Map<String, ArtifactDefinition> finalArtifactMap = artifactMap;
3813             deploymentResourceArtifacts.forEach((k, v) -> processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k, v));
3814         }
3815         resource.setDeploymentArtifacts(artifactMap);
3816     }
3817
3818     private void processDeploymentResourceArtifacts(User user, Resource resource, Map<String, ArtifactDefinition> artifactMap, String k, Object v) {
3819         Map<String, Object> artifactDetails = (Map<String, Object>) v;
3820         Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES);
3821         if (object != null) {
3822             List<String> artifactTypes = (List<String>) object;
3823             if (!artifactTypes.contains(resource.getResourceType().name())) {
3824                 return;
3825             }
3826         } else {
3827             log.info("resource types for artifact placeholder {} were not defined. default is all resources", k);
3828         }
3829         if (artifactsBusinessLogic != null) {
3830             ArtifactDefinition artifactDefinition = artifactsBusinessLogic
3831                 .createArtifactPlaceHolderInfo(resource.getUniqueId(), k, (Map<String, Object>) v, user, ArtifactGroupTypeEnum.DEPLOYMENT);
3832             if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) {
3833                 artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
3834             }
3835         }
3836     }
3837
3838     @SuppressWarnings("unchecked")
3839     private void setInformationalArtifactsPlaceHolder(Resource resource, User user) {
3840         Map<String, ArtifactDefinition> artifactMap = resource.getArtifacts();
3841         if (artifactMap == null) {
3842             artifactMap = new HashMap<>();
3843         }
3844         String resourceUniqueId = resource.getUniqueId();
3845         List<String> exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceCategory();
3846         List<String> exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceType();
3847         Map<String, Object> informationalResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
3848             .getInformationalResourceArtifacts();
3849         List<CategoryDefinition> categories = resource.getCategories();
3850         boolean isCreateArtifact = true;
3851         if (exludeResourceCategory != null) {
3852             String category = categories.get(0).getName();
3853             isCreateArtifact = exludeResourceCategory.stream().noneMatch(e -> e.equalsIgnoreCase(category));
3854         }
3855         if (isCreateArtifact && exludeResourceType != null) {
3856             String resourceType = resource.getResourceType().name();
3857             isCreateArtifact = exludeResourceType.stream().noneMatch(e -> e.equalsIgnoreCase(resourceType));
3858         }
3859         if (informationalResourceArtifacts != null && isCreateArtifact) {
3860             Set<String> keys = informationalResourceArtifacts.keySet();
3861             for (String informationalResourceArtifactName : keys) {
3862                 Map<String, Object> artifactInfoMap = (Map<String, Object>) informationalResourceArtifacts.get(informationalResourceArtifactName);
3863                 ArtifactDefinition artifactDefinition = artifactsBusinessLogic
3864                     .createArtifactPlaceHolderInfo(resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user,
3865                         ArtifactGroupTypeEnum.INFORMATIONAL);
3866                 artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
3867             }
3868         }
3869         resource.setArtifacts(artifactMap);
3870     }
3871
3872     /**
3873      * deleteResource
3874      *
3875      * @param resourceId
3876      * @param user
3877      * @return
3878      */
3879     public ResponseFormat deleteResource(String resourceId, User user) {
3880         ResponseFormat responseFormat;
3881         validateUserExists(user);
3882         Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade.getToscaElement(resourceId);
3883         if (resourceStatus.isRight()) {
3884             log.debug("failed to get resource {}", resourceId);
3885             return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), "");
3886         }
3887         Resource resource = resourceStatus.left().value();
3888         StorageOperationStatus result = StorageOperationStatus.OK;
3889         lockComponent(resourceId, resource, "Mark resource to delete");
3890         try {
3891             result = markComponentToDelete(resource);
3892             if (result == StorageOperationStatus.OK) {
3893                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
3894             } else {
3895                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
3896                 responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
3897             }
3898             return responseFormat;
3899         } finally {
3900             if (!StorageOperationStatus.OK.equals(result)) {
3901                 janusGraphDao.rollback();
3902             } else {
3903                 janusGraphDao.commit();
3904             }
3905             graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
3906         }
3907     }
3908
3909     public ResponseFormat deleteResourceByNameAndVersion(String resourceName, String version, User user) {
3910         ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
3911         validateUserExists(user);
3912         Resource resource = null;
3913         StorageOperationStatus result = StorageOperationStatus.OK;
3914         boolean failed = false;
3915         try {
3916             Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade
3917                 .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, version);
3918             if (resourceStatus.isRight()) {
3919                 log.debug("failed to get resource {} version {}", resourceName, version);
3920                 return componentsUtils
3921                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), resourceName);
3922             }
3923             resource = resourceStatus.left().value();
3924         } finally {
3925             janusGraphDao.commit();
3926         }
3927         if (resource != null) {
3928             lockComponent(resource.getUniqueId(), resource, DELETE_RESOURCE);
3929             try {
3930                 result = markComponentToDelete(resource);
3931                 if (result != StorageOperationStatus.OK) {
3932                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
3933                     responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
3934                     return responseFormat;
3935                 }
3936             } catch (ComponentException e) {
3937                 failed = true;
3938                 throw e;
3939             } finally {
3940                 if (failed || !StorageOperationStatus.OK.equals(result)) {
3941                     janusGraphDao.rollback();
3942                 } else {
3943                     janusGraphDao.commit();
3944                 }
3945                 graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource);
3946             }
3947         }
3948         return responseFormat;
3949     }
3950
3951     public Either<Resource, ResponseFormat> getResource(String resourceId, User user) {
3952         if (user != null) {
3953             validateUserExists(user);
3954         }
3955         Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceId);
3956         if (storageStatus.isRight()) {
3957             log.debug("failed to get resource by id {}", resourceId);
3958             return Either.right(
3959                 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), resourceId));
3960         }
3961         if (storageStatus.left().value() == null) {
3962             return Either.right(componentsUtils
3963                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), resourceId));
3964         }
3965         return Either.left(storageStatus.left().value());
3966     }
3967
3968     public Either<Resource, ResponseFormat> getResourceByNameAndVersion(String resourceName, String resourceVersion, String userId) {
3969         validateUserExists(userId);
3970         Either<Resource, StorageOperationStatus> getResource = toscaOperationFacade
3971             .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion);
3972         if (getResource.isRight()) {
3973             log.debug("failed to get resource by name {} and version {}", resourceName, resourceVersion);
3974             return Either.right(
3975                 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResource.right().value()), resourceName));
3976         }
3977         return Either.left(getResource.left().value());
3978     }
3979
3980     /**
3981      * updateResourceMetadata
3982      *
3983      * @param user               - modifier data (userId)
3984      * @param inTransaction      TODO
3985      * @param resourceIdToUpdate - the resource identifier
3986      * @param newResource
3987      * @return Either<Resource, responseFormat>
3988      */
3989     public Resource updateResourceMetadata(String resourceIdToUpdate, Resource newResource, Resource currentResource, User user,
3990                                            boolean inTransaction) {
3991         validateUserExists(user.getUserId());
3992         log.debug("Get resource with id {}", resourceIdToUpdate);
3993         boolean needToUnlock = false;
3994         try {
3995             if (currentResource == null) {
3996                 Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceIdToUpdate);
3997                 if (storageStatus.isRight()) {
3998                     throw new ByResponseFormatComponentException(
3999                         componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), ""));
4000                 }
4001                 currentResource = storageStatus.left().value();
4002             }
4003             // verify that resource is checked-out and the user is the last
4004
4005             // updater
4006             if (!ComponentValidationUtils.canWorkOnResource(currentResource, user.getUserId())) {
4007                 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
4008             }
4009             // lock resource
4010             StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
4011             if (lockResult != StorageOperationStatus.OK) {
4012                 BeEcompErrorManager.getInstance()
4013                     .logBeFailedLockObjectError("Upload Artifact - lock ", NodeTypeEnum.Resource.getName(), resourceIdToUpdate);
4014                 log.debug("Failed to lock resource: {}, error - {}", resourceIdToUpdate, lockResult);
4015                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult));
4016                 throw new ByResponseFormatComponentException(responseFormat);
4017             }
4018             needToUnlock = true;
4019             // critical section starts here
4020
4021             // convert json to object
4022
4023             // Update and updated resource must have a non-empty "derivedFrom"
4024
4025             // list
4026
4027             // This code is not called from import resources, because of root
4028
4029             // VF "derivedFrom" should be null (or ignored)
4030             if (ModelConverter.isAtomicComponent(currentResource)) {
4031                 validateDerivedFromNotEmpty(null, newResource, null);
4032                 validateDerivedFromNotEmpty(null, currentResource, null);
4033             } else {
4034                 newResource.setDerivedFrom(null);
4035             }
4036             Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false,
4037                 true);
4038             if (dataModelResponse.isRight()) {
4039                 log.debug("failed to update resource metadata!!!");
4040                 throw new ByResponseFormatComponentException(dataModelResponse.right().value());
4041             }
4042             log.debug("Resource metadata updated successfully!!!");
4043             return dataModelResponse.left().value();
4044         } catch (ComponentException | StorageException e) {
4045             rollback(inTransaction, newResource, null, null);
4046             throw e;
4047         } finally {
4048             if (!inTransaction) {
4049                 janusGraphDao.commit();
4050             }
4051             if (needToUnlock) {
4052                 graphLockOperation.unlockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
4053             }
4054         }
4055     }
4056
4057     private Either<Resource, ResponseFormat> updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user,
4058                                                                     Resource currentResource, boolean shouldLock, boolean inTransaction) {
4059         updateVfModuleGroupsNames(currentResource, newResource);
4060         validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction, false);
4061         // Setting last updater and uniqueId
4062         newResource.setContactId(newResource.getContactId().toLowerCase());
4063         newResource.setLastUpdaterUserId(user.getUserId());
4064         newResource.setUniqueId(resourceIdToUpdate);
4065         // Cannot set highest version through UI
4066         newResource.setHighestVersion(currentResource.isHighestVersion());
4067         newResource.setCreationDate(currentResource.getCreationDate());
4068         Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom = processUpdateOfDerivedFrom(currentResource, newResource, user.getUserId(),
4069             inTransaction);
4070         if (processUpdateOfDerivedFrom.isRight()) {
4071             log.debug("Couldn't update derived from for resource {}", resourceIdToUpdate);
4072             return Either.right(processUpdateOfDerivedFrom.right().value());
4073         }
4074         log.debug("send resource {} to dao for update", newResource.getUniqueId());
4075         if (isNotEmpty(newResource.getGroups())) {
4076             for (GroupDefinition group : newResource.getGroups()) {
4077                 if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
4078                     groupBusinessLogic
4079                         .validateAndUpdateGroupMetadata(newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getUniqueId(), user,
4080                             newResource.getComponentType(), group, true, false);
4081                 }
4082             }
4083         }
4084         Either<Resource, StorageOperationStatus> dataModelResponse = toscaOperationFacade.updateToscaElement(newResource);
4085         if (dataModelResponse.isRight()) {
4086             ResponseFormat responseFormat = componentsUtils
4087                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), newResource);
4088             return Either.right(responseFormat);
4089         } else if (dataModelResponse.left().value() == null) {
4090             log.debug("No response from updateResource");
4091             return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
4092         }
4093         return Either.left(dataModelResponse.left().value());
4094     }
4095
4096     private void updateVfModuleGroupsNames(Resource currentResource, Resource newResource) {
4097         if (currentResource.getGroups() != null && !currentResource.getName().equals(newResource.getName())) {
4098             List<GroupDefinition> updatedGroups = currentResource.getGroups().stream()
4099                 .map(group -> getUpdatedGroup(group, currentResource.getName(), newResource.getName())).collect(toList());
4100             newResource.setGroups(updatedGroups);
4101         }
4102     }
4103
4104     private GroupDefinition getUpdatedGroup(GroupDefinition currGroup, String replacePattern, String with) {
4105         GroupDefinition updatedGroup = new GroupDefinition(currGroup);
4106         if (updatedGroup.isSamePrefix(replacePattern) && updatedGroup.getType().equals(DEFAULT_GROUP_VF_MODULE)) {
4107             String prefix = updatedGroup.getName().substring(0, replacePattern.length());
4108             String newGroupName = updatedGroup.getName().replaceFirst(prefix, with);
4109             updatedGroup.setName(newGroupName);
4110         }
4111         return updatedGroup;
4112     }
4113
4114     /**
4115      * validateResourceFieldsBeforeCreate
4116      *
4117      * @param user - modifier data (userId)
4118      */
4119     private void validateResourceFieldsBeforeCreate(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
4120         componentValidator.validate(user, resource, actionEnum);
4121         // validate category
4122         log.debug("validate category");
4123         validateCategory(user, resource, actionEnum, inTransaction);
4124         // validate vendor name & release & model number
4125         log.debug("validate vendor name");
4126         validateVendorName(user, resource, actionEnum);
4127         log.debug("validate vendor release");
4128         validateVendorReleaseName(user, resource, actionEnum);
4129         log.debug("validate resource vendor model number");
4130         validateResourceVendorModelNumber(user, resource, actionEnum);
4131         // validate cost
4132         log.debug("validate cost");
4133         validateCost(resource);
4134         // validate licenseType
4135         log.debug("validate licenseType");
4136         validateLicenseType(user, resource, actionEnum);
4137         // validate template (derived from)
4138         log.debug("validate derived from");
4139         if (!ModelConverter.isAtomicComponent(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
4140             resource.setDerivedFrom(null);
4141         }
4142         validateDerivedFromExist(user, resource, actionEnum);
4143         // warn about non-updatable fields
4144         checkComponentFieldsForOverrideAttempt(resource);
4145         String currentCreatorFullName = resource.getCreatorFullName();
4146         if (currentCreatorFullName != null) {
4147             log.debug("Resource Creator fullname is automatically set and cannot be updated");
4148         }
4149         String currentLastUpdaterFullName = resource.getLastUpdaterFullName();
4150         if (currentLastUpdaterFullName != null) {
4151             log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
4152         }
4153         Long currentLastUpdateDate = resource.getLastUpdateDate();
4154         if (currentLastUpdateDate != null) {
4155             log.debug("Resource last update date is automatically set and cannot be updated");
4156         }
4157         Boolean currentAbstract = resource.isAbstract();
4158         if (currentAbstract != null) {
4159             log.debug("Resource abstract is automatically set and cannot be updated");
4160         }
4161     }
4162
4163     /**
4164      * validateResourceFieldsBeforeUpdate
4165      *
4166      * @param currentResource - Resource object to validate
4167      * @param isNested
4168      */
4169     private void validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
4170         validateFields(currentResource, updateInfoResource, inTransaction, isNested);
4171         warnNonEditableFields(currentResource, updateInfoResource);
4172     }
4173
4174     private void warnNonEditableFields(Resource currentResource, Resource updateInfoResource) {
4175         String currentResourceVersion = currentResource.getVersion();
4176         String updatedResourceVersion = updateInfoResource.getVersion();
4177         if ((updatedResourceVersion != null) && (!updatedResourceVersion.equals(currentResourceVersion))) {
4178             log.debug("Resource version is automatically set and cannot be updated");
4179         }
4180         String currentCreatorUserId = currentResource.getCreatorUserId();
4181         String updatedCreatorUserId = updateInfoResource.getCreatorUserId();
4182         if ((updatedCreatorUserId != null) && (!updatedCreatorUserId.equals(currentCreatorUserId))) {
4183             log.debug("Resource Creator UserId is automatically set and cannot be updated");
4184         }
4185         String currentCreatorFullName = currentResource.getCreatorFullName();
4186         String updatedCreatorFullName = updateInfoResource.getCreatorFullName();
4187         if ((updatedCreatorFullName != null) && (!updatedCreatorFullName.equals(currentCreatorFullName))) {
4188             log.debug("Resource Creator fullname is automatically set and cannot be updated");
4189         }
4190         String currentLastUpdaterUserId = currentResource.getLastUpdaterUserId();
4191         String updatedLastUpdaterUserId = updateInfoResource.getLastUpdaterUserId();
4192         if ((updatedLastUpdaterUserId != null) && (!updatedLastUpdaterUserId.equals(currentLastUpdaterUserId))) {
4193             log.debug("Resource LastUpdater userId is automatically set and cannot be updated");
4194         }
4195         String currentLastUpdaterFullName = currentResource.getLastUpdaterFullName();
4196         String updatedLastUpdaterFullName = updateInfoResource.getLastUpdaterFullName();
4197         if ((updatedLastUpdaterFullName != null) && (!updatedLastUpdaterFullName.equals(currentLastUpdaterFullName))) {
4198             log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
4199         }
4200         Long currentCreationDate = currentResource.getCreationDate();
4201         Long updatedCreationDate = updateInfoResource.getCreationDate();
4202         if ((updatedCreationDate != null) && (!updatedCreationDate.equals(currentCreationDate))) {
4203             log.debug("Resource Creation date is automatically set and cannot be updated");
4204         }
4205         Long currentLastUpdateDate = currentResource.getLastUpdateDate();
4206         Long updatedLastUpdateDate = updateInfoResource.getLastUpdateDate();
4207         if ((updatedLastUpdateDate != null) && (!updatedLastUpdateDate.equals(currentLastUpdateDate))) {
4208             log.debug("Resource last update date is automatically set and cannot be updated");
4209         }
4210         LifecycleStateEnum currentLifecycleState = currentResource.getLifecycleState();
4211         LifecycleStateEnum updatedLifecycleState = updateInfoResource.getLifecycleState();
4212         if ((updatedLifecycleState != null) && (!updatedLifecycleState.equals(currentLifecycleState))) {
4213             log.debug("Resource lifecycle state date is automatically set and cannot be updated");
4214         }
4215         Boolean currentAbstract = currentResource.isAbstract();
4216         Boolean updatedAbstract = updateInfoResource.isAbstract();
4217         if ((updatedAbstract != null) && (!updatedAbstract.equals(currentAbstract))) {
4218             log.debug("Resource abstract is automatically set and cannot be updated");
4219         }
4220         Boolean currentHighestVersion = currentResource.isHighestVersion();
4221         Boolean updatedHighestVersion = updateInfoResource.isHighestVersion();
4222         if ((updatedHighestVersion != null) && (!updatedHighestVersion.equals(currentHighestVersion))) {
4223             log.debug("Resource highest version is automatically set and cannot be updated");
4224         }
4225         String currentUuid = currentResource.getUUID();
4226         String updatedUuid = updateInfoResource.getUUID();
4227         if ((updatedUuid != null) && (!updatedUuid.equals(currentUuid))) {
4228             log.debug("Resource UUID is automatically set and cannot be updated");
4229         }
4230         log.debug("Resource Type  cannot be updated");
4231         String currentInvariantUuid = currentResource.getInvariantUUID();
4232         String updatedInvariantUuid = updateInfoResource.getInvariantUUID();
4233         if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) {
4234             log.debug("Resource invariant UUID is automatically set and cannot be updated");
4235             updateInfoResource.setInvariantUUID(currentInvariantUuid);
4236         }
4237     }
4238
4239     private void validateFields(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
4240         boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentResource.getVersion());
4241         log.debug("validate resource name before update");
4242         validateResourceName(currentResource, updateInfoResource, hasBeenCertified, isNested);
4243         log.debug("validate description before update");
4244         componentDescriptionValidator.validateAndCorrectField(null, updateInfoResource, null);
4245         log.debug("validate icon before update");
4246         validateIcon(currentResource, updateInfoResource, hasBeenCertified);
4247         log.debug("validate tags before update");
4248         componentTagsValidator.validateAndCorrectField(null, updateInfoResource, null);
4249         log.debug("validate vendor name before update");
4250         validateVendorName(null, updateInfoResource, null);
4251         log.debug("validate resource vendor model number before update");
4252         validateResourceVendorModelNumber(currentResource, updateInfoResource);
4253         log.debug("validate vendor release before update");
4254         validateVendorReleaseName(null, updateInfoResource, null);
4255         log.debug("validate contact info before update");
4256         componentContactIdValidator.validateAndCorrectField(null, updateInfoResource, null);
4257         log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
4258         validateDerivedFromDuringUpdate(currentResource, updateInfoResource, hasBeenCertified);
4259         log.debug("validate category before update");
4260         validateCategory(currentResource, updateInfoResource, hasBeenCertified, inTransaction);
4261     }
4262
4263     private boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) {
4264         String resourceNameUpdated = updateInfoResource.getName();
4265         String resourceNameCurrent = currentResource.getName();
4266         if (resourceNameCurrent.equals(resourceNameUpdated)) {
4267             return true;
4268         }
4269         // In case of CVFC type we should support the case of old VF with CVFC
4270
4271         // instances that were created without the "Cvfc" suffix
4272         return currentResource.getResourceType() == ResourceTypeEnum.CVFC && resourceNameUpdated
4273             .equals(addCvfcSuffixToResourceName(resourceNameCurrent));
4274     }
4275
4276     private String addCvfcSuffixToResourceName(String resourceName) {
4277         return resourceName + "Cvfc";
4278     }
4279
4280     private void validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, boolean isNested) {
4281         String resourceNameUpdated = updateInfoResource.getName();
4282         if (!isResourceNameEquals(currentResource, updateInfoResource)) {
4283             if (isNested || !hasBeenCertified) {
4284                 componentNameValidator.validateAndCorrectField(null, updateInfoResource, null);
4285                 validateResourceNameUniqueness(updateInfoResource);
4286                 currentResource.setName(resourceNameUpdated);
4287                 currentResource.setNormalizedName(ValidationUtils.normaliseComponentName(resourceNameUpdated));
4288                 currentResource.setSystemName(ValidationUtils.convertToSystemName(resourceNameUpdated));
4289             } else {
4290                 log.info("Resource name: {}, cannot be updated once the resource has been certified once.", resourceNameUpdated);
4291                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
4292             }
4293         }
4294     }
4295
4296     private void validateIcon(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) {
4297         String iconUpdated = updateInfoResource.getIcon();
4298         String iconCurrent = currentResource.getIcon();
4299         if (!iconCurrent.equals(iconUpdated)) {
4300             if (!hasBeenCertified) {
4301                 componentIconValidator.validateAndCorrectField(null, updateInfoResource, null);
4302             } else {
4303                 log.info("Icon {} cannot be updated once the resource has been certified once.", iconUpdated);
4304                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
4305             }
4306         }
4307     }
4308
4309     private void validateResourceVendorModelNumber(Resource currentResource, Resource updateInfoResource) {
4310         String updatedResourceVendorModelNumber = updateInfoResource.getResourceVendorModelNumber();
4311         String currentResourceVendorModelNumber = currentResource.getResourceVendorModelNumber();
4312         if (!currentResourceVendorModelNumber.equals(updatedResourceVendorModelNumber)) {
4313             validateResourceVendorModelNumber(null, updateInfoResource, null);
4314         }
4315     }
4316
4317     private Either<Boolean, ResponseFormat> validateCategory(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified,
4318                                                              boolean inTransaction) {
4319         validateCategory(null, updateInfoResource, null, inTransaction);
4320         if (hasBeenCertified) {
4321             CategoryDefinition currentCategory = currentResource.getCategories().get(0);
4322             SubCategoryDefinition currentSubCategory = currentCategory.getSubcategories().get(0);
4323             CategoryDefinition updateCategory = updateInfoResource.getCategories().get(0);
4324             SubCategoryDefinition updtaeSubCategory = updateCategory.getSubcategories().get(0);
4325             if (!currentCategory.getName().equals(updateCategory.getName()) || !currentSubCategory.getName().equals(updtaeSubCategory.getName())) {
4326                 log.info("Category {} cannot be updated once the resource has been certified once.", currentResource.getCategories());
4327                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
4328                 return Either.right(errorResponse);
4329             }
4330         }
4331         return Either.left(true);
4332     }
4333
4334     private Either<Boolean, ResponseFormat> validateDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
4335                                                                             boolean hasBeenCertified) {
4336         List<String> currentDerivedFrom = currentResource.getDerivedFrom();
4337         List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
4338         if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
4339             log.trace("Update normative types");
4340             return Either.left(true);
4341         }
4342         String derivedFromCurrent = currentDerivedFrom.get(0);
4343         String derivedFromUpdated = updatedDerivedFrom.get(0);
4344         if (!derivedFromCurrent.equals(derivedFromUpdated)) {
4345             if (!hasBeenCertified) {
4346                 validateDerivedFromExist(null, updateInfoResource, null);
4347             } else {
4348                 Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
4349                     null);
4350                 if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
4351                     log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
4352                     return validateDerivedFromExtending;
4353                 }
4354             }
4355         } else {
4356             // For derived from, we must know whether it was actually changed,
4357
4358             // otherwise we must do no action.
4359
4360             // Due to changes it inflicts on data model (remove artifacts,
4361
4362             // properties...), it's not like a flat field which can be
4363
4364             // overwritten if not changed.
4365
4366             // So we must indicate that derived from is not changed
4367             updateInfoResource.setDerivedFrom(null);
4368         }
4369         return Either.left(true);
4370     }
4371
4372     private Either<Boolean, ResponseFormat> validateNestedDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
4373                                                                                   boolean hasBeenCertified) {
4374         List<String> currentDerivedFrom = currentResource.getDerivedFrom();
4375         List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
4376         if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
4377             log.trace("Update normative types");
4378             return Either.left(true);
4379         }
4380         String derivedFromCurrent = currentDerivedFrom.get(0);
4381         String derivedFromUpdated = updatedDerivedFrom.get(0);
4382         if (!derivedFromCurrent.equals(derivedFromUpdated)) {
4383             if (!hasBeenCertified) {
4384                 validateDerivedFromExist(null, updateInfoResource, null);
4385             } else {
4386                 Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
4387                     null);
4388                 if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
4389                     log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
4390                     return validateDerivedFromExtending;
4391                 }
4392             }
4393         }
4394         return Either.left(true);
4395     }
4396
4397     private void validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) {
4398         if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) {
4399             return;
4400         }
4401         String templateName = resource.getDerivedFrom().get(0);
4402         Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade.validateToscaResourceNameExists(templateName);
4403         if (dataModelResponse.isRight()) {
4404             StorageOperationStatus storageStatus = dataModelResponse.right().value();
4405             BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateDerivedFromExist");
4406             log.debug("request to data model failed with error: {}", storageStatus);
4407             ResponseFormat responseFormat = componentsUtils
4408                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), resource);
4409             log.trace("audit before sending response");
4410             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4411             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageStatus));
4412         } else if (!dataModelResponse.left().value()) {
4413             log.info("resource template with name: {}, does not exists", templateName);
4414             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
4415             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4416             throw new ByActionStatusComponentException(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
4417         }
4418     }
4419
4420     // Tal G for extending inheritance US815447
4421     private Either<Boolean, ResponseFormat> validateDerivedFromExtending(User user, Resource currentResource, Resource updateInfoResource,
4422                                                                          AuditingActionEnum actionEnum) {
4423         String currentTemplateName = currentResource.getDerivedFrom().get(0);
4424         String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0);
4425         Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
4426             .validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName);
4427         if (dataModelResponse.isRight()) {
4428             StorageOperationStatus storageStatus = dataModelResponse.right().value();
4429             BeEcompErrorManager.getInstance().logBeDaoSystemError("Create/Update Resource - validateDerivingFromExtendingType");
4430             ResponseFormat responseFormat = componentsUtils
4431                 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), currentResource);
4432             log.trace("audit before sending response");
4433             componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
4434             return Either.right(responseFormat);
4435         }
4436         if (!dataModelResponse.left().value()) {
4437             log.info("resource template with name {} does not inherit as original {}", updatedTemplateName, currentTemplateName);
4438             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
4439             componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
4440             return Either.right(responseFormat);
4441         }
4442         return Either.left(true);
4443     }
4444
4445     public void validateDerivedFromNotEmpty(User user, Resource resource, AuditingActionEnum actionEnum) {
4446         log.debug("validate resource derivedFrom field");
4447         if ((resource.getDerivedFrom() == null) || (resource.getDerivedFrom().isEmpty()) || (resource.getDerivedFrom().get(0)) == null || (resource
4448             .getDerivedFrom().get(0).trim().isEmpty())) {
4449             log.info("derived from (template) field is missing for the resource");
4450             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
4451             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4452             throw new ByActionStatusComponentException(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
4453         }
4454     }
4455
4456     private void validateResourceNameUniqueness(Resource resource) {
4457         Either<Boolean, StorageOperationStatus> resourceOperationResponse = toscaOperationFacade
4458             .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
4459         if (resourceOperationResponse.isLeft() && resourceOperationResponse.left().value()) {
4460             log.debug("resource with name: {}, already exists", resource.getName());
4461             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
4462                 resource.getName());
4463         } else if (resourceOperationResponse.isRight()) {
4464             log.debug("error while validateResourceNameExists for resource: {}", resource.getName());
4465             throw new StorageException(resourceOperationResponse.right().value());
4466         }
4467     }
4468
4469     private void validateCategory(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
4470         List<CategoryDefinition> categories = resource.getCategories();
4471         if (CollectionUtils.isEmpty(categories)) {
4472             log.debug(CATEGORY_IS_EMPTY);
4473             ResponseFormat responseFormat = componentsUtils
4474                 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4475             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4476             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4477         }
4478         if (categories.size() > 1) {
4479             log.debug("Must be only one category for resource");
4480             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue());
4481         }
4482         CategoryDefinition category = categories.get(0);
4483         List<SubCategoryDefinition> subcategories = category.getSubcategories();
4484         if (CollectionUtils.isEmpty(subcategories)) {
4485             log.debug("Missinig subcategory for resource");
4486             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY);
4487         }
4488         if (subcategories.size() > 1) {
4489             log.debug("Must be only one sub category for resource");
4490             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES);
4491         }
4492         SubCategoryDefinition subcategory = subcategories.get(0);
4493         if (!ValidationUtils.validateStringNotEmpty(category.getName())) {
4494             log.debug(CATEGORY_IS_EMPTY);
4495             ResponseFormat responseFormat = componentsUtils
4496                 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4497             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4498             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4499         }
4500         if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) {
4501             log.debug(CATEGORY_IS_EMPTY);
4502             ResponseFormat responseFormat = componentsUtils
4503                 .getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4504             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4505             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4506         }
4507         validateCategoryListed(category, subcategory, user, resource, actionEnum, inTransaction);
4508     }
4509
4510     private void validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, User user, Resource resource,
4511                                         AuditingActionEnum actionEnum, boolean inTransaction) {
4512         ResponseFormat responseFormat;
4513         if (category != null && subcategory != null) {
4514             log.debug("validating resource category {} against valid categories list", category);
4515             Either<List<CategoryDefinition>, ActionStatus> categories = elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction);
4516             if (categories.isRight()) {
4517                 log.debug("failed to retrieve resource categories from JanusGraph");
4518                 responseFormat = componentsUtils.getResponseFormat(categories.right().value());
4519                 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4520                 throw new ByActionStatusComponentException(categories.right().value());
4521             }
4522             List<CategoryDefinition> categoryList = categories.left().value();
4523             Optional<CategoryDefinition> foundCategory = categoryList.stream().filter(cat -> cat.getName().equals(category.getName())).findFirst();
4524             if (foundCategory.isEmpty()) {
4525                 log.debug("Category {} is not part of resource category group. Resource category valid values are {}", category, categoryList);
4526                 failOnInvalidCategory(user, resource, actionEnum);
4527                 return; // explisite output even if failOnInvalidCategory throw an exception
4528             }
4529             Optional<SubCategoryDefinition> foundSubcategory = foundCategory.get().getSubcategories().stream()
4530                 .filter(subcat -> subcat.getName().equals(subcategory.getName())).findFirst();
4531             if (foundSubcategory.isEmpty()) {
4532                 log.debug("SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", subcategory,
4533                     foundCategory.get().getSubcategories());
4534                 failOnInvalidCategory(user, resource, actionEnum);
4535             }
4536         }
4537     }
4538
4539     private void failOnInvalidCategory(User user, Resource resource, AuditingActionEnum actionEnum) {
4540         ResponseFormat responseFormat;
4541         responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4542         componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4543         throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4544     }
4545
4546     public void validateVendorReleaseName(User user, Resource resource, AuditingActionEnum actionEnum) {
4547         String vendorRelease = resource.getVendorRelease();
4548         log.debug("validate vendor relese name");
4549         if (!ValidationUtils.validateStringNotEmpty(vendorRelease)) {
4550             log.info("vendor relese name is missing.");
4551             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_RELEASE);
4552             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4553             throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_RELEASE);
4554         }
4555         validateVendorReleaseName(vendorRelease, user, resource, actionEnum);
4556     }
4557
4558     public void validateVendorReleaseName(String vendorRelease, User user, Resource resource, AuditingActionEnum actionEnum) {
4559         if (vendorRelease != null) {
4560             if (!ValidationUtils.validateVendorReleaseLength(vendorRelease)) {
4561                 log.info("vendor release exceds limit.");
4562                 ResponseFormat errorResponse = componentsUtils
4563                     .getResponseFormat(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
4564                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4565                 throw new ByActionStatusComponentException(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
4566             }
4567             if (!ValidationUtils.validateVendorRelease(vendorRelease)) {
4568                 log.info("vendor release  is not valid.");
4569                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_RELEASE);
4570                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4571                 throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_RELEASE, vendorRelease);
4572             }
4573         }
4574     }
4575
4576     private void validateVendorName(User user, Resource resource, AuditingActionEnum actionEnum) {
4577         String vendorName = resource.getVendorName();
4578         if (!ValidationUtils.validateStringNotEmpty(vendorName)) {
4579             log.info("vendor name is missing.");
4580             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_NAME);
4581             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4582             throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_NAME);
4583         }
4584         validateVendorName(vendorName, user, resource, actionEnum);
4585     }
4586
4587     private void validateVendorName(String vendorName, User user, Resource resource, AuditingActionEnum actionEnum) {
4588         if (vendorName != null) {
4589             if (!ValidationUtils.validateVendorNameLength(vendorName)) {
4590                 log.info("vendor name exceds limit.");
4591                 ResponseFormat errorResponse = componentsUtils
4592                     .getResponseFormat(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
4593                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4594                 throw new ByActionStatusComponentException(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
4595             }
4596             if (!ValidationUtils.validateVendorName(vendorName)) {
4597                 log.info("vendor name  is not valid.");
4598                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_NAME);
4599                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4600                 throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_NAME, vendorName);
4601             }
4602         }
4603     }
4604
4605     private void validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) {
4606         String resourceVendorModelNumber = resource.getResourceVendorModelNumber();
4607         if (StringUtils.isNotEmpty(resourceVendorModelNumber)) {
4608             if (!ValidationUtils.validateResourceVendorModelNumberLength(resourceVendorModelNumber)) {
4609                 log.info("resource vendor model number exceeds limit.");
4610                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
4611                     "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
4612                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4613                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
4614                     "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
4615             }
4616             // resource vendor model number is currently validated as vendor
4617
4618             // name
4619             if (!ValidationUtils.validateVendorName(resourceVendorModelNumber)) {
4620                 log.info("resource vendor model number  is not valid.");
4621                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
4622                 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4623                 throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
4624             }
4625         }
4626     }
4627
4628     private void validateCost(Resource resource) {
4629         String cost = resource.getCost();
4630         if (cost != null) {
4631             if (!ValidationUtils.validateCost(cost)) {
4632                 log.debug("resource cost is invalid.");
4633                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
4634             }
4635         }
4636     }
4637
4638     private void validateLicenseType(User user, Resource resource, AuditingActionEnum actionEnum) {
4639         log.debug("validate licenseType");
4640         String licenseType = resource.getLicenseType();
4641         if (licenseType != null) {
4642             List<String> licenseTypes = ConfigurationManager.getConfigurationManager().getConfiguration().getLicenseTypes();
4643             if (!licenseTypes.contains(licenseType)) {
4644                 log.debug("License type {} isn't configured", licenseType);
4645                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
4646                 if (actionEnum != null) {
4647                     // In update case, no audit is required
4648                     componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4649                 }
4650                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
4651             }
4652         }
4653     }
4654
4655     private Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom(Resource currentResource, Resource updatedResource, String userId,
4656                                                                        boolean inTransaction) {
4657         if (updatedResource.getDerivedFrom() != null) {
4658             log.debug("Starting derived from update for resource {}", updatedResource.getUniqueId());
4659             log.debug("1. Removing interface artifacts from graph");
4660             // Remove all interface artifacts of resource
4661             String resourceId = updatedResource.getUniqueId();
4662             Map<String, InterfaceDefinition> interfaces = currentResource.getInterfaces();
4663             if (interfaces != null) {
4664                 Collection<InterfaceDefinition> values = interfaces.values();
4665                 for (InterfaceDefinition interfaceDefinition : values) {
4666                     String interfaceType = interfaceTypeOperation.getShortInterfaceName(interfaceDefinition);
4667                     log.trace("Starting interface artifacts removal for interface type {}", interfaceType);
4668                     Map<String, Operation> operations = interfaceDefinition.getOperationsMap();
4669                     if (operations != null) {
4670                         for (Entry<String, Operation> operationEntry : operations.entrySet()) {
4671                             Operation operation = operationEntry.getValue();
4672                             ArtifactDefinition implementation = operation.getImplementationArtifact();
4673                             if (implementation != null) {
4674                                 String uniqueId = implementation.getUniqueId();
4675                                 log.debug("Removing interface artifact definition {}, operation {}, interfaceType {}", uniqueId,
4676                                     operationEntry.getKey(), interfaceType);
4677                                 // only thing that transacts and locks here
4678                                 Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface = artifactsBusinessLogic
4679                                     .deleteArtifactByInterface(resourceId, userId, uniqueId, true);
4680                                 if (deleteArtifactByInterface.isRight()) {
4681                                     log.debug("Couldn't remove artifact definition with id {}", uniqueId);
4682                                     if (!inTransaction) {
4683                                         janusGraphDao.rollback();
4684                                     }
4685                                     return Either.right(deleteArtifactByInterface.right().value());
4686                                 }
4687                             } else {
4688                                 log.trace("No implementation found for operation {} - nothing to delete", operationEntry.getKey());
4689                             }
4690                         }
4691                     } else {
4692                         log.trace("No operations found for interface type {}", interfaceType);
4693                     }
4694                 }
4695             }
4696             log.debug("2. Removing properties");
4697             Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = propertyOperation
4698                 .deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, resourceId);
4699             if (findPropertiesOfNode.isRight() && findPropertiesOfNode.right().value() != StorageOperationStatus.OK) {
4700                 log.debug("Failed to remove all properties of resource");
4701                 if (!inTransaction) {
4702                     janusGraphDao.rollback();
4703                 }
4704                 return Either
4705                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findPropertiesOfNode.right().value())));
4706             }
4707         } else {
4708             log.debug("Derived from wasn't changed during update");
4709         }
4710         if (inTransaction) {
4711             return Either.left(true);
4712         }
4713         janusGraphDao.commit();
4714         return Either.left(true);
4715     }
4716
4717     public ICapabilityTypeOperation getCapabilityTypeOperation() {
4718         return capabilityTypeOperation;
4719     }
4720
4721     @Autowired
4722     public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) {
4723         this.capabilityTypeOperation = capabilityTypeOperation;
4724     }
4725
4726     public Boolean validatePropertiesDefaultValues(Resource resource) {
4727         log.debug("validate resource properties default values");
4728         List<PropertyDefinition> properties = resource.getProperties();
4729         if (properties != null) {
4730             iterateOverProperties(properties);
4731         }
4732         return true;
4733     }
4734
4735     public void iterateOverProperties(List<PropertyDefinition> properties) {
4736         String type = null;
4737         String innerType = null;
4738         for (PropertyDefinition property : properties) {
4739             if (!propertyOperation.isPropertyTypeValid(property, null)) {
4740                 log.info("Invalid type for property {}", property);
4741                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
4742             }
4743             Map<String, DataTypeDefinition> allDataTypes = getAllDataTypes(applicationDataTypeCache);
4744             type = property.getType();
4745             if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
4746                 ResponseFormat responseFormat = validateMapOrListPropertyType(property, innerType, allDataTypes);
4747                 if (responseFormat != null) {
4748                     break;
4749                 }
4750             }
4751             validateDefaultPropertyValue(property, allDataTypes, type, innerType);
4752         }
4753     }
4754
4755     private void validateDefaultPropertyValue(PropertyDefinition property, Map<String, DataTypeDefinition> allDataTypes, String type,
4756                                               String innerType) {
4757         if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes)) {
4758             log.info("Invalid default value for property {}", property);
4759             ResponseFormat responseFormat;
4760             if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
4761                 throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType,
4762                     property.getDefaultValue());
4763             }
4764             throw new ByActionStatusComponentException(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue());
4765         }
4766     }
4767
4768     private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, String innerType,
4769                                                          Map<String, DataTypeDefinition> allDataTypes) {
4770         ResponseFormat responseFormat = null;
4771         ImmutablePair<String, Boolean> propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes);
4772         innerType = propertyInnerTypeValid.getLeft();
4773         if (!propertyInnerTypeValid.getRight()) {
4774             log.info("Invalid inner type for property {}", property);
4775             responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName());
4776         }
4777         return responseFormat;
4778     }
4779
4780     @Override
4781     public Either<List<String>, ResponseFormat> deleteMarkedComponents() {
4782         return deleteMarkedComponents(ComponentTypeEnum.RESOURCE);
4783     }
4784
4785     @Override
4786     public ComponentInstanceBusinessLogic getComponentInstanceBL() {
4787         return componentInstanceBusinessLogic;
4788     }
4789
4790     private String getComponentTypeForResponse(Component component) {
4791         String componentTypeForResponse = "SERVICE";
4792         if (component instanceof Resource) {
4793             componentTypeForResponse = ((Resource) component).getResourceType().name();
4794         }
4795         return componentTypeForResponse;
4796     }
4797
4798     public Either<Resource, ResponseFormat> getLatestResourceFromCsarUuid(String csarUuid, User user) {
4799         // validate user
4800         if (user != null) {
4801             validateUserExists(user);
4802         }
4803         // get resource from csar uuid
4804         Either<Resource, StorageOperationStatus> either = toscaOperationFacade
4805             .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUuid, "");
4806         if (either.isRight()) {
4807             ResponseFormat resp = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_FROM_CSAR_NOT_FOUND, csarUuid);
4808             return Either.right(resp);
4809         }
4810         return Either.left(either.left().value());
4811     }
4812
4813     @Override
4814     public Either<List<ComponentInstance>, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) {
4815         return null;
4816     }
4817
4818     private Map<String, List<CapabilityDefinition>> getValidComponentInstanceCapabilities(String resourceId,
4819                                                                                           Map<String, List<CapabilityDefinition>> defaultCapabilities,
4820                                                                                           Map<String, List<UploadCapInfo>> uploadedCapabilities) {
4821         Map<String, List<CapabilityDefinition>> validCapabilitiesMap = new HashMap<>();
4822         uploadedCapabilities.forEach((k, v) -> addValidComponentInstanceCapabilities(k, v, resourceId, defaultCapabilities, validCapabilitiesMap));
4823         return validCapabilitiesMap;
4824     }
4825
4826     private void addValidComponentInstanceCapabilities(String key, List<UploadCapInfo> capabilities, String resourceId,
4827                                                        Map<String, List<CapabilityDefinition>> defaultCapabilities,
4828                                                        Map<String, List<CapabilityDefinition>> validCapabilitiesMap) {
4829         String capabilityType = capabilities.get(0).getType();
4830         if (defaultCapabilities.containsKey(capabilityType)) {
4831             CapabilityDefinition defaultCapability = getCapability(resourceId, defaultCapabilities, capabilityType);
4832             validateCapabilityProperties(capabilities, resourceId, defaultCapability);
4833             List<CapabilityDefinition> validCapabilityList = new ArrayList<>();
4834             validCapabilityList.add(defaultCapability);
4835             validCapabilitiesMap.put(key, validCapabilityList);
4836         } else {
4837             throw new ByActionStatusComponentException(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType);
4838         }
4839     }
4840
4841     private void validateCapabilityProperties(List<UploadCapInfo> capabilities, String resourceId, CapabilityDefinition defaultCapability) {
4842         if (CollectionUtils.isEmpty(defaultCapability.getProperties()) && isNotEmpty(capabilities.get(0).getProperties())) {
4843             log.debug("Failed to validate capability {} of component {}. Property list is empty. ", defaultCapability.getName(), resourceId);
4844             log.debug("Failed to update capability property values. Property list of fetched capability {} is empty. ", defaultCapability.getName());
4845             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, resourceId);
4846         } else if (isNotEmpty(capabilities.get(0).getProperties())) {
4847             validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, capabilities.get(0));
4848         }
4849     }
4850
4851     private CapabilityDefinition getCapability(String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities,
4852                                                String capabilityType) {
4853         CapabilityDefinition defaultCapability;
4854         if (isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())) {
4855             defaultCapability = defaultCapabilities.get(capabilityType).get(0);
4856         } else {
4857             Either<Component, StorageOperationStatus> getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId);
4858             if (getFullComponentRes.isRight()) {
4859                 log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value());
4860                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, resourceId);
4861             }
4862             defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0);
4863         }
4864         return defaultCapability;
4865     }
4866
4867     private void validateUniquenessUpdateUploadedComponentInstanceCapability(CapabilityDefinition defaultCapability,
4868                                                                              UploadCapInfo uploadedCapability) {
4869         List<ComponentInstanceProperty> validProperties = new ArrayList<>();
4870         Map<String, PropertyDefinition> defaultProperties = defaultCapability.getProperties().stream()
4871             .collect(toMap(PropertyDefinition::getName, Function.identity()));
4872         List<UploadPropInfo> uploadedProperties = uploadedCapability.getProperties();
4873         for (UploadPropInfo property : uploadedProperties) {
4874             String propertyName = property.getName().toLowerCase();
4875             String propertyType = property.getType();
4876             ComponentInstanceProperty validProperty;
4877             if (defaultProperties.containsKey(propertyName) && propertTypeEqualsTo(defaultProperties, propertyName, propertyType)) {
4878                 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, propertyName);
4879             }
4880             validProperty = new ComponentInstanceProperty();
4881             validProperty.setName(propertyName);
4882             if (property.getValue() != null) {
4883                 validProperty.setValue(property.getValue().toString());
4884             }
4885             validProperty.setDescription(property.getDescription());
4886             validProperty.setPassword(property.isPassword());
4887             validProperties.add(validProperty);
4888         }
4889         defaultCapability.setProperties(validProperties);
4890     }
4891
4892     private boolean propertTypeEqualsTo(Map<String, PropertyDefinition> defaultProperties, String propertyName, String propertyType) {
4893         return propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType);
4894     }
4895
4896     private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
4897         List<NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, Resource resource, User user) {
4898         EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
4899         Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
4900         Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
4901             .left(nodeTypeArtifactsToHandle);
4902         try {
4903             // add all found Csar artifacts to list to upload
4904             List<NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
4905             List<NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
4906             List<NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
4907             for (NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
4908                 ArtifactDefinition foundArtifact;
4909                 if (!existingArtifactsToHandle.isEmpty()) {
4910                     foundArtifact = existingArtifactsToHandle.stream().filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName()))
4911                         .findFirst().orElse(null);
4912                     if (foundArtifact != null) {
4913                         if (foundArtifact.getArtifactType().equals(currNewArtifact.getArtifactType())) {
4914                             if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
4915                                 currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
4916                                 // if current artifact already exists, but has
4917
4918                                 // different content, add him to the list to
4919
4920                                 // update
4921                                 artifactsToUpdate.add(currNewArtifact);
4922                             }
4923                             // remove found artifact from the list of existing
4924
4925                             // artifacts to handle, because it was already
4926
4927                             // handled
4928                             existingArtifactsToHandle.remove(foundArtifact);
4929                             // and remove found artifact from the list to
4930
4931                             // upload, because it should either be updated or be
4932
4933                             // ignored
4934                             artifactsToUpload.remove(currNewArtifact);
4935                         } else {
4936                             log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
4937                             ResponseFormat responseFormat = ResponseFormatManager.getInstance()
4938                                 .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(),
4939                                     currNewArtifact.getArtifactType(), foundArtifact.getArtifactType());
4940                             AuditingActionEnum auditingAction = artifactsBusinessLogic
4941                                 .detectAuditingType(new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE),
4942                                     foundArtifact.getArtifactChecksum());
4943                             artifactsBusinessLogic
4944                                 .handleAuditing(auditingAction, resource, resource.getUniqueId(), user, null, null, foundArtifact.getUniqueId(),
4945                                     responseFormat, resource.getComponentType(), null);
4946                             responseWrapper.setInnerElement(responseFormat);
4947                             break;
4948                         }
4949                     }
4950                 }
4951             }
4952             if (responseWrapper.isEmpty()) {
4953                 for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
4954                     if (currArtifact.getIsFromCsar()) {
4955                         artifactsToDelete.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
4956                             currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
4957                     } else {
4958                         artifactsToUpdate.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
4959                             currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
4960                     }
4961                 }
4962             }
4963             if (responseWrapper.isEmpty()) {
4964                 if (!artifactsToUpload.isEmpty()) {
4965                     nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
4966                 }
4967                 if (!artifactsToUpdate.isEmpty()) {
4968                     nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
4969                 }
4970                 if (!artifactsToDelete.isEmpty()) {
4971                     nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
4972                 }
4973             }
4974             if (!responseWrapper.isEmpty()) {
4975                 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
4976             }
4977         } catch (Exception e) {
4978             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
4979             responseWrapper.setInnerElement(responseFormat);
4980             log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
4981         }
4982         return nodeTypeArtifactsToHandleRes;
4983     }
4984
4985     ImmutablePair<String, String> buildNestedToscaResourceName(final String nodeResourceType, final String vfResourceName,
4986                                                                final String nodeTypeFullName) {
4987         String actualType;
4988         String actualVfName;
4989         if (ResourceTypeEnum.CVFC.name().equals(nodeResourceType)) {
4990             actualVfName = vfResourceName + ResourceTypeEnum.CVFC.name();
4991             actualType = ResourceTypeEnum.VFC.name();
4992         } else {
4993             actualVfName = vfResourceName;
4994             actualType = nodeResourceType;
4995         }
4996         String nameWithouNamespacePrefix;
4997         try {
4998             final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeTypeFullName);
4999             log.debug("####### buildNestedToscaResourceName nodeResourceType {}, vfResourceName {}, "
5000                     + "nodeTypeFullName {}, actualType {}, vfResourceName {} ", nodeResourceType, vfResourceName, nodeTypeFullName, actualType,
5001                 vfResourceName);
5002             final StringBuilder toscaResourceName = new StringBuilder(nodeTypeNamePrefix);
5003             if (!nodeTypeFullName.contains(nodeTypeNamePrefix)) {
5004                 nameWithouNamespacePrefix = nodeTypeFullName;
5005             } else {
5006                 nameWithouNamespacePrefix = nodeTypeFullName.substring(nodeTypeNamePrefix.length());
5007             }
5008             final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
5009             String actualName;
5010             if (nodeResourceType.equalsIgnoreCase(findTypes[0])) {
5011                 actualName = nameWithouNamespacePrefix.substring(nodeResourceType.length());
5012             } else {
5013                 actualName = "." + nameWithouNamespacePrefix;
5014             }
5015             if (actualName.startsWith(Constants.ABSTRACT)) {
5016                 toscaResourceName.append(nodeResourceType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName));
5017             } else {
5018                 toscaResourceName.append(actualType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)).append('.')
5019                     .append(Constants.ABSTRACT);
5020             }
5021             final StringBuilder previousToscaResourceName = new StringBuilder(toscaResourceName);
5022             final String[] actualNames = actualName.split("\\.");
5023             if (actualNames.length < 3) {
5024                 return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
5025                     previousToscaResourceName.append(actualName).toString());
5026             }
5027             return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
5028                 previousToscaResourceName.append(actualName.substring(actualNames[1].length() + 1).toLowerCase()).toString());
5029         } catch (final Exception e) {
5030             log.debug("Exception occured when buildNestedToscaResourceName, error is:{}", e.getMessage(), e);
5031             throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfResourceName);
5032         }
5033     }
5034
5035     /**
5036      * Extracts a Node Type Name prefix from the given Node Type Name.
5037      *
5038      * @param fullName Node Type Name
5039      * @return Node Type Name Prefix
5040      */
5041     private String getNodeTypeNamePrefix(final String fullName) {
5042         String tempPrefix = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX;
5043         final List<String> definedNodeTypeNamespaceList = getDefinedNodeTypeNamespaceList();
5044         log.debug("************* getPrefiX fullName {} FROM {}", fullName, definedNodeTypeNamespaceList);
5045         final Optional<String> validNameSpace = validateNodeTypeNamePrefix(fullName, definedNodeTypeNamespaceList);
5046         if (validNameSpace.isPresent()) {
5047             tempPrefix = validNameSpace.get();
5048         }
5049         log.debug("************* getNodeTypeNamePrefix return fullName {} ", tempPrefix);
5050         return tempPrefix;
5051     }
5052
5053     @Override
5054     public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String resourceId,
5055                                                                                                    List<String> dataParamsToReturn) {
5056         ComponentParametersView paramsToReturn = new ComponentParametersView(dataParamsToReturn);
5057         Either<Resource, StorageOperationStatus> resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, paramsToReturn);
5058         if (resourceResultEither.isRight()) {
5059             if (resourceResultEither.right().value() == StorageOperationStatus.NOT_FOUND) {
5060                 log.debug("Failed to found resource with id {} ", resourceId);
5061                 Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
5062             }
5063             log.debug("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn);
5064             return Either.right(
5065                 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), ""));
5066         }
5067         Resource resource = resourceResultEither.left().value();
5068         if (dataParamsToReturn.contains(ComponentFieldsEnum.INPUTS.getValue())) {
5069             ListUtils.emptyIfNull(resource.getInputs()).forEach(input -> input.setConstraints(setInputConstraint(input)));
5070         }
5071         UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn);
5072         return Either.left(dataTransfer);
5073     }
5074
5075     @Override
5076     public Either<Component, ActionStatus> shouldUpgradeToLatestDerived(Component clonedComponent) {
5077         Resource resource = (Resource) clonedComponent;
5078         if (ModelConverter.isAtomicComponent(resource.getResourceType())) {
5079             Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived = toscaOperationFacade.shouldUpgradeToLatestDerived(resource);
5080             if (shouldUpgradeToLatestDerived.isRight()) {
5081                 return Either.right(componentsUtils.convertFromStorageResponse(shouldUpgradeToLatestDerived.right().value()));
5082             }
5083             return Either.left(shouldUpgradeToLatestDerived.left().value());
5084         } else {
5085             return super.shouldUpgradeToLatestDerived(clonedComponent);
5086         }
5087     }
5088 }