From 19ab2338a2af9daf35835cebf85a7415867d19bb Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 12 Sep 2018 18:52:39 +0200 Subject: Guard policy Backend Introduce Guard policy backend code Issue-ID: CLAMP-190 Change-Id: I7d60c6dd700c84607329f5449287465379b94807 Signed-off-by: sebdet --- .../clamp/clds/client/GuardPolicyDelegate.java | 98 ++++++++++++++++++++++ .../clds/client/GuardPolicyDeleteDelegate.java | 74 ++++++++++++++++ .../policy/GuardPolicyAttributesConstructor.java | 78 +++++++++++++++++ .../clamp/clds/client/req/policy/PolicyClient.java | 59 ++++++++++++- src/main/java/org/onap/clamp/clds/dao/CldsDao.java | 2 +- .../clamp/clds/model/properties/PolicyItem.java | 63 ++++++++++++++ .../org/onap/clamp/clds/service/CldsService.java | 2 +- 7 files changed, 373 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/onap/clamp/clds/client/GuardPolicyDelegate.java create mode 100644 src/main/java/org/onap/clamp/clds/client/GuardPolicyDeleteDelegate.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructor.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/clamp/clds/client/GuardPolicyDelegate.java b/src/main/java/org/onap/clamp/clds/client/GuardPolicyDelegate.java new file mode 100644 index 000000000..9287abbce --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/GuardPolicyDelegate.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.client; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.UnsupportedEncodingException; +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Handler; +import org.onap.clamp.clds.client.req.policy.GuardPolicyAttributesConstructor; +import org.onap.clamp.clds.client.req.policy.PolicyClient; +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.properties.Policy; +import org.onap.clamp.clds.model.properties.PolicyChain; +import org.onap.clamp.clds.model.properties.PolicyItem; +import org.onap.clamp.clds.util.LoggingUtils; +import org.onap.policy.api.AttributeType; +import org.onap.policy.controlloop.policy.builder.BuilderException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * Send Guard Policy info to policy API. It uses the policy code to define + * the model and communicate with it. See also the PolicyClient class. + */ +@Component +public class GuardPolicyDelegate { + + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(GuardPolicyDelegate.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + private final PolicyClient policyClient; + private final ClampProperties refProp; + + @Autowired + public GuardPolicyDelegate(PolicyClient policyClient, ClampProperties refProp) { + this.policyClient = policyClient; + this.refProp = refProp; + } + + /** + * Perform activity. Send Guard Policies info to policy api. + * + * @param camelExchange + * The Camel Exchange object containing the properties + * @throws BuilderException + * In case of issues with OperationalPolicyRequestAttributesConstructor + * @throws UnsupportedEncodingException + * In case of issues with the Charset encoding + */ + @Handler + public void execute(Exchange camelExchange) throws BuilderException, UnsupportedEncodingException { + String responseMessageGuard = null; + ModelProperties prop = ModelProperties.create(camelExchange); + Policy policy = prop.getType(Policy.class); + if (policy.isFound()) { + for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) { + for(PolicyItem policyItem:policyChain.getPolicyItems()) { + if ("on".equals(policyItem.getEnableGuardPolicy())) + responseMessageGuard = createGuardPolicy(prop, policyItem); + } + } + if (responseMessageGuard != null) { + camelExchange.setProperty("guardPolicyResponseMessage", responseMessageGuard.getBytes()); + } + } + } + + private String createGuardPolicy(ModelProperties prop, PolicyItem policyItem) { + Map> attributes = GuardPolicyAttributesConstructor + .formatAttributes(refProp, prop, prop.getType(Policy.class).getId(), policyItem); + return policyClient.sendGuardPolicy(attributes, prop, LoggingUtils.getRequestId()); + } +} diff --git a/src/main/java/org/onap/clamp/clds/client/GuardPolicyDeleteDelegate.java b/src/main/java/org/onap/clamp/clds/client/GuardPolicyDeleteDelegate.java new file mode 100644 index 000000000..9e8e1b8c5 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/GuardPolicyDeleteDelegate.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.client; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import org.apache.camel.Exchange; +import org.apache.camel.Handler; +import org.onap.clamp.clds.client.req.policy.PolicyClient; +import org.onap.clamp.clds.model.CldsEvent; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.properties.Policy; +import org.onap.clamp.clds.model.properties.PolicyChain; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * Delete Operational Policy via policy api. + */ +@Component +public class GuardPolicyDeleteDelegate { + + protected static final EELFLogger logger = EELFManager.getInstance() + .getLogger(GuardPolicyDeleteDelegate.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + @Autowired + private PolicyClient policyClient; + + /** + * Perform activity. Delete Operational Policy via policy api. + * + * @param camelExchange + * The Camel Exchange object containing the properties + */ + @Handler + public void execute(Exchange camelExchange) { + ModelProperties prop = ModelProperties.create(camelExchange); + Policy policy = prop.getType(Policy.class); + prop.setCurrentModelElementId(policy.getId()); + String eventAction = (String) camelExchange.getProperty("eventAction"); + String responseMessage = ""; + if (!eventAction.equalsIgnoreCase(CldsEvent.ACTION_CREATE) && policy.isFound()) { + for (PolicyChain policyChain : policy.getPolicyChains()) { + prop.setPolicyUniqueId(policyChain.getPolicyId()); + responseMessage = policyClient.deleteBrms(prop); + } + if (responseMessage != null) { + camelExchange.setProperty("operationalPolicyDeleteResponseMessage", responseMessage.getBytes()); + } + } + } +} diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructor.java b/src/main/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructor.java new file mode 100644 index 000000000..f11b492c0 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructor.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.client.req.policy; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.util.HashMap; +import java.util.Map; + +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.properties.PolicyItem; +import org.onap.policy.api.AttributeType; + +public class GuardPolicyAttributesConstructor { + private static final EELFLogger logger = EELFManager.getInstance() + .getLogger(GuardPolicyAttributesConstructor.class); + + private GuardPolicyAttributesConstructor() { + } + + public static Map> formatAttributes(ClampProperties refProp, + ModelProperties modelProperties, String modelElementId, PolicyItem policyItem) { + Map matchingAttributes = prepareMatchingAttributes(refProp, policyItem, modelProperties); + return createAttributesMap(matchingAttributes); + } + + private static Map prepareMatchingAttributes(ClampProperties refProp, + PolicyItem policyItem, ModelProperties modelProp) { + logger.info("Preparing matching attributes for guard..."); + Map matchingAttributes = new HashMap<>(); + matchingAttributes.put("actor",policyItem.getActor()); + matchingAttributes.put("recipe",policyItem.getRecipe()); + matchingAttributes.put("targets",policyItem.getGuardTargets()); + matchingAttributes.put("clname",modelProp.getControlNameAndPolicyUniqueId()); + if ("MinMax".equals(policyItem.getGuardPolicyType())) { + matchingAttributes.put("min",policyItem.getMinGuard()); + matchingAttributes.put("max",policyItem.getMaxGuard()); + } else if ("FrequencyLimiter".equals(policyItem.getGuardPolicyType())) { + matchingAttributes.put("limit",policyItem.getLimitGuard()); + matchingAttributes.put("timeWindow",policyItem.getTimeWindowGuard()); + matchingAttributes.put("timeUnits",policyItem.getTimeUnitsGuard()); + } + matchingAttributes.put("guardActiveStart",policyItem.getGuardActiveStart()); + matchingAttributes.put("guardActiveEnd",policyItem.getGuardActiveEnd()); + + logger.info("Prepared: " + matchingAttributes); + return matchingAttributes; + } + + private static Map> createAttributesMap(Map matchingAttributes) { + Map> attributes = new HashMap<>(); + attributes.put(AttributeType.MATCHING, matchingAttributes); + return attributes; + } +} diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java index cd387b3c3..58366d954 100644 --- a/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java @@ -43,6 +43,7 @@ import org.onap.policy.api.ConfigRequestParameters; import org.onap.policy.api.DeletePolicyCondition; import org.onap.policy.api.DeletePolicyParameters; import org.onap.policy.api.PolicyChangeResponse; +import org.onap.policy.api.PolicyClass; import org.onap.policy.api.PolicyConfigException; import org.onap.policy.api.PolicyConfigType; import org.onap.policy.api.PolicyEngine; @@ -50,6 +51,7 @@ import org.onap.policy.api.PolicyEngineException; import org.onap.policy.api.PolicyParameters; import org.onap.policy.api.PolicyType; import org.onap.policy.api.PushPolicyParameters; +import org.onap.policy.api.RuleProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Primary; @@ -79,6 +81,40 @@ public class PolicyClient { @Autowired private PolicyConfiguration policyConfiguration; + /** + * Perform Guard policy type. + * + * @param attributes + * A map of attributes + * @param prop + * The ModelProperties + * @param policyRequestUuid + * PolicyRequest UUID + * @return The response message of policy + */ + public String sendGuardPolicy(Map> attributes, ModelProperties prop, + String policyRequestUuid) { + PolicyParameters policyParameters = new PolicyParameters(); + // Set Policy Type(Mandatory) + policyParameters.setPolicyClass(PolicyClass.Decision); + // Set Policy Name(Mandatory) + policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()+"Guard"); + // documentation says this is options, but when tested, got the + // following failure: java.lang.Exception: Policy send failed: PE300 - + // Data Issue: No policyDescription given. + policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription")); + policyParameters.setOnapName("PDPD"); + policyParameters.setRuleProvider(RuleProvider.GUARD_YAML); + policyParameters.setAttributes(attributes); + // Set a random UUID(Mandatory) + policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); + String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME); + String rtnMsg = send(policyParameters, prop, policyNamePrefix); + String policyType = "Decision"; + push(policyType, prop); + return rtnMsg; + } + /** * Perform BRMS policy type. * @@ -332,7 +368,7 @@ public class PolicyClient { configRequestParameters.setPolicyName(policyName); try { Collection response = getPolicyEngine().listConfig(configRequestParameters); - if (response != null && !response.isEmpty()) { + if (response != null && !response.isEmpty() && !response.contains("Policy Name: null")) { policyexists = true; } } catch (PolicyConfigException e) { @@ -391,6 +427,27 @@ public class PolicyClient { return deletePolicy(prop, PolicyConfigType.Base.toString()); } + /** + * Format and send delete Guard requests to Policy. + * + * @param prop + * The ModelProperties + * @return The response message from policy + */ + public String deleteGuard(ModelProperties prop) { + String deletePolicyResponse = ""; + try { + String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME); + if (checkPolicyExists(policyNamePrefix, prop)) { + deletePolicyResponse = deletePolicy(prop, "Decision"); + } + } catch (Exception e) { + logger.error("Exception occurred during policy communication", e); + throw new PolicyClientException("Exception while communicating with Policy", e); + } + return deletePolicyResponse; + } + /** * Format and send delete BRMS requests to Policy. * diff --git a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java index 54a5196c1..d3e89fb6b 100644 --- a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java +++ b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java @@ -277,7 +277,7 @@ public class CldsDao { * * @return model names */ - public List getBpmnNames() { + public List getModelNames() { String sql = "SELECT model_name FROM model ORDER BY 1;"; return jdbcTemplateObject.query(sql, new ValueItemMapper()); } diff --git a/src/main/java/org/onap/clamp/clds/model/properties/PolicyItem.java b/src/main/java/org/onap/clamp/clds/model/properties/PolicyItem.java index 7caba41f0..2ac51ab46 100644 --- a/src/main/java/org/onap/clamp/clds/model/properties/PolicyItem.java +++ b/src/main/java/org/onap/clamp/clds/model/properties/PolicyItem.java @@ -71,6 +71,17 @@ public class PolicyItem implements Cloneable { private String oapRop; private String oapLimit; + private String enableGuardPolicy; + private String guardPolicyType; + private String guardTargets; + private String minGuard; + private String maxGuard; + private String limitGuard; + private String timeUnitsGuard; + private String timeWindowGuard; + private String guardActiveStart; + private String guardActiveEnd; + /** * Parse Policy given json node. * @@ -99,6 +110,17 @@ public class PolicyItem implements Cloneable { oapRop = AbstractModelElement.getValueByName(node, "oapRop"); oapLimit = AbstractModelElement.getValueByName(node, "oapLimit"); actor = AbstractModelElement.getValueByName(node, "actor"); + + enableGuardPolicy = AbstractModelElement.getValueByName(node, "enableGuardPolicy"); + guardPolicyType = AbstractModelElement.getValueByName(node, "guardPolicyType"); + guardTargets = AbstractModelElement.getValueByName(node, "guardTargets"); + minGuard = AbstractModelElement.getValueByName(node, "minGuard"); + maxGuard = AbstractModelElement.getValueByName(node, "maxGuard"); + limitGuard = AbstractModelElement.getValueByName(node, "limitGuard"); + timeUnitsGuard = AbstractModelElement.getValueByName(node, "timeUnitsGuard"); + timeWindowGuard = AbstractModelElement.getValueByName(node, "timeWindowGuard"); + guardActiveStart = AbstractModelElement.getValueByName(node, "guardActiveStart"); + guardActiveEnd = AbstractModelElement.getValueByName(node, "guardActiveEnd"); } /** @@ -236,4 +258,45 @@ public class PolicyItem implements Cloneable { } return oapLimit; } + + public String getEnableGuardPolicy() { + return enableGuardPolicy; + } + + public String getGuardPolicyType() { + return guardPolicyType; + } + + public String getGuardTargets() { + return guardTargets; + } + + public String getMinGuard() { + return minGuard; + } + + public String getMaxGuard() { + return maxGuard; + } + + public String getLimitGuard() { + return limitGuard; + } + + public String getTimeUnitsGuard() { + return timeUnitsGuard; + } + + public String getTimeWindowGuard() { + return timeWindowGuard; + } + + public String getGuardActiveStart() { + return guardActiveStart; + } + + public String getGuardActiveEnd() { + return guardActiveEnd; + } + } diff --git a/src/main/java/org/onap/clamp/clds/service/CldsService.java b/src/main/java/org/onap/clamp/clds/service/CldsService.java index 521f3ce27..243881f67 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsService.java @@ -300,7 +300,7 @@ public class CldsService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadCl); logger.info("GET list of model names"); - List names = cldsDao.getBpmnNames(); + List names = cldsDao.getModelNames(); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET model names completed"); -- cgit 1.2.3-korg