Change to enable SDC list type input
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / DataTypeBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Fujitsu Limited. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.model.Component;
25 import org.openecomp.sdc.be.model.ComponentParametersView;
26 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.springframework.util.CollectionUtils;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
33
34 @org.springframework.stereotype.Component("dataTypeBusinessLogic")
35 public class DataTypeBusinessLogic extends BaseBusinessLogic {
36
37     /**
38      * Get a list of data types that the Component has.
39      *
40      * @param componentId Unique ID of the Component
41      * @return list of data types
42      */
43     public Either<List<DataTypeDefinition>, StorageOperationStatus> getPrivateDataTypes(String componentId) {
44         ComponentParametersView filter = new ComponentParametersView();
45         filter.disableAll();
46         filter.setIgnoreDataType(false);
47
48         // Get Component object
49         Either<? extends Component, StorageOperationStatus> componentResult =
50                 toscaOperationFacade.getToscaElement(componentId, filter);
51         if (componentResult.isRight()) {
52             return Either.right(componentResult.right().value());
53         }
54         Component component = componentResult.left().value();
55
56         List<DataTypeDefinition> dataTypesToReturn = component.getDataTypes();
57         if (dataTypesToReturn == null) {
58             // this means there is no DATA_TYPES graph vertex.
59             // in this case, returns empty list.
60             dataTypesToReturn = new ArrayList<>();
61         }
62
63         return Either.left(dataTypesToReturn);
64     }
65
66     /**
67      * Get a data type in a Component
68      *
69      * @param componentId  Unique ID of the Component
70      * @param dataTypeName Data type name
71      * @return found data type
72      */
73     public Either<DataTypeDefinition, StorageOperationStatus> getPrivateDataType(String componentId, String dataTypeName) {
74         Either<List<DataTypeDefinition>, StorageOperationStatus> dataTypesResult = this.getPrivateDataTypes(componentId);
75         if (dataTypesResult.isRight()) {
76             return Either.right(dataTypesResult.right().value());
77         }
78         List<DataTypeDefinition> dataTypes = dataTypesResult.left().value();
79         Optional<DataTypeDefinition> findResult = dataTypes.stream().filter(e -> e.getName().equals(dataTypeName)).findAny();
80         if (!findResult.isPresent()) {
81             return Either.right(StorageOperationStatus.NOT_FOUND);
82         }
83         return Either.left(findResult.get());
84     }
85
86     /**
87      * Delete a data type from the Component.
88      *
89      * @param componentId  Unique ID of the Component
90      * @param dataTypeName Data type name to be deleted
91      * @return deleted data type
92      */
93     public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(String componentId, String dataTypeName) {
94         ComponentParametersView filter = new ComponentParametersView();
95         filter.disableAll();
96         filter.setIgnoreDataType(false);
97
98         // Get Component object
99         Either<? extends Component, StorageOperationStatus> componentResult =
100                 toscaOperationFacade.getToscaElement(componentId, filter);
101         if (componentResult.isRight()) {
102             // not exists
103             return Either.right(componentResult.right().value());
104         }
105
106         return deletePrivateDataType(componentResult.left().value(), dataTypeName);
107     }
108
109     /**
110      * Delete a data type from the Component.
111      *
112      * @param component    Component object which has data types.
113      *                     needs to be fetched with componentParametersView.setIgnoreDataType(false)
114      * @param dataTypeName Data type name to be deleted
115      * @return deleted data type
116      */
117     public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(Component component, String dataTypeName) {
118         // check the specified data type exists
119         List<DataTypeDefinition> dataTypes = component.getDataTypes();
120         if (CollectionUtils.isEmpty(dataTypes)) {
121             return Either.right(StorageOperationStatus.NOT_FOUND);
122         }
123         Optional<DataTypeDefinition> dataTypeResult =
124                 dataTypes.stream().filter(e -> e.getName().equals(dataTypeName)).findFirst();
125         if (!dataTypeResult.isPresent()) {
126             return Either.right(StorageOperationStatus.NOT_FOUND);
127         }
128
129         // delete it
130         StorageOperationStatus deleteResult = toscaOperationFacade.deleteDataTypeOfComponent(component, dataTypeName);
131         if (deleteResult != StorageOperationStatus.OK) {
132             return Either.right(deleteResult);
133         }
134
135         // return deleted data type if ok
136         return Either.left(dataTypeResult.get());
137     }
138 }