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