diff options
author | Jim Hahn <jrh3@att.com> | 2019-06-13 18:18:39 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-06-17 14:53:51 -0400 |
commit | 6347aed1148a9fab8f7f45e46be71b8bdfc52924 (patch) | |
tree | 36b02ba338e7a17160347360a2dee03596bfb21d /models-interactions/model-yaml/src/main/java | |
parent | f59ec395bf1e41df894f884e70ff3185280668c0 (diff) |
Fix more sonar issues in models: yaml to dao
Extracted common Strings into constants.
Reduced "cyclomatic complexity" in some return statements.
Used assertj to eliminate "log or rethrow" messages in junit tests.
models-yaml
models-base
models-dao
Change-Id: I20548d4cf5e67d085245e0d54df8ba0116ec86ec
Issue-ID: POLICY-1791
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-interactions/model-yaml/src/main/java')
7 files changed, 250 insertions, 241 deletions
diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/compiler/ControlLoopCompiler.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/compiler/ControlLoopCompiler.java index 91b5266cc..c1543d05b 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/compiler/ControlLoopCompiler.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/compiler/ControlLoopCompiler.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -31,7 +31,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; - +import org.apache.commons.lang3.StringUtils; import org.jgrapht.DirectedGraph; import org.jgrapht.graph.ClassBasedEdgeFactory; import org.jgrapht.graph.DefaultEdge; @@ -52,11 +52,11 @@ public class ControlLoopCompiler implements Serializable { private static final String OPERATION_POLICY = "Operation Policy "; private static final long serialVersionUID = 1L; private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopCompiler.class.getName()); - + /** * Compiles the policy from an object. */ - public static ControlLoopPolicy compile(ControlLoopPolicy policy, + public static ControlLoopPolicy compile(ControlLoopPolicy policy, ControlLoopCompilerCallback callback) throws CompilerException { // // Ensure the control loop is sane @@ -66,19 +66,19 @@ public class ControlLoopCompiler implements Serializable { // Validate the policies // validatePolicies(policy, callback); - + return policy; } - + /** * Compiles the policy from an input stream. - * + * * @param yamlSpecification the yaml input stream * @param callback method to callback during compilation * @return Control Loop object * @throws CompilerException throws any compile exception found */ - public static ControlLoopPolicy compile(InputStream yamlSpecification, + public static ControlLoopPolicy compile(InputStream yamlSpecification, ControlLoopCompilerCallback callback) throws CompilerException { Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); Object obj = yaml.load(yamlSpecification); @@ -90,39 +90,38 @@ public class ControlLoopCompiler implements Serializable { } return ControlLoopCompiler.compile((ControlLoopPolicy) obj, callback); } - - private static void validateControlLoop(ControlLoop controlLoop, + + private static void validateControlLoop(ControlLoop controlLoop, ControlLoopCompilerCallback callback) throws CompilerException { if (controlLoop == null && callback != null) { callback.onError("controlLoop cannot be null"); } if (controlLoop != null) { - if ((controlLoop.getControlLoopName() == null || controlLoop.getControlLoopName().length() < 1) - && callback != null) { + if (StringUtils.isEmpty(controlLoop.getControlLoopName()) && callback != null) { callback.onError("Missing controlLoopName"); } if ((!controlLoop.getVersion().contentEquals(ControlLoop.getCompilerVersion())) && callback != null) { callback.onError("Unsupported version for this compiler"); } - if (controlLoop.getTrigger_policy() == null || controlLoop.getTrigger_policy().length() < 1) { + if (StringUtils.isEmpty(controlLoop.getTrigger_policy())) { throw new CompilerException("trigger_policy is not valid"); } } } - private static void validatePolicies(ControlLoopPolicy policy, + private static void validatePolicies(ControlLoopPolicy policy, ControlLoopCompilerCallback callback) throws CompilerException { if (policy == null) { throw new CompilerException("policy cannot be null"); } if (policy.getPolicies() == null) { - callback.onWarning("controlLoop is an open loop."); + callback.onWarning("controlLoop is an open loop."); } else { // // For this version we can use a directed multigraph, in the future we may not be able to // - DirectedGraph<NodeWrapper, LabeledEdge> graph = - new DirectedMultigraph<>(new ClassBasedEdgeFactory<NodeWrapper, + DirectedGraph<NodeWrapper, LabeledEdge> graph = + new DirectedMultigraph<>(new ClassBasedEdgeFactory<NodeWrapper, LabeledEdge>(LabeledEdge.class)); // // Check to see if the trigger Event is for OpenLoop, we do so by @@ -153,7 +152,7 @@ public class ControlLoopCompiler implements Serializable { FinalResultNodeWrapper finalFailure = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE); FinalResultNodeWrapper finalFailureTimeout = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_TIMEOUT); FinalResultNodeWrapper finalFailureRetries = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_RETRIES); - FinalResultNodeWrapper finalFailureException = + FinalResultNodeWrapper finalFailureException = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_EXCEPTION); FinalResultNodeWrapper finalFailureGuard = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_GUARD); graph.addVertex(finalSuccess); @@ -177,30 +176,30 @@ public class ControlLoopCompiler implements Serializable { if (node == null) { continue; } - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getSuccess(), finalSuccess, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getSuccess(), finalSuccess, PolicyResult.SUCCESS, node); - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure(), finalFailure, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure(), finalFailure, PolicyResult.FAILURE, node); - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_timeout(), finalFailureTimeout, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_timeout(), finalFailureTimeout, PolicyResult.FAILURE_TIMEOUT, node); - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_retries(), finalFailureRetries, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_retries(), finalFailureRetries, PolicyResult.FAILURE_RETRIES, node); - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_exception(), finalFailureException, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_exception(), finalFailureException, PolicyResult.FAILURE_EXCEPTION, node); - addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_guard(), finalFailureGuard, + addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_guard(), finalFailureGuard, PolicyResult.FAILURE_GUARD, node); } validateNodesAndEdges(graph, callback); - } + } } - - private static void validateOpenLoopPolicy(ControlLoopPolicy policy, FinalResult triggerResult, + + private static void validateOpenLoopPolicy(ControlLoopPolicy policy, FinalResult triggerResult, ControlLoopCompilerCallback callback) throws CompilerException { // // Ensure they didn't use some other FinalResult code // if (triggerResult != FinalResult.FINAL_OPENLOOP) { - throw new CompilerException("Unexpected Final Result for trigger_policy, should only be " + throw new CompilerException("Unexpected Final Result for trigger_policy, should only be " + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID"); } // @@ -210,8 +209,8 @@ public class ControlLoopCompiler implements Serializable { callback.onWarning("Open Loop policy contains policies. The policies will never be invoked."); } } - - private static void validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(ControlLoopPolicy policy, + + private static void validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(ControlLoopPolicy policy, ControlLoopCompilerCallback callback) throws CompilerException { int sum = 0; boolean triggerPolicyFound = false; @@ -224,15 +223,15 @@ public class ControlLoopCompiler implements Serializable { if (policy.getControlLoop().getTimeout().intValue() < sum && callback != null) { callback.onError("controlLoop overall timeout is less than the sum of operational policy timeouts."); } - + if (!triggerPolicyFound) { - throw new CompilerException("Unexpected value for trigger_policy, should only be " + throw new CompilerException("Unexpected value for trigger_policy, should only be " + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID"); } } - - private static Map<Policy, PolicyNodeWrapper> addPoliciesAsNodes(ControlLoopPolicy policy, - DirectedGraph<NodeWrapper, LabeledEdge> graph, TriggerNodeWrapper triggerNode, + + private static Map<Policy, PolicyNodeWrapper> addPoliciesAsNodes(ControlLoopPolicy policy, + DirectedGraph<NodeWrapper, LabeledEdge> graph, TriggerNodeWrapper triggerNode, ControlLoopCompilerCallback callback) { Map<Policy, PolicyNodeWrapper> mapNodes = new HashMap<>(); for (Policy operPolicy : policy.getPolicies()) { @@ -264,27 +263,27 @@ public class ControlLoopCompiler implements Serializable { } return mapNodes; } - + private static void addEdge(DirectedGraph<NodeWrapper, LabeledEdge> graph, Map<Policy, PolicyNodeWrapper> mapNodes, - String policyId, String connectedPolicy, - FinalResultNodeWrapper finalResultNodeWrapper, + String policyId, String connectedPolicy, + FinalResultNodeWrapper finalResultNodeWrapper, PolicyResult policyResult, NodeWrapper node) throws CompilerException { FinalResult finalResult = FinalResult.toResult(finalResultNodeWrapper.getId()); if (FinalResult.isResult(connectedPolicy, finalResult)) { - graph.addEdge(node, finalResultNodeWrapper, new LabeledEdge(node, finalResultNodeWrapper, + graph.addEdge(node, finalResultNodeWrapper, new LabeledEdge(node, finalResultNodeWrapper, new FinalResultEdgeWrapper(finalResult))); } else { PolicyNodeWrapper toNode = findPolicyNode(mapNodes, connectedPolicy); if (toNode == null) { - throw new CompilerException(OPERATION_POLICY + policyId + " is connected to unknown policy " + throw new CompilerException(OPERATION_POLICY + policyId + " is connected to unknown policy " + connectedPolicy); } else { graph.addEdge(node, toNode, new LabeledEdge(node, toNode, new PolicyResultEdgeWrapper(policyResult))); } } } - - private static void validateNodesAndEdges(DirectedGraph<NodeWrapper, LabeledEdge> graph, + + private static void validateNodesAndEdges(DirectedGraph<NodeWrapper, LabeledEdge> graph, ControlLoopCompilerCallback callback) throws CompilerException { for (NodeWrapper node : graph.vertexSet()) { if (node instanceof TriggerNodeWrapper) { @@ -299,8 +298,8 @@ public class ControlLoopCompiler implements Serializable { } } } - - private static void validateTriggerNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, + + private static void validateTriggerNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, NodeWrapper node) throws CompilerException { if (LOGGER.isDebugEnabled()) { LOGGER.info("Trigger Node {}", node); @@ -318,8 +317,8 @@ public class ControlLoopCompiler implements Serializable { throw new CompilerException("The event trigger should only go to ONE node"); } } - - private static void validateFinalResultNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, + + private static void validateFinalResultNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, NodeWrapper node) throws CompilerException { if (LOGGER.isDebugEnabled()) { LOGGER.info("FinalResult Node {}", node); @@ -331,8 +330,8 @@ public class ControlLoopCompiler implements Serializable { throw new CompilerException("FinalResult nodes should never have any out edges."); } } - - private static void validatePolicyNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, + + private static void validatePolicyNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, NodeWrapper node, ControlLoopCompilerCallback callback) throws CompilerException { if (LOGGER.isDebugEnabled()) { LOGGER.info("Policy Node {}", node); @@ -344,13 +343,13 @@ public class ControlLoopCompiler implements Serializable { throw new CompilerException("Policy node should ALWAYS have 6 out degrees."); } // - // All Policy Nodes should have at least 1 in degrees - // + // All Policy Nodes should have at least 1 in degrees + // if (graph.inDegreeOf(node) == 0 && callback != null) { callback.onWarning("Policy " + node.getId() + " is not reachable."); } } - + private static boolean okToAdd(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = isPolicyIdOk(operPolicy, callback); if (! isActorOk(operPolicy, callback)) { @@ -367,7 +366,7 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isPolicyIdOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; if (operPolicy.getId() == null || operPolicy.getId().length() < 1) { @@ -394,7 +393,7 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isActorOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; if (operPolicy.getActor() == null) { @@ -416,7 +415,7 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isRecipeOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; if (operPolicy.getRecipe() == null) { @@ -427,7 +426,7 @@ public class ControlLoopCompiler implements Serializable { } // // NOTE: We need a way to find the acceptable recipe values (either Enum or a database that has these) - // + // ImmutableMap<String, List<String>> recipes = new ImmutableMap.Builder<String, List<String>>() .put("APPC", ImmutableList.of("Restart", "Rebuild", "Migrate", "ModifyConfig")) .put("SDNC", ImmutableList.of("Reroute")) @@ -436,8 +435,8 @@ public class ControlLoopCompiler implements Serializable { .put("VFC", ImmutableList.of("Restart")) .build(); // - if (operPolicy.getRecipe() != null - && (!recipes.getOrDefault(operPolicy.getActor(), + if (operPolicy.getRecipe() != null + && (!recipes.getOrDefault(operPolicy.getActor(), Collections.emptyList()).contains(operPolicy.getRecipe()))) { if (callback != null) { callback.onError("Policy recipe is invalid"); @@ -446,7 +445,7 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isTargetOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; if (operPolicy.getTarget() == null) { @@ -455,9 +454,9 @@ public class ControlLoopCompiler implements Serializable { } isOk = false; } - if (operPolicy.getTarget() != null - && operPolicy.getTarget().getType() != TargetType.VM - && operPolicy.getTarget().getType() != TargetType.VFC + if (operPolicy.getTarget() != null + && operPolicy.getTarget().getType() != TargetType.VM + && operPolicy.getTarget().getType() != TargetType.VFC && operPolicy.getTarget().getType() != TargetType.PNF) { if (callback != null) { callback.onError("Policy target is invalid"); @@ -466,7 +465,7 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean arePolicyResultsOk(Policy operPolicy, ControlLoopCompilerCallback callback) { // // Check that policy results are connected to either default final * or another policy @@ -489,10 +488,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isSuccessPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getSuccess()) != null + if (FinalResult.toResult(operPolicy.getSuccess()) != null && !operPolicy.getSuccess().equals(FinalResult.FINAL_SUCCESS.toString())) { if (callback != null) { callback.onError("Policy success is neither another policy nor FINAL_SUCCESS"); @@ -501,10 +500,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isFailurePolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getFailure()) != null + if (FinalResult.toResult(operPolicy.getFailure()) != null && !operPolicy.getFailure().equals(FinalResult.FINAL_FAILURE.toString())) { if (callback != null) { callback.onError("Policy failure is neither another policy nor FINAL_FAILURE"); @@ -513,10 +512,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isFailureRetriesPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getFailure_retries()) != null + if (FinalResult.toResult(operPolicy.getFailure_retries()) != null && !operPolicy.getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())) { if (callback != null) { callback.onError("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES"); @@ -525,10 +524,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isFailureTimeoutPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getFailure_timeout()) != null + if (FinalResult.toResult(operPolicy.getFailure_timeout()) != null && !operPolicy.getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())) { if (callback != null) { callback.onError("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT"); @@ -537,10 +536,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isFailureExceptionPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getFailure_exception()) != null + if (FinalResult.toResult(operPolicy.getFailure_exception()) != null && !operPolicy.getFailure_exception().equals(FinalResult.FINAL_FAILURE_EXCEPTION.toString())) { if (callback != null) { callback.onError("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION"); @@ -549,10 +548,10 @@ public class ControlLoopCompiler implements Serializable { } return isOk; } - + private static boolean isFailureGuardPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) { boolean isOk = true; - if (FinalResult.toResult(operPolicy.getFailure_guard()) != null + if (FinalResult.toResult(operPolicy.getFailure_guard()) != null && !operPolicy.getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString())) { if (callback != null) { callback.onError("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD"); @@ -570,16 +569,16 @@ public class ControlLoopCompiler implements Serializable { } return null; } - + @FunctionalInterface private interface NodeWrapper extends Serializable { public String getId(); } - + private static class TriggerNodeWrapper implements NodeWrapper { private static final long serialVersionUID = -187644087811478349L; private String closedLoopControlName; - + public TriggerNodeWrapper(String closedLoopControlName) { this.closedLoopControlName = closedLoopControlName; } @@ -593,9 +592,9 @@ public class ControlLoopCompiler implements Serializable { public String getId() { return closedLoopControlName; } - + } - + private static class FinalResultNodeWrapper implements NodeWrapper { private static final long serialVersionUID = 8540008796302474613L; private FinalResult result; @@ -614,11 +613,11 @@ public class ControlLoopCompiler implements Serializable { return result.toString(); } } - + private static class PolicyNodeWrapper implements NodeWrapper { private static final long serialVersionUID = 8170162175653823082L; private transient Policy policy; - + public PolicyNodeWrapper(Policy operPolicy) { this.policy = operPolicy; } @@ -633,17 +632,17 @@ public class ControlLoopCompiler implements Serializable { return policy.getId(); } } - + @FunctionalInterface private interface EdgeWrapper extends Serializable { public String getId(); - + } - + private static class TriggerEdgeWrapper implements EdgeWrapper { private static final long serialVersionUID = 2678151552623278863L; private String trigger; - + public TriggerEdgeWrapper(String trigger) { this.trigger = trigger; } @@ -657,9 +656,9 @@ public class ControlLoopCompiler implements Serializable { public String toString() { return "TriggerEdgeWrapper [trigger=" + trigger + "]"; } - + } - + private static class PolicyResultEdgeWrapper implements EdgeWrapper { private static final long serialVersionUID = 6078569477021558310L; private PolicyResult policyResult; @@ -678,14 +677,14 @@ public class ControlLoopCompiler implements Serializable { public String getId() { return policyResult.toString(); } - - + + } - + private static class FinalResultEdgeWrapper implements EdgeWrapper { private static final long serialVersionUID = -1486381946896779840L; private FinalResult finalResult; - + public FinalResultEdgeWrapper(FinalResult result) { this.finalResult = result; } @@ -694,37 +693,37 @@ public class ControlLoopCompiler implements Serializable { public String toString() { return "FinalResultEdgeWrapper [finalResult=" + finalResult + "]"; } - + @Override public String getId() { return finalResult.toString(); } } - - + + private static class LabeledEdge extends DefaultEdge { private static final long serialVersionUID = 579384429573385524L; - + private NodeWrapper from; private NodeWrapper to; private EdgeWrapper edge; - + public LabeledEdge(NodeWrapper from, NodeWrapper to, EdgeWrapper edge) { this.from = from; this.to = to; this.edge = edge; } - + @SuppressWarnings("unused") public NodeWrapper from() { return from; } - + @SuppressWarnings("unused") public NodeWrapper to() { return to; } - + @SuppressWarnings("unused") public EdgeWrapper edge() { return edge; diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java index fc3d82326..69f62b28e 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -49,7 +49,7 @@ public class ControlLoop implements Serializable { /** * Constructor. - * + * * @param controlLoop copy object */ public ControlLoop(ControlLoop controlLoop) { @@ -172,10 +172,12 @@ public class ControlLoop implements Serializable { return false; } ControlLoop other = (ControlLoop) obj; - return equalsMayBeNull(controlLoopName, other.controlLoopName) && equalsMayBeNull(resources, other.resources) - && equalsMayBeNull(services, other.services) && equalsMayBeNull(timeout, other.timeout) - && equalsMayBeNull(triggerPolicy, other.triggerPolicy) && equalsMayBeNull(version, other.version) - && equalsMayBeNull(abatement, other.abatement); + + boolean isEq = equalsMayBeNull(controlLoopName, other.controlLoopName) + && equalsMayBeNull(resources, other.resources) && equalsMayBeNull(services, other.services); + isEq = isEq && equalsMayBeNull(timeout, other.timeout) && equalsMayBeNull(triggerPolicy, other.triggerPolicy) + && equalsMayBeNull(version, other.version); + return (isEq && equalsMayBeNull(abatement, other.abatement)); } private boolean equalsMayBeNull(final Object obj1, final Object obj2) { diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java index af50eaa00..728733d76 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -45,19 +45,19 @@ public class Policy implements Serializable { private String failureTimeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString(); private String failureException = FinalResult.FINAL_FAILURE_EXCEPTION.toString(); private String failureGuard = FinalResult.FINAL_FAILURE_GUARD.toString(); - - + + public Policy() { //Does Nothing Empty Constructor } - + public Policy(String id) { this.id = id; } - + /** * Constructor. - * + * * @param name name * @param actor actor * @param recipe recipe @@ -73,10 +73,10 @@ public class Policy implements Serializable { this.payload = Collections.unmodifiableMap(payload); } } - + /** * Constructor. - * + * * @param name name * @param actor actor * @param recipe recipe @@ -91,7 +91,7 @@ public class Policy implements Serializable { this.retry = retries; this.timeout = timeout; } - + /** * Constructor. * @@ -103,10 +103,10 @@ public class Policy implements Serializable { this.id = policyParam.getId(); this.description = policyParam.getDescription(); } - + /** * Constructor. - * + * * @param policy copy object */ public Policy(Policy policy) { @@ -259,13 +259,14 @@ public class Policy implements Serializable { } public boolean isValid() { - return id != null && name != null && actor != null && recipe != null && target != null; + boolean isValid = id != null && name != null && actor != null; + return isValid && recipe != null && target != null; } @Override public String toString() { return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", actor=" + actor + ", recipe=" - + recipe + ", payload=" + payload + ", target=" + target + ", operationsAccumulateParams=" + + recipe + ", payload=" + payload + ", target=" + target + ", operationsAccumulateParams=" + operationsAccumulateParams + ", retry=" + retry + ", timeout=" + timeout + ", success=" + success + ", failure=" + failure + ", failure_retries=" + failureRetries + ", failure_timeout=" + failureTimeout + ", failure_exception=" + failureException @@ -293,7 +294,7 @@ public class Policy implements Serializable { result = addHashCodeForField(result, timeout); return result; } - + private int addHashCodeForField(int hashCode, Object field) { final int prime = 31; return prime * hashCode + ((field == null) ? 0 : field.hashCode()); @@ -311,23 +312,29 @@ public class Policy implements Serializable { return false; } Policy other = (Policy) obj; - return equalsMayBeNull(actor, other.actor) + boolean isEq = equalsMayBeNull(actor, other.actor) && equalsMayBeNull(description, other.description) - && equalsMayBeNull(failure, other.failure) + && equalsMayBeNull(failure, other.failure); + isEq = isEq && equalsMayBeNull(failureException, other.failureException) - && equalsMayBeNull(failureGuard, other.failureGuard) + && equalsMayBeNull(failureGuard, other.failureGuard); + isEq = isEq && equalsMayBeNull(failureRetries, other.failureRetries) - && equalsMayBeNull(id, other.id) + && equalsMayBeNull(id, other.id); + isEq = isEq && equalsMayBeNull(name, other.name) - && equalsMayBeNull(payload, other.payload) + && equalsMayBeNull(payload, other.payload); + isEq = isEq && equalsMayBeNull(recipe, other.recipe) - && equalsMayBeNull(retry, other.retry) + && equalsMayBeNull(retry, other.retry); + isEq = isEq && equalsMayBeNull(success, other.success) - && equalsMayBeNull(operationsAccumulateParams, other.operationsAccumulateParams) + && equalsMayBeNull(operationsAccumulateParams, other.operationsAccumulateParams); + return isEq && equalsMayBeNull(target, other.target) && equalsMayBeNull(timeout, other.timeout); } - + private boolean equalsMayBeNull(final Object obj1, final Object obj2) { if ( obj1 == null ) { return obj2 == null; diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Target.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Target.java index b8432ba60..bc99975f8 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Target.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/Target.java @@ -37,6 +37,28 @@ public class Target implements Serializable { private String modelVersion; private String modelCustomizationId; + public Target() { + //Does Nothing Empty Constructor + } + + public Target(TargetType type) { + this.type = type; + } + + public Target(String resourceId) { + this.resourceId = resourceId; + } + + public Target(TargetType type, String resourceId) { + this.type = type; + this.resourceId = resourceId; + } + + public Target(Target target) { + this.type = target.type; + this.resourceId = target.resourceId; + } + public String getModelInvariantId() { return modelInvariantId; } @@ -77,28 +99,6 @@ public class Target implements Serializable { this.modelCustomizationId = modelCustomizationId; } //techm - public Target() { - //Does Nothing Empty Constructor - } - - public Target(TargetType type) { - this.type = type; - } - - public Target(String resourceId) { - this.resourceId = resourceId; - } - - public Target(TargetType type, String resourceId) { - this.type = type; - this.resourceId = resourceId; - } - - public Target(Target target) { - this.type = target.type; - this.resourceId = target.resourceId; - } - public String getResourceID() { return resourceId; } diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java index b2d243c4e..7144fe363 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -22,22 +22,23 @@ package org.onap.policy.controlloop.policy.guard; import java.util.LinkedList; +import java.util.List; public class ControlLoopGuard { - + private Guard guard; - - private LinkedList<GuardPolicy> guards; - + + private List<GuardPolicy> guards; + public ControlLoopGuard() { //DO Nothing Empty Constructor } - + public ControlLoopGuard(ControlLoopGuard clGuard) { this.guard = new Guard(); this.guards = new LinkedList<>(clGuard.guards); } - + public Guard getGuard() { return guard; } @@ -46,11 +47,11 @@ public class ControlLoopGuard { this.guard = guard; } - public LinkedList<GuardPolicy> getGuards() { + public List<GuardPolicy> getGuards() { return guards; } - public void setGuards(LinkedList<GuardPolicy> guards) { + public void setGuards(List<GuardPolicy> guards) { this.guards = guards; } @@ -97,5 +98,5 @@ public class ControlLoopGuard { return true; } - + } diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java index 4278b81e5..55b96e14e 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -21,7 +21,6 @@ package org.onap.policy.controlloop.policy.guard; -import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -31,24 +30,24 @@ public class GuardPolicy { private String name; private String description; private MatchParameters matchParameters; - private LinkedList<Constraint> limitConstraints; - + private List<Constraint> limitConstraints; + public GuardPolicy() { - //Do Nothing Empty Constructor. + //Do Nothing Empty Constructor. } - + public GuardPolicy(String id) { this.id = id; } - + public GuardPolicy(String name, MatchParameters matchParameters) { this.name = name; this.matchParameters = matchParameters; } - + /** * Constructor. - * + * * @param id id * @param name name * @param description description @@ -59,36 +58,34 @@ public class GuardPolicy { this.id = id; this.description = description; } - + /** * Constructor. - * + * * @param name name * @param matchParameters match parameters * @param limitConstraints limit constraints */ public GuardPolicy(String name, MatchParameters matchParameters, List<Constraint> limitConstraints) { this(name, matchParameters); - if (limitConstraints != null) { - this.limitConstraints = (LinkedList<Constraint>) limitConstraints; - } + this.limitConstraints = limitConstraints; } - - public GuardPolicy(String name, String description, MatchParameters matchParameters, + + public GuardPolicy(String name, String description, MatchParameters matchParameters, List<Constraint> limitConstraints) { this(name, matchParameters, limitConstraints); this.description = description; } - - public GuardPolicy(String id, String name, String description, MatchParameters matchParameters, + + public GuardPolicy(String id, String name, String description, MatchParameters matchParameters, List<Constraint> limitConstraints) { this(name, description, matchParameters, limitConstraints); this.id = id; } - + /** * Constructor. - * + * * @param policy copy object */ public GuardPolicy(GuardPolicy policy) { @@ -96,11 +93,9 @@ public class GuardPolicy { this.name = policy.name; this.description = policy.description; this.matchParameters = new MatchParameters(policy.matchParameters); - if (policy.limitConstraints != null) { - this.limitConstraints = policy.limitConstraints; - } + this.limitConstraints = policy.limitConstraints; } - + public String getId() { return id; } @@ -133,21 +128,21 @@ public class GuardPolicy { this.matchParameters = matchParameters; } - public LinkedList<Constraint> getLimit_constraints() { + public List<Constraint> getLimit_constraints() { return limitConstraints; } - public void setLimit_constraints(LinkedList<Constraint> limitConstraints) { + public void setLimit_constraints(List<Constraint> limitConstraints) { this.limitConstraints = limitConstraints; } public boolean isValid() { - return (id == null || name == null) ? false : true; + return (id != null && name != null); } - + @Override public String toString() { - return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", match_parameters=" + return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", match_parameters=" + matchParameters + ", limitConstraints=" + limitConstraints + "]"; } @@ -175,17 +170,18 @@ public class GuardPolicy { return false; } GuardPolicy other = (GuardPolicy) obj; - return equalsMayBeNull(description, other.description) + boolean isEq = equalsMayBeNull(description, other.description) && equalsMayBeNull(id, other.id) - && equalsMayBeNull(name, other.name) + && equalsMayBeNull(name, other.name); + return isEq && equalsMayBeNull(limitConstraints, other.limitConstraints) && equalsMayBeNull(matchParameters, other.matchParameters); } - + private boolean equalsMayBeNull(final Object obj1, final Object obj2) { if ( obj1 == null ) { return obj2 == null; } return obj1.equals(obj2); - } + } } diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java index a84783bc0..70d23bcba 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java @@ -2,15 +2,15 @@ * ============LICENSE_START======================================================= * policy-yaml * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -44,16 +44,16 @@ import org.yaml.snakeyaml.Yaml; public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { private static final String NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID = "No existing guard policy matching the id: "; - private static final String THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL = + private static final String THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL = "The id of target guard policy must not be null"; private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName()); private ControlLoopGuard clGuard; - + public ControlLoopGuardBuilderImpl(Guard guard) { clGuard = new ControlLoopGuard(); clGuard.setGuard(guard); } - + @Override public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException { if (policies == null) { @@ -112,26 +112,28 @@ public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { } private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException { - boolean exist = false; for (GuardPolicy policy: clGuard.getGuards()) { // // We could have only one guard policy matching the id // if (policy.getId().equals(id)) { - exist = true; - for (Constraint cons: constraints) { - if (!cons.isValid()) { - throw new BuilderException("Invalid guard constraint - some required fields are missing"); - } - if (policy.getLimit_constraints() == null) { - policy.setLimit_constraints(new LinkedList<>()); - } - policy.getLimit_constraints().add(cons); - } - break; + addConstraints(policy, constraints); + return true; + } + } + return false; + } + + private void addConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException { + for (Constraint cons: constraints) { + if (!cons.isValid()) { + throw new BuilderException("Invalid guard constraint - some required fields are missing"); + } + if (policy.getLimit_constraints() == null) { + policy.setLimit_constraints(new LinkedList<>()); } + policy.getLimit_constraints().add(cons); } - return exist; } @Override @@ -149,33 +151,35 @@ public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { } private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException { - boolean exist = false; for (GuardPolicy policy: clGuard.getGuards()) { // // We could have only one guard policy matching the id // if (policy.getId().equals(id)) { - exist = true; - for (Constraint cons: constraints) { - if (!cons.isValid()) { - throw new BuilderException("Invalid guard constraint - some required fields are missing"); - } - boolean removed = policy.getLimit_constraints().remove(cons); - if (!removed) { - throw new BuilderException("Unknown guard constraint: " + cons); - } - } - break; + removeConstraints(policy, constraints); + return true; + } + } + return false; + } + + private void removeConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException { + for (Constraint cons: constraints) { + if (!cons.isValid()) { + throw new BuilderException("Invalid guard constraint - some required fields are missing"); + } + boolean removed = policy.getLimit_constraints().remove(cons); + if (!removed) { + throw new BuilderException("Unknown guard constraint: " + cons); } } - return exist; } @Override public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException { if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) { throw new BuilderException("No guard policies exist"); - } + } if (id == null) { throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL); } @@ -192,11 +196,11 @@ public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { return this; } - + private class BuilderCompilerCallback implements ControlLoopCompilerCallback { private ResultsImpl results = new ResultsImpl(); - + @Override public boolean onWarning(String message) { results.addMessage(new MessageImpl(message, MessageLevel.WARNING)); @@ -209,13 +213,13 @@ public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { return false; } } - + @Override public ControlLoopGuard getControlLoopGuard() { return new ControlLoopGuard(this.clGuard); - } - - + } + + @Override public Results buildSpecification() { // |