Align INFO.yaml
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / rule / editor / validators / MapActionValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. 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.onap.sdc.dcae.rule.editor.validators;
22
23 import org.onap.sdc.common.onaplog.enums.LogLevel;
24 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.MapAction;
25 import org.onap.sdc.dcae.errormng.ActionStatus;
26 import org.onap.sdc.dcae.errormng.ErrConfMgr;
27 import org.onap.sdc.dcae.errormng.ResponseFormat;
28 import org.onap.sdc.dcae.rule.editor.utils.ValidationUtils;
29 import org.springframework.util.CollectionUtils;
30
31 import java.util.List;
32
33 public class MapActionValidator extends CopyActionValidator<MapAction> {
34
35         private static MapActionValidator mapActionValidator = new MapActionValidator();
36
37         public static MapActionValidator getInstance() {
38                 return mapActionValidator;
39         }
40
41         private MapActionValidator(){}
42
43         @Override
44         public boolean validate(MapAction action, List<ResponseFormat> errors) {
45                 boolean valid = super.validate(action, errors);
46                 if (action.getMap() == null || CollectionUtils.isEmpty(action.getMapValues())) {
47                         valid = false;
48                         errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_ENTRY, null, action.getTarget()));
49                 } else {
50                         if (action.mapHasDefault() && !ValidationUtils.validateNotEmpty(action.getMapDefaultValue())) {
51                                 valid = false;
52                                 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_DEFAULT_VALUE, null, action.getTarget()));
53                         }
54                         try {
55                                 if (!validateMapValues(action)) {
56                                         valid = false;
57                                         errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_ENTRY, null, action.getTarget()));
58                                 }
59                         } catch (IllegalStateException err) {
60                                 valid = false;
61                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Map validation error: {}", err);
62                                 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.DUPLICATE_KEY, null));
63                         }
64                 }
65                 return valid;
66         }
67
68         private boolean validateMapValues(MapAction action) {
69                 return action.transformToMap().entrySet().stream().allMatch(p -> ValidationUtils.validateNotEmpty(p.getKey()) && ValidationUtils.validateNotEmpty(p.getValue()));
70         }
71 }