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.jsonjanusgraph.operations.ArtifactsOperations;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
29 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
30 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
31 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
32 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.util.CollectionUtils;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Optional;
42 @org.springframework.stereotype.Component("dataTypeBusinessLogic")
43 public class DataTypeBusinessLogic extends BaseBusinessLogic {
46 public DataTypeBusinessLogic(IElementOperation elementDao,
47 IGroupOperation groupOperation,
48 IGroupInstanceOperation groupInstanceOperation,
49 IGroupTypeOperation groupTypeOperation,
50 InterfaceOperation interfaceOperation,
51 InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
52 ArtifactsOperations artifactToscaOperation) {
53 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
54 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
58 * Get a list of data types that the Component has.
60 * @param componentId Unique ID of the Component
61 * @return list of data types
63 public Either<List<DataTypeDefinition>, StorageOperationStatus> getPrivateDataTypes(String componentId) {
64 ComponentParametersView filter = new ComponentParametersView();
66 filter.setIgnoreDataType(false);
68 // Get Component object
69 Either<? extends Component, StorageOperationStatus> componentResult =
70 toscaOperationFacade.getToscaElement(componentId, filter);
71 if (componentResult.isRight()) {
72 return Either.right(componentResult.right().value());
74 Component component = componentResult.left().value();
76 List<DataTypeDefinition> dataTypesToReturn = component.getDataTypes();
77 if (dataTypesToReturn == null) {
78 // this means there is no DATA_TYPES graph vertex.
79 // in this case, returns empty list.
80 dataTypesToReturn = new ArrayList<>();
83 return Either.left(dataTypesToReturn);
87 * Get a data type in a Component
89 * @param componentId Unique ID of the Component
90 * @param dataTypeName Data type name
91 * @return found data type
93 public Either<DataTypeDefinition, StorageOperationStatus> getPrivateDataType(String componentId, String dataTypeName) {
94 Either<List<DataTypeDefinition>, StorageOperationStatus> dataTypesResult = this.getPrivateDataTypes(componentId);
95 if (dataTypesResult.isRight()) {
96 return Either.right(dataTypesResult.right().value());
98 List<DataTypeDefinition> dataTypes = dataTypesResult.left().value();
99 Optional<DataTypeDefinition> findResult = dataTypes.stream().filter(e -> e.getName().equals(dataTypeName)).findAny();
100 if (!findResult.isPresent()) {
101 return Either.right(StorageOperationStatus.NOT_FOUND);
103 return Either.left(findResult.get());
107 * Delete a data type from the Component.
109 * @param componentId Unique ID of the Component
110 * @param dataTypeName Data type name to be deleted
111 * @return deleted data type
113 public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(String componentId, String dataTypeName) {
114 ComponentParametersView filter = new ComponentParametersView();
116 filter.setIgnoreDataType(false);
118 // Get Component object
119 Either<? extends Component, StorageOperationStatus> componentResult =
120 toscaOperationFacade.getToscaElement(componentId, filter);
121 if (componentResult.isRight()) {
123 return Either.right(componentResult.right().value());
126 return deletePrivateDataType(componentResult.left().value(), dataTypeName);
130 * Delete a data type from the Component.
132 * @param component Component object which has data types.
133 * needs to be fetched with componentParametersView.setIgnoreDataType(false)
134 * @param dataTypeName Data type name to be deleted
135 * @return deleted data type
137 public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(Component component, String dataTypeName) {
138 // check the specified data type exists
139 List<DataTypeDefinition> dataTypes = component.getDataTypes();
140 if (CollectionUtils.isEmpty(dataTypes)) {
141 return Either.right(StorageOperationStatus.NOT_FOUND);
143 Optional<DataTypeDefinition> dataTypeResult =
144 dataTypes.stream().filter(e -> e.getName().equals(dataTypeName)).findFirst();
145 if (!dataTypeResult.isPresent()) {
146 return Either.right(StorageOperationStatus.NOT_FOUND);
150 StorageOperationStatus deleteResult = toscaOperationFacade.deleteDataTypeOfComponent(component, dataTypeName);
151 if (deleteResult != StorageOperationStatus.OK) {
152 return Either.right(deleteResult);
155 // return deleted data type if ok
156 return Either.left(dataTypeResult.get());