aa58c142273b4cac6a15fe29e73173d0f80aeb70
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.components.validation;
18
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.regex.Pattern;
25 import java.util.stream.Collectors;
26
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.collections.MapUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.elasticsearch.common.Strings;
31 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.model.ComponentParametersView;
34 import org.openecomp.sdc.be.model.InterfaceDefinition;
35 import org.openecomp.sdc.be.model.Operation;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.exception.ResponseFormat;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 import fj.data.Either;
46
47 @Component("interfaceOperationValidation")
48 public class InterfaceOperationValidation {
49
50     @Autowired
51     private ToscaOperationFacade toscaOperationFacade;
52     private static final String TYPE_VALIDATION_REGEX = "^[a-zA-Z]{1,200}$";
53     private static final int DESCRIPTION_MAX_LENGTH = 200;
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationValidation.class);
56
57     public Either<Boolean, ResponseFormat> validateInterfaceOperations(
58             Collection<Operation> interfaceOperations,
59             String resourceId, boolean isUpdate) {
60
61         for(Operation interfaceOperation : interfaceOperations) {
62             Either<Boolean, ResponseFormat> interfaceOperationValidatorResponse = validateInterfaceOperation(
63                     interfaceOperation, resourceId, isUpdate);
64             if (interfaceOperationValidatorResponse.isRight()) {
65                 return interfaceOperationValidatorResponse;
66             }
67         }
68         return Either.left(Boolean.TRUE);
69     }
70
71     private Either<Boolean, ResponseFormat> validateInterfaceOperation(Operation interfaceOperation,
72                                                                        String resourceId, boolean isUpdate) {
73         ResponseFormatManager responseFormatManager = getResponseFormatManager();
74
75         Either<Boolean, ResponseFormat> interfaceOperationTypeResponse = isInterfaceOperationTypeValid(interfaceOperation,
76                 responseFormatManager, resourceId, isUpdate);
77         if (interfaceOperationTypeResponse.isRight()) {
78             return Either.right(interfaceOperationTypeResponse.right().value());
79         }
80
81         Either<Boolean, ResponseFormat> descriptionResponseEither = isValidDescription(responseFormatManager,
82                 interfaceOperation.getDescription());
83         if (descriptionResponseEither.isRight()) {
84             return Either.right(descriptionResponseEither.right().value());
85         }
86
87         Either<Boolean, ResponseFormat> inputParametersResponse = validateInputParameters(interfaceOperation,
88                 responseFormatManager);
89         if(inputParametersResponse.isRight()) {
90             return Either.right(inputParametersResponse.right().value());
91         }
92         return Either.left(Boolean.TRUE);
93     }
94
95     private Either<Boolean, ResponseFormat> isInterfaceOperationTypeValid(Operation interfaceOperation,
96                                                                           ResponseFormatManager responseFormatManager,
97                                                                           String resourceId, boolean isUpdate) {
98
99         Either<Boolean, ResponseFormat> operationTypeEmptyEither = isOperationTypeEmpty(responseFormatManager,
100                 interfaceOperation.getName());  // treating name as type for now
101         if (operationTypeEmptyEither.isRight()) {
102             return Either.right(operationTypeEmptyEither.right().value());
103         }
104
105         Either<Boolean, ResponseFormat> operationTypeRegexValidationResponse =
106                 isOperationTypeRegexValid(responseFormatManager, interfaceOperation.getName());
107         if (operationTypeRegexValidationResponse.isRight()) {
108             return Either.right(operationTypeRegexValidationResponse.right().value());
109         }
110
111         Either<Boolean, ResponseFormat> operationTypeUniqueResponse = validateOperationTypeUnique(interfaceOperation,
112                 resourceId, isUpdate, responseFormatManager );
113         if(operationTypeUniqueResponse.isRight()) {
114             return Either.right(operationTypeUniqueResponse.right().value());
115         }
116         if (!operationTypeUniqueResponse.left().value()) {
117             LOGGER.error("Interface Operation type  {} already in use ", interfaceOperation.getName());
118             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
119                     .INTERFACE_OPERATION_TYPE_ALREADY_IN_USE, interfaceOperation.getName());
120             return Either.right(errorResponse);
121         }
122         return Either.left(Boolean.TRUE);
123     }
124
125     private Either<Boolean, ResponseFormat> isOperationTypeRegexValid(ResponseFormatManager responseFormatManager,
126                                                                       String operationType) {
127         if (!isValidOperationType(operationType)) {
128             LOGGER.error("Interface Operation type {} is invalid, Operation type should not contain" +
129                     "Special character, space, numbers and  should not be greater than 200 characters", operationType);
130             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
131                     .INTERFACE_OPERATION_TYPE_INVALID, operationType);
132             return Either.right(errorResponse);
133         }
134         return Either.left(Boolean.TRUE);
135     }
136
137     private Either<Boolean, ResponseFormat> isOperationTypeEmpty(ResponseFormatManager responseFormatManager,
138                                                                  String operationType) {
139         if (StringUtils.isEmpty(operationType)) {
140             LOGGER.error("Interface Operation type is mandatory");
141             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
142                     .INTERFACE_OPERATION_TYPE_MANDATORY);
143             return Either.right(errorResponse);
144         }
145         return Either.left(Boolean.TRUE);
146     }
147
148     private Either<Boolean, ResponseFormat> isValidDescription(ResponseFormatManager responseFormatManager,
149                                                                String description) {
150         if (!Strings.isNullOrEmpty(description) && description.length() > DESCRIPTION_MAX_LENGTH) {
151             LOGGER.error("Interface Operation description {} is invalid, maximum 200 characters allowed", description);
152             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
153                     .INTERFACE_OPERATION_DESCRIPTION_MAX_LENGTH);
154             return Either.right(errorResponse);
155         }
156         return Either.left(Boolean.TRUE);
157     }
158
159     private boolean isValidOperationType(String operationType) {
160         return Pattern.matches(TYPE_VALIDATION_REGEX, operationType);
161     }
162
163     private Either<Boolean, ResponseFormat> validateOperationTypeUnique(
164             Operation interfaceOperation,
165             String resourceId,
166             boolean isUpdate,
167             ResponseFormatManager responseFormatManager) {
168         boolean isOperationTypeUnique = false;
169         ComponentParametersView filter = new ComponentParametersView(true);
170         filter.setIgnoreInterfaces(false);
171         Either<Resource, StorageOperationStatus> interfaceOperationOrigin = toscaOperationFacade
172                 .getToscaElement(resourceId, filter);
173         if (interfaceOperationOrigin.isRight()){
174             LOGGER.error("Failed to fetch interface operation information by resource id {} ", resourceId);
175             return Either.right(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR));
176         }
177
178         Collection<InterfaceDefinition> interfaceDefinitions = interfaceOperationOrigin.left().value()
179                 .getInterfaces().values();
180         if(CollectionUtils.isEmpty(interfaceDefinitions)){
181             isOperationTypeUnique = true;
182             return Either.left(isOperationTypeUnique);
183         }
184         Collection<Operation> allOperations = interfaceDefinitions.stream()
185                 .filter(a -> MapUtils.isNotEmpty(a.getOperationsMap()))
186                 .map(a -> a.getOperationsMap().values()).flatMap(Collection::stream)
187                 .collect(Collectors.toList());
188         if(CollectionUtils.isEmpty(allOperations)){
189             isOperationTypeUnique = true;
190             return Either.left(isOperationTypeUnique);
191         }
192
193         Map<String, String> operationTypes = new HashMap<>();
194         allOperations.forEach(operationType -> operationTypes.put(operationType.getUniqueId(),
195                 operationType.getName()) );
196
197         if (isUpdate){
198             isOperationTypeUnique = validateOperationTypeUniqueForUpdate(interfaceOperation, operationTypes);
199         }
200         else
201         if (!operationTypes.values().contains(interfaceOperation.getName())){
202             isOperationTypeUnique = true;
203         }
204         return Either.left(isOperationTypeUnique);
205     }
206     private Either<Boolean, ResponseFormat> validateInputParameters(Operation interfaceOperation,
207                                                                     ResponseFormatManager responseFormatManager) {
208         if (isInputParameterNameEmpty(interfaceOperation)) {
209             LOGGER.error("Interface operation input  parameter name can't be empty");
210             ResponseFormat inputResponse = responseFormatManager.getResponseFormat(ActionStatus
211                     .INTERFACE_OPERATION_INPUT_NAME_MANDATORY);
212             return Either.right(inputResponse);
213         }
214
215         Either<Boolean, Set<String>> validateInputParametersUniqueResponse = isInputParametersUnique(interfaceOperation);
216         if(validateInputParametersUniqueResponse.isRight()) {
217             LOGGER.error("Interface operation input  parameter names {} already in use",
218                     validateInputParametersUniqueResponse.right().value().toString());
219             ResponseFormat inputResponse = responseFormatManager.getResponseFormat(ActionStatus
220                     .INTERFACE_OPERATION_INPUT_NAME_ALREADY_IN_USE, validateInputParametersUniqueResponse.right().value().toString());
221             return Either.right(inputResponse);
222         }
223         return Either.left(Boolean.TRUE);
224     }
225
226     private Either<Boolean, Set<String>> isInputParametersUnique(Operation operationDataDefinition) {
227         Set<String> inputParamNamesSet = new HashSet<>();
228         Set<String> duplicateParamNamesToReturn = new HashSet<>();
229         operationDataDefinition.getInputs().getListToscaDataDefinition()
230                 .forEach(inputParam -> {
231                     if(!inputParamNamesSet.add(inputParam.getName().trim())) {
232                         duplicateParamNamesToReturn.add(inputParam.getName().trim());
233                     }
234                 });
235         if(!duplicateParamNamesToReturn.isEmpty()) {
236             return Either.right(duplicateParamNamesToReturn);
237         }
238         return Either.left(Boolean.TRUE);
239     }
240
241     private Boolean isInputParameterNameEmpty(Operation operationDataDefinition) {
242         return operationDataDefinition.getInputs().getListToscaDataDefinition().stream()
243                 .anyMatch(inputParam -> inputParam.getName() == null || inputParam.getName().trim().equals(StringUtils.EMPTY));
244     }
245
246
247     private boolean validateOperationTypeUniqueForUpdate(Operation interfaceOperation,
248                                                          Map<String, String> operationTypes) {
249         boolean isOperationTypeUnique = false;
250         for(Map.Entry<String, String> entry : operationTypes.entrySet()){
251             if (entry.getKey().equals(interfaceOperation.getUniqueId()) && entry.getValue().
252                     equals(interfaceOperation.getName())) {
253                 isOperationTypeUnique = true;
254             }
255
256             if(entry.getKey().equals(interfaceOperation.getUniqueId()) && !operationTypes.values()
257                     .contains(interfaceOperation.getName())){
258                 isOperationTypeUnique = true;
259             }
260         }
261         return isOperationTypeUnique;
262     }
263
264     protected ResponseFormatManager getResponseFormatManager() {
265         return ResponseFormatManager.getInstance();
266     }
267
268 }