8321505de5b6551f130f9cb4e0d31954f20ac744
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / CreateBRMSRuleTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
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;
35
36 @Service
37 public class CreateBRMSRuleTemplate {
38     private static final Logger LOGGER = FlexLogger.getLogger(CreateBRMSRuleTemplate.class);
39     private static CommonClassDao commonClassDao;
40
41     @Autowired
42     public CreateBRMSRuleTemplate(CommonClassDao commonClassDao) {
43         CreateBRMSRuleTemplate.commonClassDao = commonClassDao;
44     }
45
46     public CreateBRMSRuleTemplate() {
47     }
48
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");
57                 return responseMap;
58             } else {
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);
67             }
68             responseMap.put("success", "success");
69         } else {
70             LOGGER.debug("Error during validating the rule for creating record for BRMS Param Template");
71             responseMap.put("error", "VALIDATION");
72         }
73         return responseMap;
74     }
75
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))) {
81                 return false;
82             }
83         }
84         return true;
85     }
86 }