Refactor to provide Common Policy Validation
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / MicroServicesPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.policy.pdp.rest.api.services;
21
22 import javax.json.JsonException;
23 import javax.json.JsonObject;
24
25 import org.onap.policy.api.PolicyException;
26 import org.onap.policy.api.PolicyParameters;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.onap.policy.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
32
33 /**
34  * MicroServices Policy implementation. 
35  * 
36  * @version 0.1
37  */
38 public class MicroServicesPolicyService{
39         private static final Logger LOGGER = FlexLogger.getLogger(MicroServicesPolicyService.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 String onapName = null;
48         private JsonObject microServiceAttributes = null;
49         
50         public MicroServicesPolicyService(String policyName, String policyScope, 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 Micro Service or Attributes Config Body Present";
61                         return false;
62                 }
63                 try{
64                         microServiceAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
65                 } catch(JsonException| IllegalStateException e){
66                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
67                         LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
68                         return false;
69                 }
70                 onapName = policyParameters.getOnapName();
71                 if (onapName==null||onapName.trim().isEmpty()){
72                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
73                         return false;
74                 }
75                 boolean levelCheck = false;
76                 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
77                 if (!levelCheck){
78                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
79                         return false;
80                 }
81                 return true;
82         }
83
84         public String getMessage() {
85                 return message;
86         }
87         
88         public String getResult(boolean updateFlag) throws PolicyException{
89             String response = null;
90         String operation = null;
91         if (updateFlag){
92             operation = "update";
93         } else {
94             operation = "create";
95         }
96         // get values and attributes from the JsonObject
97         String uuid = null;
98         String msLocation = null;
99         String configName = null;
100         String microService = null;
101         String policyDescription=null;
102         String priority=null;
103         String version=null;
104         
105         if (microServiceAttributes.get("service")!=null){
106                 microService = microServiceAttributes.get("service").toString().replace("\"", "");
107         }
108         if (microServiceAttributes.get("uuid")!=null){
109             uuid = microServiceAttributes.get("uuid").toString().replace("\"", "");
110         }
111         if (microServiceAttributes.get("location")!=null){
112             msLocation = microServiceAttributes.get("location").toString().replace("\"", "");
113         }
114         if (microServiceAttributes.get("configName")!=null){
115             configName = microServiceAttributes.get("configName").toString().replace("\"", "");
116         }
117         if(microServiceAttributes.containsKey("description")){
118                 policyDescription = microServiceAttributes.get("description").toString().replace("\"", "");
119         }
120         if(microServiceAttributes.containsKey("priority")){
121                 priority = microServiceAttributes.get("priority").toString().replace("\"", "");
122         }
123         if(microServiceAttributes.containsKey("version")){
124                 version = microServiceAttributes.get("version").toString().replace("\"", "");
125         }
126         // Create Policy. 
127         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Micro Service", policyName, policyDescription, onapName, 
128                 configName, microService, uuid, msLocation, microServiceAttributes.toString(), priority, 
129                 version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
130                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); 
131         // Send JSON Object to PAP. 
132         response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigMS");
133         LOGGER.info("Policy MS created Response: " + response);
134         return response;
135         }
136 }