2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.onap.policy.pap.xacml.rest.components;
23 import java.util.HashMap;
24 import java.util.List;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.rest.dao.CommonClassDao;
30 import org.onap.policy.rest.jpa.BRMSParamTemplate;
31 import org.onap.policy.rest.jpa.UserInfo;
32 import org.onap.policy.utils.PolicyUtils;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
37 public class CreateBRMSRuleTemplate {
38 private static final Logger LOGGER = FlexLogger.getLogger(CreateBRMSRuleTemplate.class);
39 private static CommonClassDao commonClassDao;
42 public CreateBRMSRuleTemplate(CommonClassDao commonClassDao) {
43 CreateBRMSRuleTemplate.commonClassDao = commonClassDao;
46 public CreateBRMSRuleTemplate() {
49 public Map<String, String> addRule(String rule, String ruleName, String description, String userID) {
50 Map<String, String> responseMap = new HashMap<>();
51 if (rule != null && !PolicyUtils.brmsRawValidate(rule).contains("[ERR")) {
52 List<Object> duplicateData =
53 commonClassDao.checkDuplicateEntry(ruleName, "ruleName", BRMSParamTemplate.class);
54 if (duplicateData != null && !duplicateData.isEmpty()) {
55 LOGGER.error("Import new service failed. Service already exists");
56 responseMap.put("DBError", "EXISTS");
59 BRMSParamTemplate brmsParamTemplate = new BRMSParamTemplate();
60 brmsParamTemplate.setDescription(description);
61 brmsParamTemplate.setRuleName(ruleName);
62 brmsParamTemplate.setRule(rule);
63 UserInfo userCreatedBy = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", userID);
64 brmsParamTemplate.setUserCreatedBy(userCreatedBy);
65 commonClassDao.save(brmsParamTemplate);
66 LOGGER.info("Template created with " + ruleName + " by " + userID);
68 responseMap.put("success", "success");
70 LOGGER.debug("Error during validating the rule for creating record for BRMS Param Template");
71 responseMap.put("error", "VALIDATION");
76 public static boolean validateRuleParams(String rule) {
77 CreateBrmsParamPolicy policy = new CreateBrmsParamPolicy();
78 Map<String, String> paramValues = policy.findType(rule);
79 for (String key : paramValues.keySet()) {
80 if (!PolicyUtils.SUCCESS.equals(PolicyUtils.policySpecialCharValidator(key))) {