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