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