Implement 'Update Service by importing Tosca Template'-story
[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             !AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE.equals(actionEnum) &&
70             !AuditingActionEnum.UPDATE_SERVICE_TOSCA_MODEL.equals(actionEnum)) {
71             validateComponentNameUnique(user, component, actionEnum);
72         }
73         //TODO remove assignment here
74         component.setNormalizedName(ValidationUtils.normaliseComponentName(componentName));
75         component.setSystemName(ValidationUtils.convertToSystemName(componentName));
76     }
77
78     public void validateComponentNameUnique(User user, Component component, AuditingActionEnum actionEnum) {
79         log.debug("validate component name uniqueness for: {}", component.getName());
80         ComponentTypeEnum type = component.getComponentType();
81         ResourceTypeEnum resourceType = null;
82         if (component instanceof Resource) {
83             resourceType = ((Resource) component).getResourceType();
84         }
85         Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
86             .validateComponentNameExists(component.getName(), resourceType, type);
87         if (dataModelResponse.isLeft()) {
88             if (dataModelResponse.left().value()) {
89                 log.info("Component with name {} already exists", component.getName());
90                 ResponseFormat errorResponse = componentsUtils
91                     .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,
105                                     ActionStatus actionStatus) {
106         ResponseFormat errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().getValue());
107         componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
108         throw new ByActionStatusComponentException(actionStatus, component.getComponentType().getValue());
109     }
110 }