* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import lombok.NoArgsConstructor;
import lombok.Setter;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
* @author pameladragosh
*
*/
+@NoArgsConstructor
public class StdMatchableTranslator extends StdBaseTranslator implements MatchableCallback {
private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchableTranslator.class);
private final Map<ToscaConceptIdentifier, MatchablePolicyType> matchableCache = new HashMap<>();
@Setter
- private RestServerParameters apiRestParameters;
+ private BusTopicParams apiRestParameters;
@Setter
private Path pathForData;
- public StdMatchableTranslator() {
- super();
- }
-
@Override
public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
LOGGER.info("Converting Request {}", request);
* contents and their details.
*
* @param closestMatches Map holding the current set of highest weight policy types
- * @param Obligation Obligation object
+ * @param obligation Obligation object
*/
protected void scanClosestMatchObligation(
Map<String, Map<Integer, List<Pair<String, Map<String, Object>>>>> closestMatches, Obligation obligation) {
//
// Create our OnapObligation object
//
- OnapObligation onapObligation = new OnapObligation(obligation);
+ var onapObligation = new OnapObligation(obligation);
//
// All 4 *should* be there
//
//
// Set it as the policy ID
//
- PolicyType newPolicyType = new PolicyType();
+ var newPolicyType = new PolicyType();
newPolicyType.setPolicyId(policyName);
//
// Optional description
//
// Now represent the policy as Json
//
- StandardCoder coder = new StandardCoder();
+ var coder = new StandardCoder();
String jsonPolicy;
try {
jsonPolicy = coder.encode(toscaPolicy);
//
// Now create the Permit Rule.
//
- RuleType rule = new RuleType();
+ var rule = new RuleType();
rule.setDescription("Default is to PERMIT if the policy matches.");
rule.setRuleId(policyName + ":rule");
rule.setEffect(EffectType.PERMIT);
//
// Log output of the policy
//
- try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+ try (var os = new ByteArrayOutputStream()) {
XACMLPolicyWriter.writePolicyFile(os, newPolicyType);
LOGGER.info("{}", os);
} catch (IOException e) {
* For generating target type, we scan for matchable properties
* and use those to build the policy.
*
- * @param properties Properties section of policy
- * @param policyTypes Collection of policy Type to find matchable metadata
+ * @param policy the policy
+ * @param template template containing the policy
* @return {@code Pair<TargetType, Integer>} Returns a TargetType and a Total Weight of matchables.
*/
protected Pair<TargetType, Integer> generateTargetType(ToscaPolicy policy, ToscaServiceTemplate template) {
//
// Our return object
//
- TargetType target = new TargetType();
+ var target = new TargetType();
//
// See if we have a matchable in the cache already
//
- MatchablePolicyType matchablePolicyType = matchableCache.get(policy.getTypeIdentifier());
+ var matchablePolicyType = matchableCache.get(policy.getTypeIdentifier());
//
// If not found, create one
//
//
// Our callback
//
- MyMatchableCallback myCallback = new MyMatchableCallback(this, template);
+ var myCallback = new MyMatchableCallback(this, template);
//
// Create the matchable
//
}
protected int calculateWeight(TargetType target) {
- int weight = 0;
+ var weight = 0;
for (AnyOfType anyOf : target.getAnyOf()) {
for (AllOfType allOf : anyOf.getAllOf()) {
weight += allOf.getMatch().size();
* @param policyTypeId ToscaConceptIdentifier to find
* @return ToscaPolicyType object. Can be null if failure.
*/
- private ToscaServiceTemplate findPolicyType(ToscaConceptIdentifier policyTypeId) {
+ protected ToscaServiceTemplate findPolicyType(ToscaConceptIdentifier policyTypeId) {
//
// Is it loaded in memory?
//
* @param policyTypeId ToscaConceptIdentifier input
* @return ToscaPolicyType object. Null if failure.
*/
- private ToscaServiceTemplate loadPolicyType(ToscaConceptIdentifier policyTypeId) {
+ protected ToscaServiceTemplate loadPolicyType(ToscaConceptIdentifier policyTypeId) {
//
// Construct what the file name should be
//
- Path policyTypePath = this.constructLocalFilePath(policyTypeId);
+ var policyTypePath = this.constructLocalFilePath(policyTypeId);
//
// See if it exists
//
//
// Ensure all the fields are setup correctly
//
- JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ var jtst = new JpaToscaServiceTemplate();
jtst.fromAuthorative(template);
return jtst.toAuthorative();
} catch (CoderException e) {
* @param policyTypePath Path object to store locally
* @return ToscaPolicyType object. Null if failure.
*/
- private synchronized ToscaServiceTemplate pullPolicyType(ToscaConceptIdentifier policyTypeId,
+ protected synchronized ToscaServiceTemplate pullPolicyType(ToscaConceptIdentifier policyTypeId,
Path policyTypePath) {
//
// This is what we return
//
ToscaServiceTemplate policyTemplate = null;
try {
- PolicyApiCaller api = new PolicyApiCaller(this.apiRestParameters);
+ var api = new PolicyApiCaller(this.apiRestParameters);
policyTemplate = api.getPolicyType(policyTypeId);
} catch (PolicyApiException e) {
LOGGER.error("Failed to make API call", e);
- LOGGER.error("parameters: {} ", this.apiRestParameters);
return null;
}
LOGGER.info("Successfully pulled {}", policyTypeId);
* @param policyTypeId ToscaConceptIdentifier
* @return Path object
*/
- private Path constructLocalFilePath(ToscaConceptIdentifier policyTypeId) {
+ protected Path constructLocalFilePath(ToscaConceptIdentifier policyTypeId) {
return Paths.get(this.pathForData.toAbsolutePath().toString(), policyTypeId.getName() + "-"
+ policyTypeId.getVersion() + ".yaml");
}