2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.openecomp.sdc.be.components.validation.component;
23 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
24 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.ValidationUtils;
33 import org.openecomp.sdc.exception.ResponseFormat;
35 import java.util.ArrayList;
36 import java.util.List;
38 @org.springframework.stereotype.Component
39 public class ComponentTagsValidator implements ComponentFieldValidator {
41 private static final Logger log = Logger.getLogger(ComponentTagsValidator.class.getName());
42 private static final String TAG_FIELD_LABEL = "tag";
43 private ComponentsUtils componentsUtils;
45 public ComponentTagsValidator(ComponentsUtils componentsUtils) {
46 this.componentsUtils = componentsUtils;
50 public void validateAndCorrectField(User user, Component component, AuditingActionEnum actionEnum) {
51 List<String> tagsList = component.getTags();
53 validateComponentTags(tagsList, component.getName(), component.getComponentType(), user, component, actionEnum);
54 } catch(ComponentException e){
55 ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat()
56 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
57 componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, component.getComponentType());
60 ValidationUtils.removeDuplicateFromList(component.getTags());
63 protected void validateComponentTags(List<String> tags, String name, ComponentTypeEnum componentType, User user, org.openecomp.sdc.be.model.Component component, AuditingActionEnum action) {
64 log.debug("validate component tags");
65 boolean includesComponentName = false;
67 ResponseFormat responseFormat;
68 if (tags != null && !tags.isEmpty()) {
69 for (String tag : tags) {
70 validateTagLength(componentType, user, component, action, tag);
71 if (validateTagPattern(tag)) {
72 includesComponentName = isIncludesComponentName(name, includesComponentName, tag);
74 log.debug("invalid tag {}", tag);
75 responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_FIELD_FORMAT, componentType.getValue(), TAG_FIELD_LABEL);
76 componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
77 throw new ByActionStatusComponentException(ActionStatus.INVALID_FIELD_FORMAT, componentType.getValue(), TAG_FIELD_LABEL);
79 tagListSize += tag.length() + 1;
81 if (tagListSize > 0) {
85 if (!includesComponentName) {
86 log.debug("tags must include component name");
87 responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
88 componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
89 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
91 if (!ValidationUtils.validateTagListLength(tagListSize)) {
92 log.debug("overall tags length exceeds limit {}", ValidationUtils.TAG_LIST_MAX_LENGTH);
93 responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
94 componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
95 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
98 tags = new ArrayList<>();
100 component.setTags(tags);
104 private boolean isIncludesComponentName(String name, boolean includesComponentName, String tag) {
105 if (!includesComponentName) {
106 includesComponentName = name.equals(tag);
108 return includesComponentName;
111 private void validateTagLength(ComponentTypeEnum componentType, User user, org.openecomp.sdc.be.model.Component component, AuditingActionEnum action, String tag) {
112 ResponseFormat responseFormat;
113 if (!ValidationUtils.validateTagLength(tag)) {
114 log.debug("tag length exceeds limit {}", ValidationUtils.TAG_MAX_LENGTH);
115 responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH);
116 componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
117 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH);
121 protected boolean validateTagPattern(String tag) {
122 return ValidationUtils.validateTagPattern(tag);