2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.client;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.List;
36 import java.util.UUID;
38 import javax.ws.rs.BadRequestException;
40 import org.onap.clamp.clds.exception.policy.PolicyClientException;
41 import org.onap.clamp.clds.model.prop.ModelProperties;
42 import org.onap.clamp.clds.model.refprop.RefProp;
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.beans.factory.annotation.Value;
59 import org.springframework.context.ApplicationContext;
62 * Policy utility methods - specifically, send the policy.
64 public class PolicyClient {
66 protected static final String POLICY_PREFIX_BASE = "Config_";
67 protected static final String POLICY_PREFIX_BRMS_PARAM = "Config_BRMS_Param_";
68 protected static final String POLICY_PREFIX_MICROSERVICE = "Config_MS_";
70 protected static final String LOG_POLICY_PREFIX = "Response is ";
72 protected static final EELFLogger logger = EELFManager.getInstance()
73 .getLogger(PolicyClient.class);
74 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
76 @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}")
77 protected String cldsPolicyConfigFile;
80 protected ApplicationContext appContext;
83 protected RefProp refProp;
86 * Perform BRMS policy type.
92 * @param policyRequestUuid
94 * @return The response message of policy
97 public String sendBrmsPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
98 String policyRequestUuid) {
100 PolicyParameters policyParameters = new PolicyParameters();
102 // Set Policy Type(Mandatory)
103 policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);
105 // Set Policy Name(Mandatory)
106 policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
108 // documentation says this is options, but when tested, got the
109 // following failure: java.lang.Exception: Policy send failed: PE300 -
110 // Data Issue: No policyDescription given.
111 policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
113 policyParameters.setAttributes(attributes);
115 // Set a random UUID(Mandatory)
116 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
117 String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
118 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
120 String policyType = refProp.getStringValue("policy.op.type");
121 push(policyType, prop);
127 * Perform send of microservice policy in JSON.
132 * The ModelProperties
133 * @param policyRequestUuid
134 * The policy Request UUID
135 * @return The response message of policy
137 public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) {
139 PolicyParameters policyParameters = new PolicyParameters();
142 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
143 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
144 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
146 policyParameters.setConfigBody(policyJson);
147 policyParameters.setConfigBodyType(PolicyType.JSON);
149 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
150 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
152 // Adding this line to clear the policy id from policy name while
153 // pushing to policy engine
154 prop.setPolicyUniqueId("");
156 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
157 String policyType = refProp.getStringValue("policy.ms.type");
158 push(policyType, prop);
164 * Perform send of base policy in OTHER type.
167 * The config policy string body
168 * @param configPolicyName
169 * The config policy name of the component that has been pre-deployed in DCAE
171 * The ModelProperties
172 * @param policyRequestUuid
173 * The policy request UUID
174 * @return The answer from policy call
176 public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop, String policyRequestUuid) {
178 PolicyParameters policyParameters = new PolicyParameters();
181 policyParameters.setPolicyConfigType(PolicyConfigType.Base);
182 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
183 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
185 policyParameters.setConfigBody(configBody);
186 policyParameters.setConfigBodyType(PolicyType.OTHER);
187 policyParameters.setConfigName("HolmesPolicy");
188 policyParameters.setPolicyName(configPolicyName);
190 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
192 // Adding this line to clear the policy id from policy name while
193 // pushing to policy engine
194 prop.setPolicyUniqueId("");
196 String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE);
197 push(PolicyConfigType.Base.toString(), prop);
203 * Perform send of Microservice policy in OTHER type.
206 * The config policy string body
208 * The ModelProperties
209 * @param policyRequestUuid
210 * The policy request UUID
211 * @return The answer from policy call
213 public String sendMicroServiceInOther(String configBody, ModelProperties prop, String policyRequestUuid) {
215 PolicyParameters policyParameters = new PolicyParameters();
218 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
219 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
220 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
222 policyParameters.setConfigBody(configBody);
223 policyParameters.setConfigBodyType(PolicyType.OTHER);
225 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
226 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
228 // Adding this line to clear the policy id from policy name while
229 // pushing to policy engine
230 prop.setPolicyUniqueId("");
232 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
233 String policyType = refProp.getStringValue("policy.ms.type");
234 push(policyType, prop);
240 * Perform send of policy.
242 * @param policyParameters
243 * The PolicyParameters
245 * The ModelProperties
246 * @return The response message of Policy
248 protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) {
249 // Verify whether it is triggered by Validation Test button from UI
251 return "send not executed for test action";
254 // API method to create or update Policy.
255 PolicyChangeResponse response = null;
256 String responseMessage = "";
257 Date startTime = new Date();
259 List<Integer> versions = getVersions(policyNamePrefix, prop);
260 if (versions.isEmpty()) {
261 LoggingUtils.setTargetContext("Policy", "createPolicy");
262 logger.info("Attempting to create policy for action=" + prop.getActionCd());
263 response = getPolicyEngine().createPolicy(policyParameters);
264 responseMessage = response.getResponseMessage();
266 LoggingUtils.setTargetContext("Policy", "updatePolicy");
267 logger.info("Attempting to update policy for action=" + prop.getActionCd());
268 response = getPolicyEngine().updatePolicy(policyParameters);
269 responseMessage = response.getResponseMessage();
271 } catch (Exception e) {
272 logger.error("Exception occurred during policy communication", e);
273 throw new PolicyClientException("Exception while communicating with Policy", e);
275 logger.info(LOG_POLICY_PREFIX + responseMessage);
277 LoggingUtils.setTimeContext(startTime, new Date());
279 if (response.getResponseCode() == 200) {
280 logger.info("Policy send successful");
281 metricsLogger.info("Policy send success");
283 logger.warn("Policy send failed: " + responseMessage);
284 metricsLogger.info("Policy send failure");
285 throw new BadRequestException("Policy send failed: " + responseMessage);
288 return responseMessage;
292 * Format and send push of policy.
297 * The ModelProperties
298 * @return The response message of policy
300 protected String push(String policyType, ModelProperties prop) {
301 // Verify whether it is triggered by Validation Test button from UI
303 return "push not executed for test action";
306 PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
308 // Parameter arguments
309 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
310 pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
312 pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
314 logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());
316 pushPolicyParameters.setPolicyType(policyType);
317 pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
318 pushPolicyParameters.setRequestID(null);
320 // API method to create or update Policy.
321 PolicyChangeResponse response = null;
322 String responseMessage = "";
324 logger.info("Attempting to push policy...");
325 response = getPolicyEngine().pushPolicy(pushPolicyParameters);
326 responseMessage = response.getResponseMessage();
327 } catch (Exception e) {
328 logger.error("Exception occurred during policy communication", e);
330 logger.info(LOG_POLICY_PREFIX + responseMessage);
332 if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
333 logger.info("Policy push successful");
335 logger.warn("Policy push failed: " + responseMessage);
336 throw new BadRequestException("Policy push failed: " + responseMessage);
339 return responseMessage;
343 * Use Get Config Policy API to retrieve the versions for a policy. Return
344 * versions in sorted order. Return empty list if none found.
346 * @param policyNamePrefix
347 * The Policy Name Prefix
349 * The ModelProperties
350 * @return The response message from policy
351 * @throws PolicyConfigException
352 * In case of issues with policy engine
354 protected List<Integer> getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException {
356 ArrayList<Integer> versions = new ArrayList<>();
357 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
358 String policyName = "";
360 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
361 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId();
363 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
366 logger.info("policyName=" + policyName);
367 configRequestParameters.setPolicyName(policyName);
369 Collection<PolicyConfig> response = getPolicyEngine().getConfig(configRequestParameters);
370 for (PolicyConfig policyConfig : response) {
371 Integer version = Integer.valueOf(policyConfig.getPolicyVersion());
372 versions.add(version);
374 Collections.sort(versions);
375 logger.info("Policy versions.size()=" + versions.size());
376 } catch (PolicyConfigException e) {
377 // just print warning - if no policy version found
378 logger.warn("warning: policy not found...policy name - " + policyName, e.getMessage());
385 * This method create a new policy engine.
387 * @return A new policy engine
389 private PolicyEngine getPolicyEngine() {
390 PolicyEngine policyEngine;
392 policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
393 } catch (IOException e1) {
394 throw new PolicyClientException("Exception when opening policy config file", e1);
395 } catch (PolicyEngineException e) {
396 throw new PolicyClientException("Exception when creating a new policy engine", e);
402 * Format and send delete Micro Service requests to Policy.
405 * The ModelProperties
406 * @return The response message from Policy
408 public String deleteMicrosService(ModelProperties prop) {
409 String policyType = refProp.getStringValue("policy.ms.type");
410 return deletePolicy(prop, policyType);
414 * This method delete the Base policy.
417 * The model Properties
418 * @return A string with the answer from policy
420 public String deleteBasePolicy(ModelProperties prop) {
421 return deletePolicy(prop, PolicyConfigType.Base.toString());
425 * Format and send delete BRMS requests to Policy.
428 * The ModelProperties
429 * @return The response message from policy
431 public String deleteBrms(ModelProperties prop) {
432 String policyType = refProp.getStringValue("policy.op.type");
433 return deletePolicy(prop, policyType);
437 * Format and send delete PAP and PDP requests to Policy.
440 * The ModelProperties
442 * @return The response message from policy
444 protected String deletePolicy(ModelProperties prop, String policyType) {
445 DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();
447 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
448 deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
450 deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
452 logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
453 deletePolicyParameters.setPolicyComponent("PDP");
454 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
455 deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
456 deletePolicyParameters.setPolicyType(policyType);
457 // send delete request
458 StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
460 logger.info("Deleting policy from PAP...");
461 deletePolicyParameters.setPolicyComponent("PAP");
462 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
464 // send delete request
465 responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop));
467 return responseMessage.toString();
471 * Send delete request to Policy.
473 * @param deletePolicyParameters
474 * The DeletePolicyParameters
476 * The ModelProperties
477 * @return The response message from policy
479 protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) {
480 // Verify whether it is triggered by Validation Test button from UI
482 return "delete not executed for test action";
485 // API method to create or update Policy.
486 PolicyChangeResponse response = null;
487 String responseMessage = "";
489 logger.info("Attempting to delete policy...");
490 response = getPolicyEngine().deletePolicy(deletePolicyParameters);
491 responseMessage = response.getResponseMessage();
492 } catch (Exception e) {
493 logger.error("Exception occurred during policy communnication", e);
495 logger.info(LOG_POLICY_PREFIX + responseMessage);
497 if (response != null && response.getResponseCode() == 200) {
498 logger.info("Policy delete successful");
500 logger.warn("Policy delete failed: " + responseMessage);
501 throw new BadRequestException("Policy delete failed: " + responseMessage);
504 return responseMessage;