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.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
25 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.common.util.ValidationUtils;
34 import org.openecomp.sdc.exception.ResponseFormat;
36 import java.util.Arrays;
38 @org.springframework.stereotype.Component
39 public class ComponentIconValidator implements ComponentFieldValidator {
41 private static final Logger log = Logger.getLogger(ComponentIconValidator.class.getName());
42 private static final String DEFAULT_ICON = "defaulticon";
43 private ComponentsUtils componentsUtils;
45 public ComponentIconValidator(ComponentsUtils componentsUtils) {
46 this.componentsUtils = componentsUtils;
50 public void validateAndCorrectField(User user, Component component, AuditingActionEnum actionEnum) {
51 log.debug("validate Icon");
52 ComponentTypeEnum type = component.getComponentType();
53 String icon = component.getIcon();
54 if (StringUtils.isEmpty(icon)) {
55 log.info("icon is missing.");
56 component.setIcon(DEFAULT_ICON);
59 if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
60 validateAndSetDefaultIcon(icon, component);
62 validateIcon(icon,component.getComponentType());
65 } catch(ComponentException e){
66 ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat()
67 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
68 componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, type);
73 private void validateAndSetDefaultIcon(String icon, org.openecomp.sdc.be.model.Component componnet) {
75 if (componnet.getCategories().get(0).getIcons() == null) {
76 componnet.getCategories().get(0).setIcons(Arrays.asList(DEFAULT_ICON));
79 if (componnet.getCategories().get(0).getIcons().contains(icon)) {
83 } catch (NullPointerException exp) {
84 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY,
85 ComponentTypeEnum.SERVICE.getValue());
87 componnet.setIcon(DEFAULT_ICON);
90 private void validateIcon(String icon, ComponentTypeEnum type) {
92 if (!ValidationUtils.validateIconLength(icon)) {
93 log.debug("icon exceeds max length");
94 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT, type.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
97 if (!ValidationUtils.validateIcon(icon)) {
98 log.info("icon is invalid.");
99 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_ICON, type.getValue());