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