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