2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
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;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
34 @org.springframework.stereotype.Component("dataTypeBusinessLogic")
35 public class DataTypeBusinessLogic extends BaseBusinessLogic {
38 * Get a list of data types that the Component has.
40 * @param componentId Unique ID of the Component
41 * @return list of data types
43 public Either<List<DataTypeDefinition>, StorageOperationStatus> getPrivateDataTypes(String componentId) {
44 ComponentParametersView filter = new ComponentParametersView();
46 filter.setIgnoreDataType(false);
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());
54 Component component = componentResult.left().value();
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<>();
63 return Either.left(dataTypesToReturn);
67 * Get a data type in a Component
69 * @param componentId Unique ID of the Component
70 * @param dataTypeName Data type name
71 * @return found data type
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());
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);
83 return Either.left(findResult.get());
87 * Delete a data type from the Component.
89 * @param componentId Unique ID of the Component
90 * @param dataTypeName Data type name to be deleted
91 * @return deleted data type
93 public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(String componentId, String dataTypeName) {
94 ComponentParametersView filter = new ComponentParametersView();
96 filter.setIgnoreDataType(false);
98 // Get Component object
99 Either<? extends Component, StorageOperationStatus> componentResult =
100 toscaOperationFacade.getToscaElement(componentId, filter);
101 if (componentResult.isRight()) {
103 return Either.right(componentResult.right().value());
106 return deletePrivateDataType(componentResult.left().value(), dataTypeName);
110 * Delete a data type from the Component.
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
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);
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);
130 StorageOperationStatus deleteResult = toscaOperationFacade.deleteDataTypeOfComponent(component, dataTypeName);
131 if (deleteResult != StorageOperationStatus.OK) {
132 return Either.right(deleteResult);
135 // return deleted data type if ok
136 return Either.left(dataTypeResult.get());