[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / ClosedLoopPMPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-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.pdp.rest.api.services;
21
22 import javax.json.JsonException;
23 import javax.json.JsonObject;
24
25 import org.openecomp.policy.api.PolicyException;
26 import org.openecomp.policy.api.PolicyParameters;
27 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
28 import org.openecomp.policy.common.logging.flexlogger.Logger;
29 import org.openecomp.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
31 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
32
33 /**
34  * Closed Loop PM policy Implementation. 
35  * 
36  * @version 0.1
37  */
38 public class ClosedLoopPMPolicyService{
39         private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.class.getName());
40         
41         private PAPServices papServices = null;
42         private PolicyParameters policyParameters = null;
43         private String message = null;
44         private String policyName = null;
45         private String policyScope = null;
46         private String date = null;
47         private JsonObject configBody = null;
48         
49         public ClosedLoopPMPolicyService(String policyName, String policyScope,
50                         PolicyParameters policyParameters, String date) {
51                 this.policyParameters = policyParameters;
52                 this.policyName = policyName;
53                 this.policyScope = policyScope;
54                 this.date = date;
55                 papServices = new PAPServices();
56         }
57
58         public Boolean getValidation() {
59                 if(policyParameters.getConfigBody()==null){
60                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Config Body Present";
61                         return false;
62                 }
63                 if(!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())){
64                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
65                         return false;
66                 }
67                 try{
68                         configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
69                 } catch(JsonException| IllegalStateException e){
70                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
71                         LOGGER.error("Error during parsing JSON config body for Closed loop PM policy  " , e);
72                         return false;
73                 }
74                 return true;
75         }
76
77         public String getMessage() {
78                 return message;
79         }
80
81         public String getResult(boolean updateFlag) throws PolicyException{
82                 String response = null;
83                 String operation = null;
84                 if (updateFlag){
85                         operation = "update";
86                 } else {
87                         operation = "create";
88                 }
89                 // get values and attributes from the JsonObject
90                 if(!configBody.containsKey("ecompname")){
91                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Ecomp Name given.";
92                         LOGGER.error(message);
93                         return message;
94                 }
95                 if(!configBody.containsKey("serviceTypePolicyName")){
96                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given.";
97                         LOGGER.error(message);
98                         return message;
99                 }
100                 String ecompName = configBody.get("ecompname").toString().trim().replace("\"", "");
101                 if (ecompName==null||ecompName.trim().isEmpty()){
102                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Ecomp Name given.";
103                         LOGGER.error(message);
104                         return message;
105                 }
106                 boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
107                 if (!levelCheck){
108                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
109                         LOGGER.error(message);
110                         return message;
111                 }
112                 String jsonBody = configBody.toString();
113                 String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", "");
114                 // Create Policy. 
115                 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", policyName, policyParameters.getPolicyDescription(), ecompName, 
116                                 jsonBody, false, null, serviceType, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
117                                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); 
118                 //send JSON object to PAP
119                 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop");
120                 return response;
121         }
122 }