2 * ============LICENSE_START=======================================================
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
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.onap.sdc.dcae.rule.editor.validators;
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;
31 import java.util.List;
33 public class MapActionValidator extends CopyActionValidator<MapAction> {
35 private static MapActionValidator mapActionValidator = new MapActionValidator();
37 public static MapActionValidator getInstance() {
38 return mapActionValidator;
41 private MapActionValidator(){}
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())) {
48 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_ENTRY, null, action.getTarget()));
50 if (action.mapHasDefault() && !ValidationUtils.validateNotEmpty(action.getMapDefaultValue())) {
52 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_DEFAULT_VALUE, null, action.getTarget()));
55 if (!validateMapValues(action)) {
57 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.MISSING_ENTRY, null, action.getTarget()));
59 } catch (IllegalStateException err) {
61 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Map validation error: {}", err);
62 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.DUPLICATE_KEY, null));
68 private boolean validateMapValues(MapAction action) {
69 return action.transformToMap().entrySet().stream().allMatch(p -> ValidationUtils.validateNotEmpty(p.getKey()) && ValidationUtils.validateNotEmpty(p.getValue()));