Merge "Improve policy Config"
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / PolicyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client.req.policy;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36
37 import javax.ws.rs.BadRequestException;
38
39 import org.onap.clamp.clds.config.ClampProperties;
40 import org.onap.clamp.clds.config.PolicyConfiguration;
41 import org.onap.clamp.clds.exception.policy.PolicyClientException;
42 import org.onap.clamp.clds.model.properties.ModelProperties;
43 import org.onap.clamp.clds.util.LoggingUtils;
44 import org.onap.policy.api.AttributeType;
45 import org.onap.policy.api.ConfigRequestParameters;
46 import org.onap.policy.api.DeletePolicyCondition;
47 import org.onap.policy.api.DeletePolicyParameters;
48 import org.onap.policy.api.PolicyChangeResponse;
49 import org.onap.policy.api.PolicyConfig;
50 import org.onap.policy.api.PolicyConfigException;
51 import org.onap.policy.api.PolicyConfigType;
52 import org.onap.policy.api.PolicyEngine;
53 import org.onap.policy.api.PolicyEngineException;
54 import org.onap.policy.api.PolicyParameters;
55 import org.onap.policy.api.PolicyType;
56 import org.onap.policy.api.PushPolicyParameters;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.ApplicationContext;
59 import org.springframework.stereotype.Component;
60
61 /**
62  * Policy utility methods - specifically, send the policy.
63  */
64 @Component
65 public class PolicyClient {
66
67     protected static final String POLICY_PREFIX_BASE = "Config_";
68     protected static final String LOG_POLICY_PREFIX = "Response is ";
69     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
70     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
71     protected static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type";
72     protected static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name";
73     @Autowired
74     protected ApplicationContext appContext;
75     @Autowired
76     protected ClampProperties refProp;
77     @Autowired
78     private PolicyConfiguration policyConfiguration;
79
80     /**
81      * Perform BRMS policy type.
82      *
83      * @param attributes
84      *            A map of attributes
85      * @param prop
86      *            The ModelProperties
87      * @param policyRequestUuid
88      *            PolicyRequest UUID
89      * @return The response message of policy
90      */
91     public String sendBrmsPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
92             String policyRequestUuid) {
93         PolicyParameters policyParameters = new PolicyParameters();
94         // Set Policy Type(Mandatory)
95         policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);
96         // Set Policy Name(Mandatory)
97         policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
98         // documentation says this is options, but when tested, got the
99         // following failure: java.lang.Exception: Policy send failed: PE300 -
100         // Data Issue: No policyDescription given.
101         policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
102         policyParameters.setAttributes(attributes);
103         // Set a random UUID(Mandatory)
104         policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
105         String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
106         String rtnMsg = send(policyParameters, prop, policyNamePrefix);
107         String policyType = refProp.getStringValue("policy.op.type");
108         push(policyType, prop);
109         return rtnMsg;
110     }
111
112     /**
113      * Perform send of microservice policy in JSON.
114      *
115      * @param policyJson
116      *            The policy JSON
117      * @param prop
118      *            The ModelProperties
119      * @param policyRequestUuid
120      *            The policy Request UUID
121      * @return The response message of policy
122      */
123     public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) {
124         PolicyParameters policyParameters = new PolicyParameters();
125         // Set Policy Type
126         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
127         policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
128         policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
129         policyParameters.setConfigBody(policyJson);
130         policyParameters.setConfigBodyType(PolicyType.JSON);
131         policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
132         String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
133         // Adding this line to clear the policy id from policy name while
134         // pushing to policy engine
135         prop.setPolicyUniqueId("");
136         String rtnMsg = send(policyParameters, prop, policyNamePrefix);
137         String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
138         push(policyType, prop);
139         return rtnMsg;
140     }
141
142     /**
143      * Perform send of base policy in OTHER type.
144      *
145      * @param configBody
146      *            The config policy string body
147      * @param configPolicyName
148      *            The config policy name of the component that has been
149      *            pre-deployed in DCAE
150      * @param prop
151      *            The ModelProperties
152      * @param policyRequestUuid
153      *            The policy request UUID
154      * @return The answer from policy call
155      */
156     public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop,
157             String policyRequestUuid) {
158         PolicyParameters policyParameters = new PolicyParameters();
159         // Set Policy Type
160         policyParameters.setPolicyConfigType(PolicyConfigType.Base);
161         policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
162         policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
163         policyParameters.setConfigBody(configBody);
164         policyParameters.setConfigBodyType(PolicyType.OTHER);
165         policyParameters.setConfigName("HolmesPolicy");
166         policyParameters.setPolicyName(configPolicyName);
167         policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
168         // Adding this line to clear the policy id from policy name while
169         // pushing to policy engine
170         prop.setPolicyUniqueId("");
171         String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE);
172         push(PolicyConfigType.Base.toString(), prop);
173         return rtnMsg;
174     }
175
176     /**
177      * Perform send of Microservice policy in OTHER type.
178      * 
179      * @param configBody
180      *            The config policy string body
181      * @param prop
182      *            The ModelProperties
183      * @return The answer from policy call
184      */
185     public String sendMicroServiceInOther(String configBody, ModelProperties prop) {
186         PolicyParameters policyParameters = new PolicyParameters();
187         // Set Policy Type
188         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
189         policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME));
190         policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
191         policyParameters.setConfigBody(configBody);
192         String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
193         // Adding this line to clear the policy id from policy name while
194         // pushing to policy engine
195         prop.setPolicyUniqueId("");
196         String rtnMsg = send(policyParameters, prop, policyNamePrefix);
197         String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
198         push(policyType, prop);
199         return rtnMsg;
200     }
201
202     /**
203      * Perform send of policy.
204      *
205      * @param policyParameters
206      *            The PolicyParameters
207      * @param prop
208      *            The ModelProperties
209      * @return The response message of Policy
210      */
211     protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) {
212         // Verify whether it is triggered by Validation Test button from UI
213         if (prop.isTestOnly()) {
214             return "send not executed for test action";
215         }
216         // API method to create or update Policy.
217         PolicyChangeResponse response = null;
218         String responseMessage = "";
219         Date startTime = new Date();
220         try {
221             List<Integer> versions = getVersions(policyNamePrefix, prop);
222             if (versions.isEmpty()) {
223                 LoggingUtils.setTargetContext("Policy", "createPolicy");
224                 logger.info("Attempting to create policy for action=" + prop.getActionCd());
225                 response = getPolicyEngine().createPolicy(policyParameters);
226                 responseMessage = response.getResponseMessage();
227             } else {
228                 LoggingUtils.setTargetContext("Policy", "updatePolicy");
229                 logger.info("Attempting to update policy for action=" + prop.getActionCd());
230                 response = getPolicyEngine().updatePolicy(policyParameters);
231                 responseMessage = response.getResponseMessage();
232             }
233         } catch (Exception e) {
234             LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
235             LoggingUtils.setErrorContext("900", "Policy send error");
236             logger.error("Exception occurred during policy communication", e);
237             throw new PolicyClientException("Exception while communicating with Policy", e);
238         }
239         logger.info(LOG_POLICY_PREFIX + responseMessage);
240         LoggingUtils.setTimeContext(startTime, new Date());
241         if (response.getResponseCode() == 200) {
242             LoggingUtils.setResponseContext("0", "Policy send success", this.getClass().getName());
243             logger.info("Policy send successful");
244             metricsLogger.info("Policy send success");
245         } else {
246             LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
247             logger.warn("Policy send failed: " + responseMessage);
248             metricsLogger.info("Policy send failure");
249             throw new BadRequestException("Policy send failed: " + responseMessage);
250         }
251         return responseMessage;
252     }
253
254     /**
255      * Format and send push of policy.
256      *
257      * @param policyType
258      *            The policy Type
259      * @param prop
260      *            The ModelProperties
261      * @return The response message of policy
262      */
263     protected String push(String policyType, ModelProperties prop) {
264         // Verify whether it is triggered by Validation Test button from UI
265         if (prop.isTestOnly()) {
266             return "push not executed for test action";
267         }
268         PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
269         // Parameter arguments
270         if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
271             pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
272         } else {
273             pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
274         }
275         logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());
276         pushPolicyParameters.setPolicyType(policyType);
277         pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
278         pushPolicyParameters.setRequestID(null);
279         // API method to create or update Policy.
280         PolicyChangeResponse response;
281         String responseMessage = "";
282         try {
283             LoggingUtils.setTargetContext("Policy", "pushPolicy");
284             logger.info("Attempting to push policy...");
285             response = getPolicyEngine().pushPolicy(pushPolicyParameters);
286             if (response != null) {
287                 responseMessage = response.getResponseMessage();
288             }
289         } catch (Exception e) {
290             LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
291             LoggingUtils.setErrorContext("900", "Policy push error");
292             logger.error("Exception occurred during policy communication", e);
293             throw new PolicyClientException("Exception while communicating with Policy", e);
294         }
295         logger.info(LOG_POLICY_PREFIX + responseMessage);
296         if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
297             LoggingUtils.setResponseContext("0", "Policy push success", this.getClass().getName());
298             logger.info("Policy push successful");
299             metricsLogger.info("Policy push success");
300         } else {
301             LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
302             logger.warn("Policy push failed: " + responseMessage);
303             metricsLogger.info("Policy push failure");
304             throw new BadRequestException("Policy push failed: " + responseMessage);
305         }
306         return responseMessage;
307     }
308
309     /**
310      * Use Get Config Policy API to retrieve the versions for a policy. Return
311      * versions in sorted order. Return empty list if none found.
312      *
313      * @param policyNamePrefix
314      *            The Policy Name Prefix
315      * @param prop
316      *            The ModelProperties
317      * @return The response message from policy
318      * @throws PolicyConfigException
319      *             In case of issues with policy engine
320      */
321     protected List<Integer> getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException {
322         ArrayList<Integer> versions = new ArrayList<>();
323         ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
324         String policyName = "";
325         if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
326             policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId();
327         } else {
328             policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
329         }
330         logger.info("policyName=" + policyName);
331         configRequestParameters.setPolicyName(policyName);
332         try {
333             Collection<PolicyConfig> response = getPolicyEngine().getConfig(configRequestParameters);
334             for (PolicyConfig policyConfig : response) {
335                 Integer version = Integer.valueOf(policyConfig.getPolicyVersion());
336                 versions.add(version);
337             }
338             Collections.sort(versions);
339             logger.info("Policy versions.size()=" + versions.size());
340         } catch (PolicyConfigException e) {
341             // just print warning - if no policy version found
342             logger.warn("policy not found...policy name - " + policyName, e);
343         }
344         return versions;
345     }
346
347     /**
348      * This method create a new policy engine.
349      * 
350      * @return A new policy engine
351      */
352     private PolicyEngine getPolicyEngine() {
353         PolicyEngine policyEngine;
354         try {
355             policyEngine = new PolicyEngine(policyConfiguration.getProperties());
356         } catch (PolicyEngineException e) {
357             throw new PolicyClientException("Exception when creating a new policy engine", e);
358         }
359         return policyEngine;
360     }
361
362     /**
363      * Format and send delete Micro Service requests to Policy.
364      *
365      * @param prop
366      *            The ModelProperties
367      * @return The response message from Policy
368      */
369     public String deleteMicrosService(ModelProperties prop) {
370         String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME);
371         return deletePolicy(prop, policyType);
372     }
373
374     /**
375      * This method delete the Base policy.
376      *
377      * @param prop
378      *            The model Properties
379      * @return A string with the answer from policy
380      */
381     public String deleteBasePolicy(ModelProperties prop) {
382         return deletePolicy(prop, PolicyConfigType.Base.toString());
383     }
384
385     /**
386      * Format and send delete BRMS requests to Policy.
387      *
388      * @param prop
389      *            The ModelProperties
390      * @return The response message from policy
391      */
392     public String deleteBrms(ModelProperties prop) {
393         String policyType = refProp.getStringValue("policy.op.type");
394         return deletePolicy(prop, policyType);
395     }
396
397     /**
398      * Format and send delete PAP and PDP requests to Policy.
399      *
400      * @param prop
401      *            The ModelProperties
402      * @return The response message from policy
403      */
404     protected String deletePolicy(ModelProperties prop, String policyType) {
405         DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();
406         if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
407             deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
408         } else {
409             deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
410         }
411         logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
412         deletePolicyParameters.setPolicyComponent("PDP");
413         deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
414         deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
415         deletePolicyParameters.setPolicyType(policyType);
416         // send delete request
417         StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
418         logger.info("Deleting policy from PAP...");
419         deletePolicyParameters.setPolicyComponent("PAP");
420         deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
421         // send delete request
422         responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop));
423         return responseMessage.toString();
424     }
425
426     /**
427      * Send delete request to Policy.
428      *
429      * @param deletePolicyParameters
430      *            The DeletePolicyParameters
431      * @param prop
432      *            The ModelProperties
433      * @return The response message from policy
434      */
435     protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) {
436         // Verify whether it is triggered by Validation Test button from UI
437         if (prop.isTestOnly()) {
438             return "delete not executed for test action";
439         }
440         // API method to create or update Policy.
441         PolicyChangeResponse response = null;
442         String responseMessage = "";
443         try {
444             logger.info("Attempting to delete policy...");
445             response = getPolicyEngine().deletePolicy(deletePolicyParameters);
446             responseMessage = response.getResponseMessage();
447         } catch (Exception e) {
448             logger.error("Exception occurred during policy communnication", e);
449         }
450         logger.info(LOG_POLICY_PREFIX + responseMessage);
451         if (response != null && response.getResponseCode() == 200) {
452             logger.info("Policy delete successful");
453         } else {
454             logger.warn("Policy delete failed: " + responseMessage);
455             throw new BadRequestException("Policy delete failed: " + responseMessage);
456         }
457         return responseMessage;
458     }
459 }