3ca768bad2f4416791e31d883ee87694eb7de218
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / component / ComponentNameValidator.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 package org.openecomp.sdc.be.components.validation.component;
21
22 import fj.data.Either;
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.ByResponseFormatComponentException;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.common.util.ValidationUtils;
39 import org.openecomp.sdc.exception.ResponseFormat;
40
41 @org.springframework.stereotype.Component
42 public class ComponentNameValidator implements ComponentFieldValidator {
43
44     private static final Logger log = Logger.getLogger(ComponentNameValidator.class.getName());
45     private ComponentsUtils componentsUtils;
46     private ToscaOperationFacade toscaOperationFacade;
47
48     public ComponentNameValidator(ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade) {
49         this.componentsUtils = componentsUtils;
50         this.toscaOperationFacade = toscaOperationFacade;
51     }
52
53     @Override
54     public void validateAndCorrectField(User user, Component component, AuditingActionEnum actionEnum) {
55         String componentName = component.getName();
56         if (StringUtils.isEmpty(componentName)) {
57             log.debug("component name is empty");
58             auditErrorAndThrow(user, component, actionEnum, ActionStatus.MISSING_COMPONENT_NAME);
59         }
60         if (!ValidationUtils.validateComponentNameLength(componentName)) {
61             log.debug("Component name exceeds max length {} ", ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
62             auditErrorAndThrow(user, component, actionEnum, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT);
63         }
64         if (!ValidationUtils.validateComponentNamePattern(componentName)) {
65             log.debug("Component name {} has invalid format", componentName);
66             auditErrorAndThrow(user, component, actionEnum, ActionStatus.INVALID_COMPONENT_NAME);
67         }
68         if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
69             validateComponentNameUnique(user, component, actionEnum);
70         }
71         //TODO remove assignment here
72         component.setNormalizedName(ValidationUtils.normaliseComponentName(componentName));
73         component.setSystemName(ValidationUtils.convertToSystemName(componentName));
74     }
75
76     public void validateComponentNameUnique(User user, Component component, AuditingActionEnum actionEnum) {
77         log.debug("validate component name uniqueness for: {}", component.getName());
78         ComponentTypeEnum type = component.getComponentType();
79         ResourceTypeEnum resourceType = null;
80         if (component instanceof Resource) {
81             resourceType = ((Resource) component).getResourceType();
82         }
83         Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
84             .validateComponentNameExists(component.getName(), resourceType, type);
85         if (dataModelResponse.isLeft()) {
86             if (dataModelResponse.left().value()) {
87                 log.info("Component with name {} already exists", component.getName());
88                 ResponseFormat errorResponse = componentsUtils
89                     .getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, type.getValue(), component.getName());
90                 componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, type);
91                 throw new ByResponseFormatComponentException(errorResponse);
92             }
93             return;
94         }
95         BeEcompErrorManager.getInstance().logBeSystemError("validateComponentNameUnique");
96         log.debug("Error while validateComponentNameUnique for component: {}", component.getName());
97         ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
98         componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, type);
99         throw new ByResponseFormatComponentException(errorResponse);
100     }
101
102     private void auditErrorAndThrow(User user, org.openecomp.sdc.be.model.Component component, AuditingActionEnum actionEnum,
103                                     ActionStatus actionStatus) {
104         ResponseFormat errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().getValue());
105         componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
106         throw new ByActionStatusComponentException(actionStatus, component.getComponentType().getValue());
107     }
108 }