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.client.req.TcaRequestFormatter;
41 import org.onap.clamp.clds.exception.policy.PolicyClientException;
42 import org.onap.clamp.clds.model.prop.ModelProperties;
43 import org.onap.clamp.clds.model.refprop.RefProp;
44 import org.onap.clamp.clds.util.LoggingUtils;
45 import org.onap.policy.api.AttributeType;
46 import org.onap.policy.api.ConfigRequestParameters;
47 import org.onap.policy.api.DeletePolicyCondition;
48 import org.onap.policy.api.DeletePolicyParameters;
49 import org.onap.policy.api.PolicyChangeResponse;
50 import org.onap.policy.api.PolicyConfig;
51 import org.onap.policy.api.PolicyConfigException;
52 import org.onap.policy.api.PolicyConfigType;
53 import org.onap.policy.api.PolicyEngine;
54 import org.onap.policy.api.PolicyEngineException;
55 import org.onap.policy.api.PolicyParameters;
56 import org.onap.policy.api.PolicyType;
57 import org.onap.policy.api.PushPolicyParameters;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.beans.factory.annotation.Value;
60 import org.springframework.context.ApplicationContext;
63 * Policy utility methods - specifically, send the policy.
65 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 // Set a random UUID(Mandatory)
114 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
115 String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
116 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
118 String policyType = refProp.getStringValue("policy.op.type");
119 push(policyType, prop);
125 * Perform send of microservice policy in JSON.
130 * The ModelProperties
131 * @param policyRequestUuid
132 * The policy Request UUID
133 * @return The response message of policy
135 public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) {
137 PolicyParameters policyParameters = new PolicyParameters();
140 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
141 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
142 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
144 policyParameters.setConfigBody(policyJson);
145 policyParameters.setConfigBodyType(PolicyType.JSON);
147 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
148 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
150 // Adding this line to clear the policy id from policy name while
151 // pushing to policy engine
152 prop.setPolicyUniqueId("");
154 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
155 String policyType = refProp.getStringValue("policy.ms.type");
156 push(policyType, prop);
162 * Perform send of base policy in OTHER type.
165 * The config policy string body
166 * @param configPolicyName
167 * The config policy name of the component that has been pre-deployed in DCAE
169 * The ModelProperties
170 * @param policyRequestUuid
171 * The policy request UUID
172 * @return The answer from policy call
174 public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop, String policyRequestUuid) {
176 PolicyParameters policyParameters = new PolicyParameters();
179 policyParameters.setPolicyConfigType(PolicyConfigType.Base);
180 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
181 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
183 policyParameters.setConfigBody(configBody);
184 policyParameters.setConfigBodyType(PolicyType.OTHER);
185 policyParameters.setConfigName("HolmesPolicy");
186 policyParameters.setPolicyName(configPolicyName);
188 policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
190 // Adding this line to clear the policy id from policy name while
191 // pushing to policy engine
192 prop.setPolicyUniqueId("");
194 String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE);
195 push(PolicyConfigType.Base.toString(), prop);
201 * Perform send of Microservice policy in OTHER type.
204 * The config policy string body
206 * The ModelProperties
207 * @param policyRequestUuid
208 * The policy request UUID
209 * @return The answer from policy call
211 public String sendMicroServiceInOther(String configBody, ModelProperties prop, String policyRequestUuid) {
213 PolicyParameters policyParameters = new PolicyParameters();
215 policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
216 policyParameters.setOnapName("DCAE");
217 policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
218 policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
219 policyParameters.setConfigBody(configBody);
220 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
222 // Adding this line to clear the policy id from policy name while
223 // pushing to policy engine
224 prop.setPolicyUniqueId("");
226 String rtnMsg = send(policyParameters, prop, policyNamePrefix);
227 String policyType = refProp.getStringValue("policy.ms.type");
228 push(policyType, prop);
234 * Perform send of policy.
236 * @param policyParameters
237 * The PolicyParameters
239 * The ModelProperties
240 * @return The response message of Policy
242 protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) {
243 // Verify whether it is triggered by Validation Test button from UI
245 return "send not executed for test action";
248 // API method to create or update Policy.
249 PolicyChangeResponse response = null;
250 String responseMessage = "";
251 Date startTime = new Date();
253 List<Integer> versions = getVersions(policyNamePrefix, prop);
254 if (versions.isEmpty()) {
255 LoggingUtils.setTargetContext("Policy", "createPolicy");
256 logger.info("Attempting to create policy for action=" + prop.getActionCd());
257 response = getPolicyEngine().createPolicy(policyParameters);
258 responseMessage = response.getResponseMessage();
260 LoggingUtils.setTargetContext("Policy", "updatePolicy");
261 logger.info("Attempting to update policy for action=" + prop.getActionCd());
262 response = getPolicyEngine().updatePolicy(policyParameters);
263 responseMessage = response.getResponseMessage();
265 } catch (Exception e) {
266 logger.error("Exception occurred during policy communication", e);
267 throw new PolicyClientException("Exception while communicating with Policy", e);
269 logger.info(LOG_POLICY_PREFIX + responseMessage);
271 LoggingUtils.setTimeContext(startTime, new Date());
273 if (response.getResponseCode() == 200) {
274 logger.info("Policy send successful");
275 metricsLogger.info("Policy send success");
277 logger.warn("Policy send failed: " + responseMessage);
278 metricsLogger.info("Policy send failure");
279 throw new BadRequestException("Policy send failed: " + responseMessage);
282 return responseMessage;
286 * Format and send push of policy.
291 * The ModelProperties
292 * @return The response message of policy
294 protected String push(String policyType, ModelProperties prop) {
295 // Verify whether it is triggered by Validation Test button from UI
297 return "push not executed for test action";
300 PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
302 // Parameter arguments
303 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
304 pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
306 pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
308 logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());
310 pushPolicyParameters.setPolicyType(policyType);
311 pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
312 pushPolicyParameters.setRequestID(null);
314 // API method to create or update Policy.
315 PolicyChangeResponse response = null;
316 String responseMessage = "";
318 logger.info("Attempting to push policy...");
319 response = getPolicyEngine().pushPolicy(pushPolicyParameters);
320 responseMessage = response.getResponseMessage();
321 } catch (Exception e) {
322 logger.error("Exception occurred during policy communication", e);
324 logger.info(LOG_POLICY_PREFIX + responseMessage);
326 if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
327 logger.info("Policy push successful");
329 logger.warn("Policy push failed: " + responseMessage);
330 throw new BadRequestException("Policy push failed: " + responseMessage);
333 return responseMessage;
337 * Use Get Config Policy API to retrieve the versions for a policy. Return
338 * versions in sorted order. Return empty list if none found.
340 * @param policyNamePrefix
341 * The Policy Name Prefix
343 * The ModelProperties
344 * @return The response message from policy
345 * @throws PolicyConfigException
346 * In case of issues with policy engine
348 protected List<Integer> getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException {
350 ArrayList<Integer> versions = new ArrayList<>();
351 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
352 String policyName = "";
354 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
355 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId();
357 policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
360 logger.info("policyName=" + policyName);
361 configRequestParameters.setPolicyName(policyName);
363 Collection<PolicyConfig> response = getPolicyEngine().getConfig(configRequestParameters);
364 for (PolicyConfig policyConfig : response) {
365 Integer version = Integer.valueOf(policyConfig.getPolicyVersion());
366 versions.add(version);
368 Collections.sort(versions);
369 logger.info("Policy versions.size()=" + versions.size());
370 } catch (PolicyConfigException e) {
371 // just print warning - if no policy version found
372 logger.warn("warning: policy not found...policy name - " + policyName, e.getMessage());
379 * This method create a new policy engine.
381 * @return A new policy engine
383 private PolicyEngine getPolicyEngine() {
384 PolicyEngine policyEngine;
386 policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
387 } catch (IOException e1) {
388 throw new PolicyClientException("Exception when opening policy config file", e1);
389 } catch (PolicyEngineException e) {
390 throw new PolicyClientException("Exception when creating a new policy engine", e);
396 * Format and send delete Micro Service requests to Policy.
399 * The ModelProperties
400 * @return The response message from Policy
402 public String deleteMicrosService(ModelProperties prop) {
403 String policyType = refProp.getStringValue("policy.ms.type");
404 return deletePolicy(prop, policyType);
408 * This method delete the Base policy.
411 * The model Properties
412 * @return A string with the answer from policy
414 public String deleteBasePolicy(ModelProperties prop) {
415 return deletePolicy(prop, PolicyConfigType.Base.toString());
419 * Format and send delete BRMS requests to Policy.
422 * The ModelProperties
423 * @return The response message from policy
425 public String deleteBrms(ModelProperties prop) {
426 String policyType = refProp.getStringValue("policy.op.type");
427 return deletePolicy(prop, policyType);
431 * Format and send delete PAP and PDP requests to Policy.
434 * The ModelProperties
436 * @return The response message from policy
438 protected String deletePolicy(ModelProperties prop, String policyType) {
439 DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();
441 if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
442 deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
444 deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
446 logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
447 deletePolicyParameters.setPolicyComponent("PDP");
448 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
449 deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
450 deletePolicyParameters.setPolicyType(policyType);
451 // send delete request
452 StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));
454 logger.info("Deleting policy from PAP...");
455 deletePolicyParameters.setPolicyComponent("PAP");
456 deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
458 // send delete request
459 responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop));
461 return responseMessage.toString();
465 * Send delete request to Policy.
467 * @param deletePolicyParameters
468 * The DeletePolicyParameters
470 * The ModelProperties
471 * @return The response message from policy
473 protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) {
474 // Verify whether it is triggered by Validation Test button from UI
476 return "delete not executed for test action";
479 // API method to create or update Policy.
480 PolicyChangeResponse response = null;
481 String responseMessage = "";
483 logger.info("Attempting to delete policy...");
484 response = getPolicyEngine().deletePolicy(deletePolicyParameters);
485 responseMessage = response.getResponseMessage();
486 } catch (Exception e) {
487 logger.error("Exception occurred during policy communnication", e);
489 logger.info(LOG_POLICY_PREFIX + responseMessage);
491 if (response != null && response.getResponseCode() == 200) {
492 logger.info("Policy delete successful");
494 logger.warn("Policy delete failed: " + responseMessage);
495 throw new BadRequestException("Policy delete failed: " + responseMessage);
498 return responseMessage;