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.apache.commons.lang3.StringUtils;
27 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
28 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Service;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.category.CategoryDefinition;
35 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
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;
41 @org.springframework.stereotype.Component
42 public class ServiceCategoryValidator implements ServiceFieldValidator {
44 private static final Logger log = Logger.getLogger(ServiceCategoryValidator.class.getName());
45 protected IElementOperation elementDao;
46 private ComponentsUtils componentsUtils;
48 public ServiceCategoryValidator(ComponentsUtils componentsUtils, IElementOperation elementDao) {
49 this.componentsUtils = componentsUtils;
50 this.elementDao = elementDao;
54 public void validateAndCorrectField(User user, Service service, AuditingActionEnum actionEnum) {
55 log.debug("validate Service category");
56 if (isEmpty(service.getCategories())) {
57 ResponseFormat errorResponse = componentsUtils
58 .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);
70 private Either<Boolean, ResponseFormat> validateServiceCategory(List<CategoryDefinition> list) {
72 if (list.size() > 1) {
73 log.debug("Must be only one category for service");
74 ResponseFormat responseFormat = componentsUtils
75 .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 (StringUtils.isEmpty(category.getName())) {
85 log.debug("Resource category is empty");
86 ResponseFormat responseFormat = componentsUtils
87 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
88 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);