2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.onap.policy.pap.xacml.rest.components;
22 import java.util.HashMap;
23 import java.util.List;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.rest.dao.CommonClassDao;
29 import org.onap.policy.rest.jpa.BRMSParamTemplate;
30 import org.onap.policy.rest.jpa.UserInfo;
31 import org.onap.policy.utils.PolicyUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
36 public class CreateBRMSRuleTemplate {
37 private static final Logger LOGGER = FlexLogger.getLogger(CreateBRMSRuleTemplate.class);
38 private static CommonClassDao commonClassDao;
41 public CreateBRMSRuleTemplate(CommonClassDao commonClassDao){
42 CreateBRMSRuleTemplate.commonClassDao = commonClassDao;
45 public CreateBRMSRuleTemplate() {}
47 public Map<String, String> addRule(String rule, String ruleName, String description, String userID) {
48 Map<String,String> responseMap = new HashMap<>();
49 if(rule!=null && !PolicyUtils.brmsRawValidate(rule).contains("[ERR")){
50 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(ruleName, "ruleName", BRMSParamTemplate.class);
51 if(duplicateData!=null && !duplicateData.isEmpty()){
52 LOGGER.error("Import new service failed. Service already exists");
53 responseMap.put("DBError", "EXISTS");
56 BRMSParamTemplate brmsParamTemplate = new BRMSParamTemplate();
57 brmsParamTemplate.setDescription(description);
58 brmsParamTemplate.setRuleName(ruleName);
59 brmsParamTemplate.setRule(rule);
60 UserInfo userCreatedBy = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", userID);
61 brmsParamTemplate.setUserCreatedBy(userCreatedBy);
62 commonClassDao.save(brmsParamTemplate);
63 LOGGER.info("Template created with " + ruleName + " by " + userID);
65 responseMap.put("success", "success");
67 LOGGER.debug("Error during validating the rule for creating record for BRMS Param Template");
68 responseMap.put("error", "VALIDATION");