package org.onap.policy.pap.xacml.rest.policycontroller;
import java.io.File;
+import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@RequestMapping(value="/policycreation/save_policy", method = RequestMethod.POST)
@ResponseBody
- public ResponseEntity<String> savePolicy(@RequestBody PolicyRestAdapter policyData, HttpServletResponse response) throws Exception{
+ public ResponseEntity<String> savePolicy(@RequestBody PolicyRestAdapter policyData, HttpServletResponse response){
String body = null;
HttpStatus status = HttpStatus.BAD_REQUEST;
Map<String, String> successMap = new HashMap<>();
policyData.setTtlDate("NA");
}else{
String dateTTL = policyData.getTtlDate();
- String newDate = convertDate(dateTTL, false);
+ String newDate = convertDate(dateTTL);
policyData.setTtlDate(newDate);
}
String policyType = policyData.getPolicyType();
String filePrefix = null;
- if (policyType.equalsIgnoreCase("Config")) {
+ if ("Config".equalsIgnoreCase(policyType)) {
policyConfigType = policyData.getConfigPolicyType();
- if (policyConfigType.equalsIgnoreCase("Firewall Config")) {
+ if ("Firewall Config".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_FW_";
- }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_Fault")) {
+ }else if ("ClosedLoop_Fault".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_Fault_";
- }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_PM")) {
+ }else if ("ClosedLoop_PM".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_PM_";
- }else if (policyConfigType.equalsIgnoreCase("Micro Service")) {
+ }else if ("Micro Service".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_MS_";
- }else if (policyConfigType.equalsIgnoreCase("BRMS_Raw")) {
+ }else if ("BRMS_Raw".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_BRMS_Raw_";
- }else if (policyConfigType.equalsIgnoreCase("BRMS_Param")) {
+ }else if ("BRMS_Param".equalsIgnoreCase(policyConfigType)) {
filePrefix = "Config_BRMS_Param_";
}else {
filePrefix = "Config_";
}
- } else if (policyType.equalsIgnoreCase("Action")) {
+ } else if ("Action".equalsIgnoreCase(policyType)) {
filePrefix = "Action_";
- } else if (policyType.equalsIgnoreCase("Decision")) {
+ } else if ("Decision".equalsIgnoreCase(policyType)) {
filePrefix = "Decision_";
}
int version = 0;
int highestVersion = 0;
- String createdBy = "";
- String modifiedBy = userId;
+ String createdBy;
+ String modifiedBy;
String scopeCheck = policyData.getDomainDir().replace(".", File.separator);
PolicyEditorScopes policyEditorScope = (PolicyEditorScopes) commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", scopeCheck);
if(policyEditorScope == null){
policyVersionDao.setActiveVersion(version);
policyVersionDao.setHigherVersion(version);
policyVersionDao.setModifiedBy(modifiedBy);
+ policyVersionDao.setModifiedDate(new Date());
}else{
body = "policyExists";
status = HttpStatus.CONFLICT;
response.setStatus(HttpServletResponse.SC_CONFLICT);
response.addHeader("error", "policyExists");
response.addHeader("policyName", policyData.getPolicyName());
- return new ResponseEntity<String>(body, status);
+ return new ResponseEntity<>(body, status);
}
}else{
+ // if policy does not exist and the request is updatePolicy return error
+ if(policyData.isEditPolicy()){
+ body = "policyNotAvailableForEdit";
+ status = HttpStatus.NOT_FOUND;
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ response.addHeader("error", body);
+ response.addHeader("message", policyData.getPolicyName() + " does not exist on the PAP and cannot be updated.");
+ return new ResponseEntity<>(body, status);
+ }
version = 1;
if(userId == null){
createdBy = "API";
policyData.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
if(policyData.getApiflag() == null){
//set the Rule Combining Algorithm Id to be sent to PAP-REST via JSON
- if(policyData.getAttributes() != null){
- if(policyData.getAttributes().size() > 0){
- for(Object attribute : policyData.getAttributes()){
- if(attribute instanceof LinkedHashMap<?, ?>){
- String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
- String value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
- attributeMap.put(key, value);
- }
+ if(policyData.getAttributes() != null && !policyData.getAttributes().isEmpty()){
+ for(Object attribute : policyData.getAttributes()){
+ if(attribute instanceof LinkedHashMap<?, ?>){
+ String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
+ String value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
+ attributeMap.put(key, value);
}
}
}
if(policyData.getApiflag() == null){
// If there is any dynamic field create the matches here
String key="templateName";
- String value=(String) policyData.getRuleName();
+ String value= policyData.getRuleName();
drlRuleAndUIParams.put(key, value);
if(policyData.getRuleData().size() > 0){
for(Object keyValue: policyData.getRuleData().keySet()){
List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
- if(policyData.getRuleAlgorithmschoices().size() > 0){
+ if(!policyData.getRuleAlgorithmschoices().isEmpty()){
for(Object attribute : policyData.getRuleAlgorithmschoices()){
if(attribute instanceof LinkedHashMap<?, ?>){
String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
String actionDictValue = policyData.getActionAttributeValue();
ActionPolicyDict jsonData = ((ActionPolicyDict) commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", actionDictValue));
- String actionBodyString = jsonData.getBody();
- String actionDictHeader = jsonData.getHeader();
- String actionDictType = jsonData.getType();
- String actionDictUrl = jsonData.getUrl();
- String actionDictMethod = jsonData.getMethod();
- policyData.setActionDictHeader(actionDictHeader);
- policyData.setActionDictType(actionDictType);
- policyData.setActionDictUrl(actionDictUrl);
- policyData.setActionDictMethod(actionDictMethod);
+ if(jsonData!=null){
+ String actionBodyString = jsonData.getBody();
+ String actionDictHeader = jsonData.getHeader();
+ String actionDictType = jsonData.getType();
+ String actionDictUrl = jsonData.getUrl();
+ String actionDictMethod = jsonData.getMethod();
+ policyData.setActionDictHeader(actionDictHeader);
+ policyData.setActionDictType(actionDictType);
+ policyData.setActionDictUrl(actionDictUrl);
+ policyData.setActionDictMethod(actionDictMethod);
+ if (actionBodyString != null) {
+ policyData.setActionBody(actionBodyString);
+ }
+ }
policyData.setActionAttribute(actionDictValue);
policyData.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
policyData.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
policyData.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
policyData.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
- if (actionBodyString != null) {
- policyData.setActionBody(actionBodyString);
+ }else{
+ // API request.
+ String comboDictValue = policyData.getActionAttribute();
+ ActionPolicyDict jsonData = ((ActionPolicyDict) commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", comboDictValue));
+ if(jsonData!=null){
+ policyData.setActionBody(jsonData.getBody());
+ policyData.setActionDictHeader(jsonData.getHeader());
+ policyData.setActionDictType(jsonData.getType());
+ policyData.setActionDictUrl(jsonData.getUrl());
+ policyData.setActionDictMethod(jsonData.getMethod());
}
}
- newPolicy = new ActionPolicy(policyData);
+ newPolicy = new ActionPolicy(policyData, commonClassDao);
} else if (policyType.equalsIgnoreCase("Decision")) {
if(policyData.getApiflag() == null){
Map<String, String> settingsMap = new HashMap<>();
List<String> errorCodeList = new LinkedList<>();
List<String> treatmentList = new LinkedList<>();
- if(policyData.getSettings().size() > 0){
+ if(!policyData.getSettings().isEmpty()){
for(Object settingsData : policyData.getSettings()){
if(settingsData instanceof LinkedHashMap<?, ?>){
String key = ((LinkedHashMap<?, ?>) settingsData).get("key").toString();
policyData.setErrorCodeList(errorCodeList);
policyData.setTreatmentList(treatmentList);
}
- newPolicy = new DecisionPolicy(policyData);
+ newPolicy = new DecisionPolicy(policyData, commonClassDao);
}
if(newPolicy != null){
status = HttpStatus.INTERNAL_SERVER_ERROR;
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.addHeader("error", "error");
- return new ResponseEntity<String>(body, status);
+ return new ResponseEntity<>(body, status);
}
PolicyDBDaoTransaction policyDBDaoTransaction = null;