Fix import VFC with attributes
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentBusinessLogic.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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components.impl;
24
25 import fj.data.Either;
26 import io.vavr.Function3;
27 import java.util.Optional;
28 import java.util.function.BiFunction;
29 import java.util.function.BooleanSupplier;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.commons.lang3.tuple.ImmutablePair;
33 import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
35 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
36 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
37 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
38 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
39 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
40 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
41 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
42 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
43 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
44 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
45 import org.openecomp.sdc.be.config.BeEcompErrorManager;
46 import org.openecomp.sdc.be.config.ConfigurationManager;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
49 import org.openecomp.sdc.be.dao.utils.MapUtil;
50 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
51 import org.openecomp.sdc.be.datatypes.components.ServiceMetadataDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
56 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
57 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
58 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
59 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
60 import org.openecomp.sdc.be.impl.ComponentsUtils;
61 import org.openecomp.sdc.be.model.ArtifactDefinition;
62 import org.openecomp.sdc.be.model.CapReqDef;
63 import org.openecomp.sdc.be.model.Component;
64 import org.openecomp.sdc.be.model.ComponentInstance;
65 import org.openecomp.sdc.be.model.ComponentInstanceInput;
66 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
67 import org.openecomp.sdc.be.model.ComponentParametersView;
68 import org.openecomp.sdc.be.model.DataTypeDefinition;
69 import org.openecomp.sdc.be.model.GroupDefinition;
70 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
71 import org.openecomp.sdc.be.model.InputDefinition;
72 import org.openecomp.sdc.be.model.LifecycleStateEnum;
73 import org.openecomp.sdc.be.model.Operation;
74 import org.openecomp.sdc.be.model.PropertyDefinition;
75 import org.openecomp.sdc.be.model.Resource;
76 import org.openecomp.sdc.be.model.User;
77 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
79 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
80 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
81 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
82 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
83 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
84 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
85 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
86 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
87 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
88 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
89 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
90 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
91 import org.openecomp.sdc.be.user.Role;
92 import org.openecomp.sdc.be.utils.CommonBeUtils;
93 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
94 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
95 import org.openecomp.sdc.common.log.wrappers.Logger;
96 import org.openecomp.sdc.common.util.ValidationUtils;
97 import org.openecomp.sdc.exception.ResponseFormat;
98 import org.springframework.beans.factory.annotation.Autowired;
99
100 import java.util.ArrayList;
101 import java.util.HashMap;
102 import java.util.List;
103 import java.util.Map;
104 import java.util.Map.Entry;
105 import java.util.stream.Collectors;
106
107 public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
108
109     protected ArtifactsBusinessLogic artifactsBusinessLogic;
110
111     protected final GroupBusinessLogic groupBusinessLogic;
112
113     protected GenericTypeBusinessLogic genericTypeBusinessLogic;
114
115     protected ComponentDescriptionValidator componentDescriptionValidator;
116     protected ComponentProjectCodeValidator componentProjectCodeValidator;
117
118     protected CatalogOperation catalogOperations;
119     protected ComponentIconValidator componentIconValidator;
120
121     protected ComponentValidator componentValidator;
122     protected ComponentTagsValidator componentTagsValidator;
123     protected ComponentNameValidator componentNameValidator;
124     protected ComponentContactIdValidator componentContactIdValidator;
125
126     public ComponentBusinessLogic(IElementOperation elementDao,
127         IGroupOperation groupOperation,
128         IGroupInstanceOperation groupInstanceOperation,
129         IGroupTypeOperation groupTypeOperation,
130         GroupBusinessLogic groupBusinessLogic,
131         InterfaceOperation interfaceOperation,
132         InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
133         ArtifactsBusinessLogic artifactsBusinessLogic,
134         ArtifactsOperations artifactToscaOperation,
135         ComponentContactIdValidator componentContactIdValidator,
136         ComponentNameValidator componentNameValidator,
137         ComponentTagsValidator componentTagsValidator,
138         ComponentValidator componentValidator,
139         ComponentIconValidator componentIconValidator,
140         ComponentProjectCodeValidator componentProjectCodeValidator,
141         ComponentDescriptionValidator componentDescriptionValidator){
142
143         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
144             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
145         this.artifactsBusinessLogic = artifactsBusinessLogic;
146         this.groupBusinessLogic = groupBusinessLogic;
147         this.componentContactIdValidator = componentContactIdValidator;
148         this.componentNameValidator = componentNameValidator;
149         this.componentTagsValidator = componentTagsValidator;
150         this.componentValidator = componentValidator;
151         this.componentIconValidator = componentIconValidator;
152         this.componentProjectCodeValidator = componentProjectCodeValidator;
153         this.componentDescriptionValidator = componentDescriptionValidator;
154     }
155
156     public void setComponentDescriptionValidator(ComponentDescriptionValidator componentDescriptionValidator) {
157         this.componentDescriptionValidator = componentDescriptionValidator;
158     }
159
160     public void setComponentProjectCodeValidator(ComponentProjectCodeValidator componentProjectCodeValidator) {
161         this.componentProjectCodeValidator = componentProjectCodeValidator;
162     }
163
164     public void setComponentIconValidator(ComponentIconValidator componentIconValidator) {
165         this.componentIconValidator = componentIconValidator;
166     }
167
168     public void setComponentContactIdValidator(ComponentContactIdValidator componentContactIdValidator) {
169         this.componentContactIdValidator = componentContactIdValidator;
170     }
171
172     public void setComponentTagsValidator(ComponentTagsValidator componentTagsValidator) {
173         this.componentTagsValidator = componentTagsValidator;
174     }
175
176     public void setComponentNameValidator(ComponentNameValidator componentNameValidator) {
177         this.componentNameValidator = componentNameValidator;
178     }
179
180
181     @Autowired
182     public void setGenericTypeBusinessLogic(GenericTypeBusinessLogic genericTypeBusinessLogic) {
183         this.genericTypeBusinessLogic = genericTypeBusinessLogic;
184     }
185
186     private static final Logger log = Logger.getLogger(ComponentBusinessLogic.class.getName());
187
188     private static final String TAG_FIELD_LABEL = "tag";
189
190     public abstract Either<List<String>, ResponseFormat> deleteMarkedComponents();
191
192     public abstract ComponentInstanceBusinessLogic getComponentInstanceBL();
193
194     public abstract Either<List<ComponentInstance>, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId);
195
196     /**
197      *
198      * @param componentId
199      * @param dataParamsToReturn
200      * @return
201      */
202     public abstract  Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String componentId, List<String> dataParamsToReturn);
203
204     User validateUser(User user, String ecompErrorContext, Component component, AuditingActionEnum auditAction, boolean inTransaction) {
205         User validatedUser;
206         ResponseFormat responseFormat;
207         try {
208             validateUserNotEmpty(user, ecompErrorContext);
209             validatedUser = validateUserExists(user);
210         } catch(ByActionStatusComponentException e){
211             if(e.getActionStatus() == ActionStatus.MISSING_INFORMATION){
212                 user.setUserId("UNKNOWN");
213             }
214             responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
215             componentsUtils.auditComponentAdmin(responseFormat, user, component, auditAction, component.getComponentType());
216             throw e;
217         } catch(ByResponseFormatComponentException e){
218             responseFormat = e.getResponseFormat();
219             componentsUtils.auditComponentAdmin(responseFormat, user, component, auditAction, component.getComponentType());
220             throw e;
221         }
222         return validatedUser;
223     }
224
225     protected void validateUserRole(User user, Component component, List<Role> roles, AuditingActionEnum auditAction, String comment) {
226         if (roles != null && roles.isEmpty()) {
227             roles.add(Role.ADMIN);
228             roles.add(Role.DESIGNER);
229         }
230         try {
231             validateUserRole(user, roles);
232         }catch (ByActionStatusComponentException e) {
233             ResponseFormat responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
234             handleComponentException(component, comment, responseFormat, user, auditAction);
235             throw e;
236         }catch (ByResponseFormatComponentException e) {
237             ResponseFormat responseFormat = e.getResponseFormat();
238             handleComponentException(component, comment, responseFormat, user, auditAction);
239             throw e;
240         }
241     }
242
243     private void handleComponentException(Component component, String comment, ResponseFormat responseFormat,
244         User user, AuditingActionEnum auditAction){
245         String commentStr = null;
246         String distrStatus = null;
247         ComponentTypeEnum componentType = component.getComponentType();
248             if (componentType == ComponentTypeEnum.SERVICE) {
249             distrStatus = ((ServiceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getDistributionStatus();
250             commentStr = comment;
251         }
252         componentsUtils.auditComponent(responseFormat, user, component, auditAction, new ResourceCommonInfo(componentType.getValue()),
253             ResourceVersionInfo.newBuilder().distributionStatus(distrStatus).build(),
254             ResourceVersionInfo.newBuilder().distributionStatus(distrStatus).build(),
255             commentStr, null, null);
256     }
257
258     public Either<Boolean, ResponseFormat> validateConformanceLevel(String componentUuid, ComponentTypeEnum componentTypeEnum, String userId) {
259         log.trace("validate conformance level");
260
261         if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
262             log.error("conformance level validation for non service component, id {}", componentUuid);
263             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
264             return Either.right(errorResponse);
265         }
266
267         validateUserExists(userId);
268
269         Either<ComponentMetadataData, StorageOperationStatus> eitherComponent = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, null);
270         if (eitherComponent.isRight()) {
271             log.error("can't validate conformance level, component not found, uuid {}", componentUuid);
272             BeEcompErrorManager.getInstance().logBeComponentMissingError("validateConformanceLevel", componentTypeEnum.getValue(), componentUuid);
273
274             StorageOperationStatus status = eitherComponent.right().value();
275             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(status, componentTypeEnum);
276             ResponseFormat responseFormat = componentsUtils.getResponseFormat(actionStatus);
277             return Either.right(responseFormat);
278         }
279
280         String componentConformanceLevel = eitherComponent.left().value().getMetadataDataDefinition().getConformanceLevel();
281         if (StringUtils.isBlank(componentConformanceLevel)) {
282             log.error("component conformance level property is null or empty, uuid {}", componentUuid);
283             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
284             return Either.right(errorResponse);
285         }
286
287         String configConformanceLevel = ConfigurationManager.getConfigurationManager().getConfiguration().getMinToscaConformanceLevel();
288         Boolean result = true;
289         if (CommonBeUtils.conformanceLevelCompare(componentConformanceLevel, configConformanceLevel) < 0) {
290             log.error("invalid asset conformance level, uuid {}, asset conformanceLevel {}, config conformanceLevel {}", componentUuid, componentConformanceLevel, configConformanceLevel);
291             result = false;
292         }
293         log.trace("conformance level validation finished");
294
295         return Either.left(result);
296     }
297
298     protected void validateIcon(User user, Component component, AuditingActionEnum actionEnum) {
299         log.debug("validate Icon");
300         ComponentTypeEnum type = component.getComponentType();
301         String icon = component.getIcon();
302         if (!ValidationUtils.validateStringNotEmpty(icon)) {
303             log.info("icon is missing.");
304             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_ICON, type.getValue());
305             componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, type);
306             throw new ComponentException(ActionStatus.COMPONENT_MISSING_ICON, type.getValue());
307         }
308         try {
309             validateIcon(icon, type);
310         } catch(ComponentException e){
311             ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat()
312                     : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
313             componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, type);
314             throw e;
315         }
316     }
317
318     private void validateIcon(String icon, ComponentTypeEnum type) {
319         if (icon != null) {
320             if (!ValidationUtils.validateIconLength(icon)) {
321                 log.debug("icon exceeds max length");
322                 throw new ComponentException(ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT, type.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
323             }
324
325             if (!ValidationUtils.validateIcon(icon)) {
326                 log.info("icon is invalid.");
327                 throw new ComponentException(ActionStatus.COMPONENT_INVALID_ICON, type.getValue());
328             }
329         }
330     }
331
332     protected void checkComponentFieldsForOverrideAttempt(Component component) {
333         if (component.getLifecycleState() != null) {
334             log.info("LifecycleState cannot be defined by user. This field will be overridden by the application");
335         }
336         if (component.getVersion() != null) {
337             log.info("Version cannot be defined by user. This field will be overridden by the application");
338         }
339         if (component.getCreatorUserId() != null || component.getCreatorFullName() != null) {
340             log.info("Creator cannot be defined by user. This field will be overridden by the application");
341         }
342         if (component.getLastUpdaterUserId() != null || component.getLastUpdaterFullName() != null) {
343             log.info("Last Updater cannot be defined by user. This field will be overridden by the application");
344         }
345         if (component.getCreationDate() != null) {
346             log.info("Creation Date cannot be defined by user. This field will be overridden by the application");
347         }
348         if (component.isHighestVersion() != null) {
349             log.info("Is Highest Version cannot be defined by user. This field will be overridden by the application");
350         }
351         if (component.getUUID() != null) {
352             log.info("UUID cannot be defined by user. This field will be overridden by the application");
353         }
354         if (component.getLastUpdateDate() != null) {
355             log.info("Last Update Date cannot be defined by user. This field will be overridden by the application");
356         }
357         if (component.getUniqueId() != null) {
358             log.info("uid cannot be defined by user. This field will be overridden by the application.");
359             component.setUniqueId(null);
360         }
361         if (component.getInvariantUUID() != null) {
362             log.info("Invariant UUID cannot be defined by user. This field will be overridden by the application.");
363         }
364     }
365
366     protected void validateComponentFieldsBeforeCreate(User user, Component component, AuditingActionEnum actionEnum) {
367         // validate component name uniqueness
368         log.debug("validate component name ");
369         componentNameValidator.validateAndCorrectField(user, component, actionEnum);
370         // validate description
371         log.debug("validate description");
372         componentDescriptionValidator.validateAndCorrectField(user, component, actionEnum);
373         // validate tags
374         log.debug("validate tags");
375         componentTagsValidator.validateAndCorrectField(user, component, actionEnum);
376         // validate contact info
377         log.debug("validate contact info");
378         componentContactIdValidator.validateAndCorrectField(user, component, actionEnum);
379         // validate icon
380         log.debug("validate icon");
381         validateIcon(user, component, actionEnum);
382     }
383         
384     public CapReqDef getRequirementsAndCapabilities(String componentId, ComponentTypeEnum componentTypeEnum, String userId) {
385
386         validateUserExists(userId);
387         ComponentParametersView filter = new ComponentParametersView(true);
388         filter.setIgnoreCapabilities(false);
389         filter.setIgnoreRequirements(false);
390         filter.setIgnoreComponentInstances(false);
391
392         try {
393             Component component = validateComponentExists(componentId, componentTypeEnum, filter);
394             return new CapReqDef(component.getRequirements(), component.getCapabilities());
395         }catch (ComponentException e){
396             BeEcompErrorManager.getInstance().logBeComponentMissingError("getRequirementsAndCapabilities", componentTypeEnum.getValue(), componentId);
397             throwComponentException(e.getResponseFormat());
398         }
399         return null;
400     }
401
402     public Either<List<Component>, ResponseFormat> getLatestVersionNotAbstractComponents(boolean isAbstractAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids,
403                                                                                          String userId) {
404         try{
405             validateUserExists(userId);
406             List<Component> result = new ArrayList<>();
407             List<String> componentsUidToFetch = new ArrayList<>();
408             componentsUidToFetch.addAll(componentUids);
409             if (!componentsUidToFetch.isEmpty()) {
410                 log.debug("Number of Components to fetch from graph is {}", componentsUidToFetch.size());
411                 Either<List<Component>, StorageOperationStatus> nonCheckoutCompResponse = toscaOperationFacade.getLatestVersionNotAbstractComponents(isAbstractAbstract, componentTypeEnum, internalComponentType, componentsUidToFetch);
412
413                 if (nonCheckoutCompResponse.isLeft()) {
414                     log.debug("Retrived Resource successfully.");
415                     result.addAll(nonCheckoutCompResponse.left().value());
416                 } else {
417                     return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(nonCheckoutCompResponse.right().value())));
418                 }
419             }
420             return Either.left(result);
421         }
422         finally{
423             janusGraphDao.commit();
424         }
425     }
426
427     private Boolean isHighest(HighestFilterEnum highestFilter) {
428         Boolean isHighest = null;
429         switch (highestFilter) {
430         case ALL:
431             break;
432         case HIGHEST_ONLY:
433             isHighest = true;
434             break;
435         case NON_HIGHEST_ONLY:
436             isHighest = false;
437             break;
438         default:
439             break;
440         }
441         return isHighest;
442     }
443
444     public Either<List<Component>, ResponseFormat> getLatestVersionNotAbstractComponentsMetadata(boolean isAbstractAbstract, HighestFilterEnum highestFilter, ComponentTypeEnum componentTypeEnum, String internalComponentType, String userId) {
445         ResponseFormat responseFormat = null;
446
447         try{
448             validateUserExists(userId);
449             Boolean isHighest = isHighest(highestFilter);
450             Either<List<Component>, StorageOperationStatus> nonCheckoutCompResponse = toscaOperationFacade.getLatestVersionNotAbstractMetadataOnly(isAbstractAbstract, componentTypeEnum, internalComponentType);
451
452             if (nonCheckoutCompResponse.isLeft()) {
453                 log.debug("Retrieved Resource successfully.");
454                 return Either.left(nonCheckoutCompResponse.left().value());
455             }
456             responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(nonCheckoutCompResponse.right().value()));
457         } finally {
458             janusGraphDao.commit();
459         }
460         return Either.right(responseFormat);
461     }
462
463     public void setDeploymentArtifactsPlaceHolder(Component component, User user) {
464
465     }
466
467     @SuppressWarnings("unchecked")
468     public void setToscaArtifactsPlaceHolders(Component component, User user) {
469         Map<String, ArtifactDefinition> artifactMap = component.getToscaArtifacts();
470         if (artifactMap == null) {
471             artifactMap = new HashMap<>();
472         }
473         String componentUniqueId = component.getUniqueId();
474         String componentSystemName = component.getSystemName();
475         String componentType = component.getComponentType().getValue().toLowerCase();
476         Map<String, Object> toscaArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts();
477
478         if (toscaArtifacts != null) {
479             for (Entry<String, Object> artifactInfoMap : toscaArtifacts.entrySet()) {
480                 Map<String, Object> artifactInfo = (Map<String, Object>) artifactInfoMap.getValue();
481                 ArtifactDefinition artifactDefinition = artifactsBusinessLogic.createArtifactPlaceHolderInfo(componentUniqueId, artifactInfoMap.getKey(), artifactInfo, user, ArtifactGroupTypeEnum.TOSCA);
482                 artifactDefinition.setArtifactName(ValidationUtils.normalizeFileName(componentType + "-" + componentSystemName + artifactInfo.get("artifactName")));
483                 artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
484             }
485         }
486         component.setToscaArtifacts(artifactMap);
487     }
488
489     public Either<ArtifactDefinition, Operation> populateToscaArtifacts(Component component, User user, boolean isInCertificationRequest, boolean inTransaction, boolean shouldLock) {
490         return populateToscaArtifacts(component, user, isInCertificationRequest, inTransaction, shouldLock, true, true);
491     }
492
493     public Either<ArtifactDefinition, Operation> populateToscaArtifacts(Component component, User user, boolean isInCertificationRequest, boolean inTransaction, boolean shouldLock, boolean retrieveResource) {
494         return populateToscaArtifacts(component, user, isInCertificationRequest, inTransaction, shouldLock, true, retrieveResource);
495     }
496
497     private Either<ArtifactDefinition, Operation> populateToscaArtifacts(Component component, User user, boolean isInCertificationRequest, boolean inTransaction, boolean shouldLock, boolean fetchTemplatesFromDB, boolean retrieveResource) {
498         if (retrieveResource) {
499             Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaFullElement(component.getUniqueId());
500             if ( toscaElement.isRight() ){
501                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(toscaElement.right().value(), component.getComponentType()));
502             }
503             component = toscaElement.left().value();
504         }
505
506         Either<ArtifactDefinition, Operation> generateToscaRes =
507             saveToscaArtifactAndPopulateToscaArtifactsWithResult(component, componentsUtils,
508                 ArtifactTypeEnum.TOSCA_TEMPLATE,
509                 (comp, toscaArtifact) ->
510                     saveToscaArtifactPayload(toscaArtifact, comp, user, isInCertificationRequest, shouldLock,
511                         inTransaction, fetchTemplatesFromDB));
512
513         if (!isAbstractResource(component)) {
514             generateToscaRes = saveToscaArtifactAndPopulateToscaArtifactsWithResult(component, componentsUtils,
515                 ArtifactTypeEnum.TOSCA_CSAR,
516                 (comp, toscaArtifactArg) -> saveToscaArtifactPayload(toscaArtifactArg, comp, user,
517                     isInCertificationRequest, shouldLock, inTransaction, true));
518         }
519         return generateToscaRes;
520     }
521
522     private static Either<ArtifactDefinition, Operation> saveToscaArtifactAndPopulateToscaArtifactsWithResult(
523         Component component,
524         final ComponentsUtils componentsUtils,
525         final ArtifactTypeEnum artifactEnum,
526         final BiFunction<Component, ArtifactDefinition, Either<ArtifactDefinition, Operation>> saveToscaArtifactPayloadFunction) {
527
528         ArtifactDefinition artifactDefinition = getToscaArtifactByTypeOrThrowException(component, artifactEnum,
529             componentsUtils);
530
531         Either<ArtifactDefinition, Operation> result =
532             saveToscaArtifactPayloadFunction.apply(component, artifactDefinition);
533
534         if (result.isLeft()) {
535             ArtifactDefinition def = result.left().value();
536             component.getToscaArtifacts().put(def.getArtifactLabel(), def);
537         }
538
539         return result;
540     }
541
542     private static Optional<ArtifactDefinition> getToscaArtifactByType(
543         final Map<String, ArtifactDefinition> toscaArtifacts,
544         final ArtifactTypeEnum typeEnum) {
545         return toscaArtifacts.values().stream()
546             .filter(p -> p.getArtifactType().equals(typeEnum.getType()))
547             .findAny();
548     }
549
550     private static ArtifactDefinition getToscaArtifactByTypeOrThrowException(
551         final Component component,
552         final ArtifactTypeEnum typeEnum,
553         final ComponentsUtils componentsUtils) {
554
555         return Optional.ofNullable(component.getToscaArtifacts())
556             .flatMap(toscaArtifacts -> getToscaArtifactByType(toscaArtifacts, typeEnum))
557             .orElseThrow(() -> {
558                     log.debug("Impossible to find a ToscaArtifact with type '{}' for {}", typeEnum.getType(), component);
559                     return new ByResponseFormatComponentException(
560                         componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, typeEnum.name()));
561                 });
562     }
563
564     private boolean isAbstractResource(Component component) {
565         return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource)component).isAbstract();
566     }
567
568     private Either<ArtifactDefinition, Operation> saveToscaArtifactPayload(ArtifactDefinition artifactDefinition, org.openecomp.sdc.be.model.Component component, User user, boolean isInCertificationRequest, boolean shouldLock,
569                                                                                                    boolean inTransaction, boolean fetchTemplatesFromDB) {
570         return artifactsBusinessLogic.generateAndSaveToscaArtifact(artifactDefinition, component, user, isInCertificationRequest, shouldLock, inTransaction, fetchTemplatesFromDB);
571     }
572
573     public ImmutablePair<String, byte[]> getToscaModelByComponentUuid(ComponentTypeEnum componentType, String uuid, ResourceCommonInfo resourceCommonInfo) {
574
575         Either<List<Component>, StorageOperationStatus> latestVersionEither = toscaOperationFacade.getComponentListByUuid(uuid, null);
576
577         if (latestVersionEither.isRight()) {
578             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(latestVersionEither.right().value(), componentType));
579         }
580
581         List<Component> components = latestVersionEither.left().value();
582
583         Component component = components.stream().filter(Component::isHighestVersion).findFirst().orElse(null);
584         if(component == null){
585             component = components.stream().filter(c -> c.getLifecycleState() == LifecycleStateEnum.CERTIFIED).findFirst().orElse(null);
586         }
587
588         if(component == null){
589             throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, componentType)));
590         }
591         resourceCommonInfo.setResourceName(component.getName());
592         // TODO remove after migration - handle artifact not found(no
593         // placeholder)
594         if (null == component.getToscaArtifacts() || component.getToscaArtifacts().isEmpty()) {
595             throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ArtifactTypeEnum.TOSCA_CSAR.name()));
596         }
597         ArtifactDefinition csarArtifact = component.getToscaArtifacts().values().stream()
598                 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType()))
599                 .findAny().get();
600         return artifactsBusinessLogic.handleDownloadToscaModelRequest(component, csarArtifact);
601     }
602
603     protected StorageOperationStatus markComponentToDelete(Component component) {
604
605         ComponentTypeEnum componentType = component.getComponentType();
606         String uniqueId = component.getUniqueId();
607         if (Boolean.TRUE.equals(component.getIsDeleted())) {
608             log.info("component {} already marked as deleted. id= {}, type={}", component.getName(), uniqueId, componentType);
609             return StorageOperationStatus.NOT_FOUND;
610         }
611
612         StorageOperationStatus markResourceToDelete = toscaOperationFacade.markComponentToDelete(component);
613         if (StorageOperationStatus.OK != markResourceToDelete) {
614             log.debug("failed to mark component {} of type {} for delete. error = {}", uniqueId, componentType, markResourceToDelete);
615             return markResourceToDelete;
616         } else {
617             log.debug("Component {}  of type {} was marked as deleted", uniqueId, componentType);
618             updateCatalog(component, ChangeTypeEnum.DELETE);
619             return StorageOperationStatus.OK;
620         }
621     }
622
623     public Either<Boolean, ResponseFormat> validateAndUpdateDescription(User user, Component currentComponent, Component updatedComponent, AuditingActionEnum auditingAction) {
624         String descriptionUpdated = updatedComponent.getDescription();
625         String descriptionCurrent = currentComponent.getDescription();
626         if (descriptionUpdated != null && !descriptionCurrent.equals(descriptionUpdated)) {
627             componentDescriptionValidator.validateAndCorrectField(user, updatedComponent, auditingAction);
628             currentComponent.setDescription(updatedComponent.getDescription());
629         }
630         return Either.left(true);
631     }
632
633     public Either<Boolean, ResponseFormat> validateAndUpdateProjectCode(User user, Component currentComponent, Component updatedComponent) {
634         String projectCodeUpdated = updatedComponent.getProjectCode();
635         String projectCodeCurrent = currentComponent.getProjectCode();
636         if (projectCodeUpdated != null && !projectCodeCurrent.equals(projectCodeUpdated)) {
637             try {
638                 componentProjectCodeValidator.validateAndCorrectField(user, updatedComponent, null);
639             } catch (ComponentException exp) {
640                 ResponseFormat errorRespons = exp.getResponseFormat();
641                 return Either.right(errorRespons);
642             }
643             currentComponent.setProjectCode(updatedComponent.getProjectCode());
644         }
645         return Either.left(true);
646     }
647
648     public Either<Boolean, ResponseFormat> validateAndUpdateIcon(User user, Component currentComponent, Component updatedComponent, boolean hasBeenCertified) {
649         String iconUpdated = updatedComponent.getIcon();
650         String iconCurrent = currentComponent.getIcon();
651         if (iconUpdated != null && !iconCurrent.equals(iconUpdated)) {
652             if (!hasBeenCertified) {
653                 componentIconValidator.validateAndCorrectField(user, updatedComponent, null);
654                 currentComponent.setIcon(updatedComponent.getIcon());
655             } else {
656                 log.info("icon {} cannot be updated once the component has been certified once.", iconUpdated);
657                 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_PARAMETER_CANNOT_BE_CHANGED, "Icon", currentComponent.getComponentType().name().toLowerCase());
658                 return Either.right(errorResponse);
659             }
660         }
661         return Either.left(true);
662     }
663
664     protected Either<List<String>, ResponseFormat> deleteMarkedComponents(ComponentTypeEnum componentType) {
665
666         log.trace("start deleteMarkedComponents");
667         Either<List<String>, StorageOperationStatus> deleteMarkedElements = toscaOperationFacade.deleteMarkedElements(componentType);
668
669
670         if ( deleteMarkedElements.isRight()){
671             janusGraphDao.rollback();
672             ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteMarkedElements.right().value(), componentType));
673             return Either.right(responseFormat);
674         }
675         log.trace("end deleteMarkedComponents");
676         janusGraphDao.commit();
677         return Either.left(deleteMarkedElements.left().value());
678     }
679
680     public Either<List<ArtifactDefinition>, StorageOperationStatus> getComponentArtifactsForDelete(String parentId, NodeTypeEnum parentType) {
681         List<ArtifactDefinition> artifacts = new ArrayList<>();
682         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsResponse = artifactToscaOperation.getArtifacts(parentId);
683         if (artifactsResponse.isRight()) {
684             if (artifactsResponse.right().value() != StorageOperationStatus.NOT_FOUND) {
685                 log.debug("failed to retrieve artifacts for {} {}", parentType, parentId);
686                 return Either.right(artifactsResponse.right().value());
687             }
688         } else {
689             artifacts.addAll(artifactsResponse.left().value().values());
690         }
691         return Either.left(artifacts);
692     }
693
694     /**
695      *
696      * @param componentId
697      * @param user
698      * @param dataParamsToReturn - ui list of params to return
699      * @return
700      */
701
702     public Either<UiComponentDataTransfer, ResponseFormat> getComponentDataFilteredByParams(String componentId, User user, List<String> dataParamsToReturn) {
703
704         if (user != null) {
705             validateUserExists(user);
706         }
707
708         UiComponentDataTransfer result =  new UiComponentDataTransfer();
709
710         if(dataParamsToReturn == null ||  dataParamsToReturn.isEmpty()) {
711             Either.left(result);
712
713         } else {
714             Either<UiComponentDataTransfer, ResponseFormat> uiDataTransferEither = getUiComponentDataTransferByComponentId(componentId, dataParamsToReturn);
715             if(uiDataTransferEither.isRight()){
716                 return Either.right(uiDataTransferEither.right().value());
717             }
718             result = uiDataTransferEither.left().value();
719         }
720
721         return Either.left(result);
722     }
723
724     protected <T extends Component> void generateAndAddInputsFromGenericTypeProperties(T component, Resource genericType) {
725         List<InputDefinition> genericAndComponentInputs = new ArrayList<>();
726         List<InputDefinition> genericInputs = genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericType);
727         genericAndComponentInputs.addAll(genericInputs);
728         if (null != component.getInputs()){
729             List<InputDefinition> nonGenericInputsFromComponent = getAllNonGenericInputsFromComponent(genericInputs, component.getInputs());
730             genericAndComponentInputs.addAll(nonGenericInputsFromComponent);
731         }
732         component.setInputs(genericAndComponentInputs);
733     }
734
735     private List<InputDefinition> getAllNonGenericInputsFromComponent(List<InputDefinition> genericInputs, List<InputDefinition> componentInputs) {
736         if (genericInputs == null) {
737             return componentInputs;
738         }
739
740         Map<String, InputDefinition> inputByNameMap = MapUtil.toMap(genericInputs, InputDefinition::getName);
741         List<InputDefinition> componentNonGenericInputs = new ArrayList<>();
742         componentInputs.stream().forEach(input -> {
743             if (!inputByNameMap.containsKey(input.getName())) {
744                 componentNonGenericInputs.add(input);
745             }
746         });
747         return componentNonGenericInputs;
748     }
749
750     protected <T extends Component> Resource fetchAndSetDerivedFromGenericType(T component){
751         Either<Resource, ResponseFormat> genericTypeEither = this.genericTypeBusinessLogic.fetchDerivedFromGenericType(component);
752         if(genericTypeEither.isRight()){
753             log.debug("Failed to fetch latest generic type for component {} of type", component.getName(), component.assetType());
754             throw new ByActionStatusComponentException(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType());
755         }
756         Resource genericTypeResource = genericTypeEither.left().value();
757         component.setDerivedFromGenericInfo(genericTypeResource);
758         return genericTypeResource;
759     }
760
761     public Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> getFilteredComponentInstanceProperties(String componentId, Map<FilterKeyEnum, List<String>> filters, String userId) {
762         Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> response = null;
763         Either<Component, StorageOperationStatus> getResourceRes = null;
764         try{
765             if(!filters.containsKey(FilterKeyEnum.NAME_FRAGMENT) && StringUtils.isEmpty(filters.get(FilterKeyEnum.NAME_FRAGMENT).get(0))){
766                 response = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
767             }
768             if (userId != null && response == null) {
769                 validateUserExists(userId);
770             }
771             if(response == null){
772                 getResourceRes = toscaOperationFacade.getToscaElement(componentId);
773                 if(getResourceRes.isRight()){
774                     response = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getResourceRes.right().value())));
775                 }
776             }
777             if(response == null){
778                 response = getFilteredComponentInstancesProperties(getResourceRes.left().value(), filters);
779             }
780         } catch(Exception e){
781             log.debug("The exception {} occured during filtered instance properties fetching. the  containing component is {}. ", e, componentId);
782             response = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
783         } finally{
784             if (response != null && response.isLeft()){
785                 toscaOperationFacade.commit();
786             } else {
787                 toscaOperationFacade.rollback();
788             }
789         }
790         return response;
791     }
792
793     private Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> getFilteredComponentInstancesProperties(Component component, Map<FilterKeyEnum, List<String>> filters) {
794
795         Map<String,  List<IComponentInstanceConnectedElement>> filteredProperties = new HashMap<>();
796         Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> result = Either.left(filteredProperties);
797         List<ComponentInstance> filteredInstances = getFilteredInstances(component, filters.get(FilterKeyEnum.RESOURCE_TYPE));
798         String propertyNameFragment= filters.get(FilterKeyEnum.NAME_FRAGMENT).get(0);
799         boolean searchByFragment = propertyNameFragment.length() > 3 ;
800         if(CollectionUtils.isNotEmpty(filteredInstances)){
801             for(ComponentInstance instance : filteredInstances){
802                 if(component.getComponentInstancesProperties()!=null &&component.getComponentInstancesProperties().containsKey(instance.getUniqueId())){
803                     List<IComponentInstanceConnectedElement> currProperties =  getFilteredComponentInstanceProperties(component.getComponentInstancesProperties().get(instance.getUniqueId()), propertyNameFragment, searchByFragment);
804                     setFilteredProperties(filteredProperties, instance, currProperties);
805                 }
806                 if(component.getComponentInstancesInputs()!=null && component.getComponentInstancesInputs().containsKey(instance.getUniqueId())){
807                     List<IComponentInstanceConnectedElement> currInputs =  getFilteredComponentInstanceInputs(component.getComponentInstancesInputs().get(instance.getUniqueId()), propertyNameFragment, searchByFragment);
808                     if(CollectionUtils.isNotEmpty(currInputs)){
809                         checkFilteredProperties(filteredProperties, instance, currInputs);
810                     }
811                 }
812             }
813         }
814         return result;
815     }
816
817     private void setFilteredProperties(Map<String, List<IComponentInstanceConnectedElement>> filteredProperties, ComponentInstance instance, List<IComponentInstanceConnectedElement> currProperties) {
818         if(CollectionUtils.isNotEmpty(currProperties)){
819             filteredProperties.put(instance.getUniqueId(), currProperties);
820         }
821     }
822
823     private void checkFilteredProperties(Map<String, List<IComponentInstanceConnectedElement>> filteredProperties, ComponentInstance instance, List<IComponentInstanceConnectedElement> currInputs) {
824         if(filteredProperties.get(instance.getUniqueId())!=null){
825             filteredProperties.get(instance.getUniqueId()).addAll(currInputs);
826         } else {
827             filteredProperties.put(instance.getUniqueId(), currInputs);
828         }
829     }
830
831     private List<IComponentInstanceConnectedElement> getFilteredComponentInstanceInputs(List<ComponentInstanceInput> inputs, String propertyNameFragment, boolean searchByFragment) {
832         return inputs.stream().filter(i -> isMatchingInput(i, propertyNameFragment, searchByFragment)).collect(Collectors.toList());
833     }
834
835     private List<IComponentInstanceConnectedElement> getFilteredComponentInstanceProperties(List<ComponentInstanceProperty> instanceProperties, String propertyNameFragment, boolean searchByFragment) {
836         return instanceProperties.stream().filter(p -> isMatchingProperty(p, propertyNameFragment, searchByFragment)).collect(Collectors.toList());
837     }
838
839     private boolean isMatchingInput(ComponentInstanceInput input, String propertyNameFragment, boolean searchByFragment) {
840         boolean isMatching = false;
841         if(searchByFragment && input.getName().toLowerCase().contains(propertyNameFragment)){
842             isMatching = true;
843         }
844         if(!searchByFragment && input.getName().equalsIgnoreCase(propertyNameFragment)){
845             isMatching = true;
846         }
847         return isMatching;
848     }
849
850     private boolean isMatchingProperty(ComponentInstanceProperty property, String propertyNameFragment, boolean searchByFragment) {
851         boolean isMatching = false;
852         if(searchByFragment && property.getName().toLowerCase().contains(propertyNameFragment)){
853             isMatching = true;
854         }
855         if(!searchByFragment && property.getName().equalsIgnoreCase(propertyNameFragment)){
856             isMatching = true;
857         }
858         if (!isMatching && !ToscaPropertyType.isPrimitiveType(property.getType())){
859             isMatching = isMatchingComplexPropertyByRecursively(property, propertyNameFragment, searchByFragment);
860         }
861         return isMatching;
862     }
863
864     private boolean isMatchingComplexPropertyByRecursively(PropertyDataDefinition property, String propertyNameFragment,  boolean searchByFragment) {
865         String propertyType;
866         List<PropertyDefinition>  dataTypeProperties;
867         DataTypeDefinition currentProperty;
868         if(searchByFragment && property.getName().toLowerCase().contains(propertyNameFragment.toLowerCase())){
869             return true;
870         }
871         if(!searchByFragment && property.getName().equalsIgnoreCase(propertyNameFragment)){
872             return true;
873         }
874
875         propertyType = isEmptyInnerType(property) ? property.getType() : property.getSchema().getProperty().getType();
876
877         if(ToscaPropertyType.isScalarType(propertyType)){
878             return false;
879         }
880         Either<DataTypeDefinition, StorageOperationStatus>  getDataTypeByNameRes = propertyOperation.getDataTypeByName(propertyType);
881         if(getDataTypeByNameRes.isRight()){
882             return false;
883         }
884         currentProperty = getDataTypeByNameRes.left().value();
885         dataTypeProperties = currentProperty.getProperties();
886
887         boolean dataPropertiesNotNull = CollectionUtils.isNotEmpty(dataTypeProperties);
888         BooleanSupplier dataMatchesComplexProperty = () -> isMatchingComplexProperty(propertyNameFragment,
889             searchByFragment, dataTypeProperties);
890         BooleanSupplier parentPropertiesNotNull = () -> CollectionUtils
891             .isNotEmpty(currentProperty.getDerivedFrom().getProperties());
892         BooleanSupplier parentDataMatchesComplexProperty = () -> isMatchingComplexProperty(propertyNameFragment,
893             searchByFragment, currentProperty.getDerivedFrom().getProperties());
894
895         return ((dataPropertiesNotNull && dataMatchesComplexProperty.getAsBoolean())
896             || (parentPropertiesNotNull.getAsBoolean() && parentDataMatchesComplexProperty.getAsBoolean()));
897     }
898
899     private boolean isMatchingComplexProperty(String propertyNameFragment, boolean searchByFragment, List<PropertyDefinition> dataTypeProperties) {
900         for(PropertyDefinition prop : dataTypeProperties){
901             if(isMatchingComplexPropertyByRecursively(prop, propertyNameFragment, searchByFragment)){
902                 return true;
903             }
904         }
905         return false;
906     }
907
908     private boolean isEmptyInnerType(PropertyDataDefinition property) {
909         return property == null|| property.getSchema() == null || property.getSchema().getProperty() == null || property.getSchema().getProperty().getType() == null;
910     }
911
912     public Either<Boolean, ResponseFormat> shouldUpgradeToLatestGeneric(Component clonedComponent) {
913
914         if(!clonedComponent.deriveFromGeneric())
915             return Either.left(false);
916         Boolean shouldUpgrade = false;
917         String currentGenericType = clonedComponent.getDerivedFromGenericType();
918         String currentGenericVersion = clonedComponent.getDerivedFromGenericVersion();
919         Resource genericTypeResource = fetchAndSetDerivedFromGenericType(clonedComponent);
920         if(null == currentGenericType || !currentGenericType.equals(genericTypeResource.getToscaResourceName()) || !currentGenericVersion.equals(genericTypeResource.getVersion())){
921             shouldUpgrade = upgradeToLatestGeneric(clonedComponent, genericTypeResource);
922             if(!shouldUpgrade) {
923                 reverntUpdateOfGenericVersion(clonedComponent, currentGenericType, currentGenericVersion);
924             }
925         }
926         return Either.left(shouldUpgrade);
927     }
928
929     private void reverntUpdateOfGenericVersion(Component clonedComponent, String currentGenericType, String currentGenericVersion) {
930         clonedComponent.setDerivedFromGenericType(currentGenericType);
931         clonedComponent.setDerivedFromGenericVersion(currentGenericVersion);
932     }
933
934     private <T extends ToscaDataDefinition> Either<Map<String, T>, String> validateNoConflictingProperties(List<T> currentList, List<T> upgradedList) {
935         Map<String, T> currentMap = ToscaDataDefinition.listToMapByName(currentList);
936         Map<String, T> upgradedMap = ToscaDataDefinition.listToMapByName(upgradedList);
937         return ToscaDataDefinition.mergeDataMaps(upgradedMap, currentMap, true);
938     }
939
940     private boolean shouldUpgradeNodeType(Component componentToCheckOut, Resource latestGeneric){
941
942         List<PropertyDefinition> genericTypeProps = latestGeneric.getProperties();
943         Either<Map<String, PropertyDefinition>, String> validPropertiesMerge = validateNoConflictingProperties(genericTypeProps, ((Resource)componentToCheckOut).getProperties());
944         if (validPropertiesMerge.isRight()) {
945             log.debug("property {} cannot be overriden, check out performed without upgrading to latest generic", validPropertiesMerge.right().value());
946             return false;
947         }
948         List<AttributeDataDefinition> genericTypeAttributes = latestGeneric.getAttributes();
949         final Either<Map<String, AttributeDataDefinition>, String> validAttributesMerge = validateNoConflictingProperties(
950             genericTypeAttributes, ((Resource) componentToCheckOut).getAttributes());
951         if (validAttributesMerge.isRight()) {
952             log.debug("attribute {} cannot be overriden, check out performed without upgrading to latest generic", validAttributesMerge.right().value());
953             return false;
954         }
955         return true;
956     }
957
958     private boolean upgradeToLatestGeneric(Component componentToCheckOut, Resource latestGeneric) {
959
960         if (!componentToCheckOut.shouldGenerateInputs()) {
961             //node type - validate properties and attributes
962             return shouldUpgradeNodeType(componentToCheckOut, latestGeneric);
963         }
964         List<PropertyDefinition> genericTypeProps = latestGeneric.getProperties();
965         List<InputDefinition> genericTypeInputs = null == genericTypeProps? null : genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericTypeProps, latestGeneric.getUniqueId());
966         List<InputDefinition> currentList = new ArrayList<>();
967         // nullify existing ownerId from existing list and merge into updated list
968         if (null != componentToCheckOut.getInputs()) {
969             for(InputDefinition input : componentToCheckOut.getInputs()) {
970                 InputDefinition copy = new InputDefinition(input);
971                 copy.setOwnerId(null);
972                 currentList.add(copy);
973             }
974         }
975         if (null == genericTypeInputs) {
976             componentToCheckOut.setInputs(currentList);
977             return true;
978         }
979
980         Either<Map<String, InputDefinition>, String> eitherMerged = validateNoConflictingProperties(genericTypeInputs, currentList);
981         if (eitherMerged.isRight()) {
982             log.debug("input {} cannot be overriden, check out performed without upgrading to latest generic", eitherMerged.right().value());
983             return false;
984         }
985         componentToCheckOut.setInputs(new ArrayList<>(eitherMerged.left().value().values()));
986         return true;
987     }
988
989
990     private List<ComponentInstance> getFilteredInstances(Component component, List<String> resourceTypes) {
991         List<ComponentInstance> filteredInstances = null;
992         if(CollectionUtils.isEmpty(resourceTypes)){
993             filteredInstances = component.getComponentInstances();
994         }
995         else if(CollectionUtils.isNotEmpty(component.getComponentInstances())){
996             filteredInstances = component.getComponentInstances()
997                     .stream().filter(i -> isMatchingType(i.getOriginType(), resourceTypes)).collect(Collectors.toList());
998         }
999         if(filteredInstances == null){
1000             filteredInstances = new ArrayList<>();
1001         }
1002         return filteredInstances;
1003     }
1004
1005     private boolean isMatchingType(OriginTypeEnum originType, List<String> resourceTypes) {
1006         boolean isMatchingType = false;
1007         for(String resourceType : resourceTypes){
1008             if(originType == OriginTypeEnum.findByValue(resourceType.toUpperCase())){
1009                 isMatchingType = true;
1010                 break;
1011             }
1012         }
1013         return isMatchingType;
1014     }
1015
1016     public Either<Component, ActionStatus> shouldUpgradeToLatestDerived(Component clonedComponent) {
1017         //general implementation. Must be error for service, VF . In ResourceBuisnessLogic exist override
1018         return Either.right(ActionStatus.GENERAL_ERROR);
1019     }
1020
1021     protected Either<Component, ResponseFormat>  updateCatalog(Component component, ChangeTypeEnum changeStatus){
1022         log.debug("update Catalog start with Component Type {} And Componet Name {} with change status {}", component.getComponentType().name(), component.getName(), changeStatus.name());
1023         ActionStatus status =  catalogOperations.updateCatalog(changeStatus,component);
1024         if(status != ActionStatus.OK){
1025             return Either.right( componentsUtils.getResponseFormat(status));
1026         }
1027
1028        return Either.left(component);
1029     }
1030
1031     public CatalogOperation getCatalogOperations() {
1032         return catalogOperations;
1033     }
1034
1035     @Autowired
1036     public void setCatalogOperations(CatalogOperation catalogOperations) {
1037         this.catalogOperations = catalogOperations;
1038     }
1039
1040     public List<GroupDefinition> throwComponentException(ResponseFormat responseFormat) {
1041         throw new ByResponseFormatComponentException(responseFormat);
1042     }
1043
1044 }
1045
1046