From 2ddb38bd7f0c5e6b74f5ac74685120f32ce9e9de Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 13 Jul 2021 10:30:02 +0100 Subject: Refactor Control Loop Parameters in Spring Issue-ID: POLICY-3461 Change-Id: I54351d02da9bfa2b775f40fad0a12a4f32994cd9 Signed-off-by: FrancescoFioraEst --- .../clamp/controlloop/runtime/Application.java | 2 + .../runtime/config/PropertiesConfig.java | 38 ------------ .../main/parameters/ClRuntimeParameterGroup.java | 43 ++++++++----- .../main/parameters/ClRuntimeParameterHandler.java | 72 ---------------------- .../main/parameters/ParticipantParameters.java | 28 ++++----- .../ParticipantStateChangeParameters.java | 19 ++---- .../parameters/ParticipantUpdateParameters.java | 20 ++---- 7 files changed, 54 insertions(+), 168 deletions(-) delete mode 100644 runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/PropertiesConfig.java delete mode 100644 runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterHandler.java (limited to 'runtime-controlloop/src/main/java') diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java index 6b772513c..28814b354 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java @@ -22,11 +22,13 @@ package org.onap.policy.clamp.controlloop.runtime; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan({"org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider", "org.onap.policy.clamp.controlloop.runtime"}) +@ConfigurationPropertiesScan("org.onap.policy.clamp.controlloop.runtime.main.parameters") public class Application { public static void main(String[] args) { diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/PropertiesConfig.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/PropertiesConfig.java deleted file mode 100644 index 04bd35da3..000000000 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/PropertiesConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.runtime.config; - -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; -import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterHandler; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class PropertiesConfig { - - @Bean - public ClRuntimeParameterGroup clRuntimeParameterGroup(@Value("${runtime.file}") String file) - throws ControlLoopException { - return new ClRuntimeParameterHandler().getParameters(file); - } -} diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java index 433eeeebb..f46b90294 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java @@ -20,41 +20,52 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; -import javax.validation.constraints.NotBlank; +import javax.validation.Valid; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; import lombok.Getter; -import org.onap.policy.common.endpoints.parameters.RestServerParameters; +import lombok.Setter; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; -import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.validation.ParameterGroupConstraint; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.validation.annotation.Validated; /** * Class to hold all parameters needed for the Control Loop runtime component. * */ -@NotNull -@NotBlank +@Validated @Getter -public class ClRuntimeParameterGroup extends ParameterGroupImpl { - private RestServerParameters restServerParameters; +@Setter +@ConfigurationProperties(prefix = "runtime") +public class ClRuntimeParameterGroup { + + @NotNull + @ParameterGroupConstraint private PolicyModelsProviderParameters databaseProviderParameters; + + @Valid + @NotNull private ParticipantParameters participantParameters; + + @NotNull + @ParameterGroupConstraint private TopicParameterGroup topicParameterGroup; + @Min(value = 0) private long supervisionScannerIntervalSec; + + @Min(value = 0) private long participantStateChangeIntervalSec; + + @Min(value = 0) private long participantClUpdateIntervalSec; + + @Min(value = 0) private long participantClStateChangeIntervalSec; private long participantRegisterAckIntervalSec; private long participantDeregisterAckIntervalSec; private long participantUpdateIntervalSec; - /** - * Create the Control Loop parameter group. - * - * @param name the parameter group name - */ - public ClRuntimeParameterGroup(final String name) { - super(name); - } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterHandler.java deleted file mode 100644 index bcf1124d2..000000000 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterHandler.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.runtime.main.parameters; - -import java.io.File; -import javax.ws.rs.core.Response; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.common.parameters.ValidationResult; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; - -/** - * This class handles reading, parsing and validating of control loop runtime parameters from JSON files. - */ -public class ClRuntimeParameterHandler { - - private static final Coder CODER = new StandardCoder(); - - /** - * Read the parameters from the parameter file. - * - * @param path the path passed to control loop runtime - * @return the parameters read from the configuration file - * @throws ControlLoopException on parameter exceptions - */ - public ClRuntimeParameterGroup getParameters(final String path) throws ControlLoopException { - ClRuntimeParameterGroup clRuntimeParameterGroup = null; - - // Read the parameters - try { - // Read the parameters from JSON - File file = new File(path); - clRuntimeParameterGroup = CODER.decode(file, ClRuntimeParameterGroup.class); - } catch (final CoderException e) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - "error reading parameters from \"" + path + "\"\n" + "(" + e.getClass().getSimpleName() + ")", e); - } - - // The JSON processing returns null if there is an empty file - if (clRuntimeParameterGroup == null) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, "no parameters found in \"" + path + "\""); - } - - // validate the parameters - final ValidationResult validationResult = clRuntimeParameterGroup.validate(); - if (!validationResult.isValid()) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - "validation error(s) on parameters from \"" + path + "\"\n" + validationResult.getResult()); - } - - return clRuntimeParameterGroup; - } -} diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java index dfc1b2806..a4e84af0d 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java @@ -19,19 +19,20 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; import java.util.concurrent.TimeUnit; +import javax.validation.Valid; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; import lombok.Getter; -import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; +import lombok.Setter; +import org.springframework.validation.annotation.Validated; /** * Parameters for communicating with participants. */ -@NotNull -@NotBlank @Getter -public class ParticipantParameters extends ParameterGroupImpl { +@Setter +@Validated +public class ParticipantParameters { /** * Default maximum message age, in milliseconds, that should be examined. Any message @@ -39,21 +40,18 @@ public class ParticipantParameters extends ParameterGroupImpl { */ public static final long DEFAULT_MAX_AGE_MS = TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES); - @Min(1) private long heartBeatMs; @Min(1) private long maxMessageAgeMs = DEFAULT_MAX_AGE_MS; + @Valid + @NotNull private ParticipantUpdateParameters updateParameters; - private ParticipantStateChangeParameters stateChangeParameters; + @Valid + @NotNull + private ParticipantStateChangeParameters stateChangeParameters; - /** - * Constructs the object. - */ - public ParticipantParameters() { - super(ParticipantParameters.class.getSimpleName()); - } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantStateChangeParameters.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantStateChangeParameters.java index 2eea4ab51..a3e2eee2e 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantStateChangeParameters.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantStateChangeParameters.java @@ -18,19 +18,18 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; +import javax.validation.constraints.Min; import lombok.Getter; -import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; +import lombok.Setter; +import org.springframework.validation.annotation.Validated; /** * Parameters for Participant STATE-CHANGE requests. */ -@NotNull -@NotBlank @Getter -public class ParticipantStateChangeParameters extends ParameterGroupImpl { +@Setter +@Validated +public class ParticipantStateChangeParameters { /** * Maximum number of times to re-send a request to a PDP. @@ -44,10 +43,4 @@ public class ParticipantStateChangeParameters extends ParameterGroupImpl { @Min(value = 0) private long maxWaitMs; - /** - * Constructs the object. - */ - public ParticipantStateChangeParameters() { - super(ParticipantStateChangeParameters.class.getSimpleName()); - } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java index 2af5be534..8102fe90e 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java @@ -18,19 +18,18 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; +import javax.validation.constraints.Min; import lombok.Getter; -import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; +import lombok.Setter; +import org.springframework.validation.annotation.Validated; /** * Parameters for Participant UPDATE requests. */ -@NotNull -@NotBlank @Getter -public class ParticipantUpdateParameters extends ParameterGroupImpl { +@Setter +@Validated +public class ParticipantUpdateParameters { /** * Maximum number of times to re-send a request to a PDP. @@ -44,11 +43,4 @@ public class ParticipantUpdateParameters extends ParameterGroupImpl { @Min(value = 0) private long maxWaitMs; - /** - * Constructs the object. - */ - public ParticipantUpdateParameters() { - super(ParticipantUpdateParameters.class.getSimpleName()); - } } - -- cgit 1.2.3-korg