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=========================================================
20 package org.openecomp.sdc.be.components.validation.service;
22 import static org.apache.commons.collections.CollectionUtils.isEmpty;
24 import fj.data.Either;
25 import java.util.List;
26 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
27 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.Service;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.model.category.CategoryDefinition;
34 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.openecomp.sdc.common.util.ValidationUtils;
38 import org.openecomp.sdc.exception.ResponseFormat;
40 @org.springframework.stereotype.Component
41 public class ServiceCategoryValidator implements ServiceFieldValidator {
43 private static final Logger log = Logger.getLogger(ServiceCategoryValidator.class.getName());
44 protected IElementOperation elementDao;
45 private ComponentsUtils componentsUtils;
47 public ServiceCategoryValidator(ComponentsUtils componentsUtils, IElementOperation elementDao) {
48 this.componentsUtils = componentsUtils;
49 this.elementDao = elementDao;
53 public void validateAndCorrectField(User user, Service service, AuditingActionEnum actionEnum) {
54 log.debug("validate Service category");
55 if (isEmpty(service.getCategories())) {
56 ResponseFormat errorResponse = componentsUtils
57 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
58 componentsUtils.auditComponentAdmin(errorResponse, user, service, actionEnum, ComponentTypeEnum.SERVICE);
59 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
61 Either<Boolean, ResponseFormat> validatCategory = validateServiceCategory(service.getCategories());
62 if (validatCategory.isRight()) {
63 ResponseFormat responseFormat = validatCategory.right().value();
64 componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE);
65 throw new ByResponseFormatComponentException(responseFormat);
69 private Either<Boolean, ResponseFormat> validateServiceCategory(List<CategoryDefinition> list) {
71 if (list.size() > 1) {
72 log.debug("Must be only one category for service");
73 ResponseFormat responseFormat = componentsUtils
74 .getResponseFormat(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.SERVICE.getValue());
75 return Either.right(responseFormat);
77 CategoryDefinition category = list.get(0);
78 if (category.getSubcategories() != null) {
79 log.debug("Subcategories cannot be defined for service");
80 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.SERVICE_CANNOT_CONTAIN_SUBCATEGORY);
81 return Either.right(responseFormat);
83 if (!ValidationUtils.validateStringNotEmpty(category.getName())) {
84 log.debug("Resource category is empty");
85 ResponseFormat responseFormat = componentsUtils
86 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
87 return Either.right(responseFormat);
89 log.debug("validating service category {} against valid categories list", list);
90 Either<List<CategoryDefinition>, ActionStatus> categorys = elementDao.getAllServiceCategories();
91 if (categorys.isRight()) {
92 log.debug("failed to retrive service categories from JanusGraph");
93 ResponseFormat responseFormat = componentsUtils.getResponseFormat(categorys.right().value());
94 return Either.right(responseFormat);
96 List<CategoryDefinition> categoryList = categorys.left().value();
97 for (CategoryDefinition value : categoryList) {
98 if (value.getName().equals(category.getName())) {
99 return Either.left(true);
102 log.debug("Category {} is not part of service category group. Service category valid values are {}", list, categoryList);
103 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.SERVICE.getValue()));
105 return Either.left(false);