Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / component / ComponentIconValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.openecomp.sdc.be.components.validation.component;
22
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;
35
36 import java.util.Arrays;
37
38 @org.springframework.stereotype.Component
39 public class ComponentIconValidator implements ComponentFieldValidator {
40
41     private static final Logger log = Logger.getLogger(ComponentIconValidator.class.getName());
42     private static final String DEFAULT_ICON = "defaulticon";
43     private ComponentsUtils componentsUtils;
44
45     public ComponentIconValidator(ComponentsUtils componentsUtils) {
46         this.componentsUtils = componentsUtils;
47     }
48
49     @Override
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);
57         }
58         try {
59             if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
60                 validateAndSetDefaultIcon(icon, component);
61             } else {
62                 validateIcon(icon,component.getComponentType());
63             }
64
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);
69             throw e;
70         }
71     }
72
73     private void validateAndSetDefaultIcon(String icon, org.openecomp.sdc.be.model.Component componnet) {
74         try {
75             if (componnet.getCategories().get(0).getIcons() == null) {
76                 componnet.getCategories().get(0).setIcons(Arrays.asList(DEFAULT_ICON));
77             }
78             if (icon != null) {
79                 if (componnet.getCategories().get(0).getIcons().contains(icon)) {
80                     return;
81                 }
82             }
83         } catch (NullPointerException exp) {
84             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY,
85                     ComponentTypeEnum.SERVICE.getValue());
86         }
87         componnet.setIcon(DEFAULT_ICON);
88     }
89
90     private void validateIcon(String icon, ComponentTypeEnum type) {
91         if (icon != null) {
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);
95             }
96
97             if (!ValidationUtils.validateIcon(icon)) {
98                 log.info("icon is invalid.");
99                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_ICON, type.getValue());
100             }
101         }
102     }
103 }