Add PAP Policy parameter builder xacml policy
[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,
51                                       String date) {
52         this.policyParameters = policyParameters;
53         this.policyName = policyName;
54         this.policyScope = policyScope;
55         this.date = date;
56         papServices = new PAPServices();
57     }
58
59     public Boolean getValidation() {
60         if (policyParameters.getConfigBody() == null) {
61             message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Micro Service or Attributes Config Body Present";
62             return false;
63         }
64         try {
65             microServiceAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
66         } catch (JsonException | IllegalStateException e) {
67             message =
68                     XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " +
69                             policyParameters.getConfigBody();
70             LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e);
71             return false;
72         }
73         onapName = policyParameters.getOnapName();
74         if (onapName == null || onapName.trim().isEmpty()) {
75             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given.";
76             return false;
77         }
78         boolean levelCheck = false;
79         levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
80         if (!levelCheck) {
81             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
82             return false;
83         }
84         return true;
85     }
86
87     public String getMessage() {
88         return message;
89     }
90
91     public String getResult(boolean updateFlag) throws PolicyException {
92         String response = null;
93         String operation = null;
94         if (updateFlag) {
95             operation = "update";
96         } else {
97             operation = "create";
98         }
99         // get values and attributes from the JsonObject
100         String uuid = null;
101         String msLocation = null;
102         String configName = null;
103         String microService = null;
104         String policyDescription = null;
105         String priority = null;
106         String version = null;
107
108         if (microServiceAttributes.get("service") != null) {
109             microService = microServiceAttributes.get("service").toString().replace("\"", "");
110         }
111         if (microServiceAttributes.get("uuid") != null) {
112             uuid = microServiceAttributes.get("uuid").toString().replace("\"", "");
113         }
114         if (microServiceAttributes.get("location") != null) {
115             msLocation = microServiceAttributes.get("location").toString().replace("\"", "");
116         }
117         if (microServiceAttributes.get("configName") != null) {
118             configName = microServiceAttributes.get("configName").toString().replace("\"", "");
119         }
120         if (microServiceAttributes.containsKey("description")) {
121             policyDescription = microServiceAttributes.get("description").toString().replace("\"", "");
122         }
123         if (microServiceAttributes.containsKey("priority")) {
124             priority = microServiceAttributes.get("priority").toString().replace("\"", "");
125         }
126         if (microServiceAttributes.containsKey("version")) {
127             version = microServiceAttributes.get("version").toString().replace("\"", "");
128         }
129         // Create Policy. 
130         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Micro Service", policyName, policyDescription, onapName,
131                 configName, microService, uuid, msLocation, microServiceAttributes.toString(), priority,
132                 version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
133                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
134         // Send JSON Object to PAP. 
135         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
136                 "policyType=Config"}, policyParameters.getRequestID(), "ConfigMS");
137         LOGGER.info("Policy MS created Response: " + response);
138         return response;
139     }
140 }