500 error at WFD artifact-deliveries api
[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.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;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Optional;
41
42 @org.springframework.stereotype.Component("dataTypeBusinessLogic")
43 public class DataTypeBusinessLogic extends BaseBusinessLogic {
44
45     @Autowired
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);
55     }
56
57     /**
58      * Get a list of data types that the Component has.
59      *
60      * @param componentId Unique ID of the Component
61      * @return list of data types
62      */
63     public Either<List<DataTypeDefinition>, StorageOperationStatus> getPrivateDataTypes(String componentId) {
64         ComponentParametersView filter = new ComponentParametersView();
65         filter.disableAll();
66         filter.setIgnoreDataType(false);
67
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());
73         }
74         Component component = componentResult.left().value();
75
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<>();
81         }
82
83         return Either.left(dataTypesToReturn);
84     }
85
86     /**
87      * Get a data type in a Component
88      *
89      * @param componentId  Unique ID of the Component
90      * @param dataTypeName Data type name
91      * @return found data type
92      */
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());
97         }
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);
102         }
103         return Either.left(findResult.get());
104     }
105
106     /**
107      * Delete a data type from the Component.
108      *
109      * @param componentId  Unique ID of the Component
110      * @param dataTypeName Data type name to be deleted
111      * @return deleted data type
112      */
113     public Either<DataTypeDefinition, StorageOperationStatus> deletePrivateDataType(String componentId, String dataTypeName) {
114         ComponentParametersView filter = new ComponentParametersView();
115         filter.disableAll();
116         filter.setIgnoreDataType(false);
117
118         // Get Component object
119         Either<? extends Component, StorageOperationStatus> componentResult =
120                 toscaOperationFacade.getToscaElement(componentId, filter);
121         if (componentResult.isRight()) {
122             // not exists
123             return Either.right(componentResult.right().value());
124         }
125
126         return deletePrivateDataType(componentResult.left().value(), dataTypeName);
127     }
128
129     /**
130      * Delete a data type from the Component.
131      *
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
136      */
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);
142         }
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);
147         }
148
149         // delete it
150         StorageOperationStatus deleteResult = toscaOperationFacade.deleteDataTypeOfComponent(component, dataTypeName);
151         if (deleteResult != StorageOperationStatus.OK) {
152             return Either.right(deleteResult);
153         }
154
155         // return deleted data type if ok
156         return Either.left(dataTypeResult.get());
157     }
158 }