Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / CreateBRMSRuleTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
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 package org.openecomp.policy.pap.xacml.rest.components;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
27 import org.openecomp.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.policy.rest.dao.CommonClassDao;
29 import org.openecomp.policy.rest.jpa.BRMSParamTemplate;
30 import org.openecomp.policy.rest.jpa.UserInfo;
31 import org.openecomp.policy.utils.PolicyUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34
35 @Service
36 public class CreateBRMSRuleTemplate {
37         private static final Logger LOGGER  = FlexLogger.getLogger(CreateBRMSRuleTemplate.class);
38         private static CommonClassDao commonClassDao;
39         
40         @Autowired
41         public CreateBRMSRuleTemplate(CommonClassDao commonClassDao){
42                 CreateBRMSRuleTemplate.commonClassDao = commonClassDao;
43         }
44         
45         public CreateBRMSRuleTemplate() {}
46
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");
54                                 return responseMap;
55                         }else{
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);
64                         }
65                         responseMap.put("success", "success");
66                 }else{
67                         LOGGER.debug("Error during validating the rule for creating record for BRMS Param Template");
68                         responseMap.put("error", "VALIDATION");
69                 }
70                 return responseMap;
71         }
72         
73 }