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