Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / GlobalInputsFilteringBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.merge;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.InputDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32
33 import java.util.List;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36
37 @org.springframework.stereotype.Component
38 public class GlobalInputsFilteringBusinessLogic {
39
40     private GenericTypeBusinessLogic genericTypeBusinessLogic;
41     private ToscaOperationFacade toscaOperationFacade;
42     private ComponentsUtils componentsUtils;
43
44     public GlobalInputsFilteringBusinessLogic(GenericTypeBusinessLogic genericTypeBusinessLogic, ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils) {
45         this.genericTypeBusinessLogic = genericTypeBusinessLogic;
46         this.toscaOperationFacade = toscaOperationFacade;
47         this.componentsUtils = componentsUtils;
48     }
49
50     public Either<List<InputDefinition>, ActionStatus> filterGlobalInputs(Component newResource) {
51             Either<Resource, StorageOperationStatus> genericComp = toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(newResource.fetchGenericTypeToscaNameFromConfig());
52             return genericComp.bimap(genericResource -> findCommonInputs(genericResource, newResource),
53                                      storageOperationStatus -> componentsUtils.convertFromStorageResponse(storageOperationStatus));
54     }
55
56     private List<InputDefinition> findCommonInputs(Resource genericResource, Component resource) {
57         List<InputDefinition> resourceInputs = resource.getInputs();
58         List<InputDefinition> genericInputs = genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericResource);
59         Set<String> genericInputsNames = genericInputs.stream().map(InputDefinition::getName).collect(Collectors.toSet());
60         return resourceInputs.stream().filter(input -> genericInputsNames.contains(input.getName())).collect(Collectors.toList());
61     }
62
63 }