diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-06-09 18:23:49 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-06-09 18:23:49 +0000 |
commit | 9deb780264c7d12aa932fbb3ff2d442fcb3e8d5a (patch) | |
tree | ca4f78ed517be2dc51592eeae6fbc08585931d67 /participant | |
parent | 5e7f7c3fdf6b9f1b04d0bfefa678e19823605ea7 (diff) | |
parent | 8a269b8f73838e46323502e671ec88ba09707f8e (diff) |
Merge "Convert Policy Participant to SpringBoot Application"
Diffstat (limited to 'participant')
17 files changed, 317 insertions, 919 deletions
diff --git a/participant/participant-impl/participant-impl-policy/pom.xml b/participant/participant-impl/participant-impl-policy/pom.xml index a5a75626e..a0176588b 100644 --- a/participant/participant-impl/participant-impl-policy/pom.xml +++ b/participant/participant-impl/participant-impl-policy/pom.xml @@ -31,4 +31,55 @@ <artifactId>policy-clamp-participant-impl-policy</artifactId> <name>${project.artifactId}</name> <description>Policy participant, that allows Policy to partake in control loops</description> + + <properties> + <springboot.version>2.4.4</springboot.version> + </properties> + + <dependencyManagement> + <dependencies> + <!-- Spring Boot BOM --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>${springboot.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-validation</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${springboot.version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + <phase>package</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> </project> diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/PolicyParticipantApplication.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/PolicyParticipantApplication.java new file mode 100644 index 000000000..bdc09c8b9 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/PolicyParticipantApplication.java @@ -0,0 +1,41 @@ +/*- + * ============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.participant.policy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.context.annotation.ComponentScan; + +/** + * Starter. + * + */ +@SpringBootApplication +@ComponentScan({"org.onap.policy.clamp.controlloop.participant.policy", + "org.onap.policy.clamp.controlloop.participant.intermediary"}) +@ConfigurationPropertiesScan("org.onap.policy.clamp.controlloop.participant.policy.main.parameters") +public class PolicyParticipantApplication { + + public static void main(String[] args) { + SpringApplication.run(PolicyParticipantApplication.class, args); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParametersConfig.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParametersConfig.java new file mode 100644 index 000000000..9c65d029c --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParametersConfig.java @@ -0,0 +1,40 @@ +/*- + * ============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.participant.policy.config; + +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ParametersConfig { + + @Value("${participant.file}") + private String file; + + @Bean + public ParticipantPolicyParameters participantPolicyParameters() throws ControlLoopException { + return new ParticipantPolicyParameterHandler().toParticipantDcaeParameters(file); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java new file mode 100644 index 000000000..afef1a5d2 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java @@ -0,0 +1,65 @@ +/*- + * ============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.participant.policy.config; + +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryFactory; +import org.onap.policy.clamp.controlloop.participant.policy.main.handler.ControlLoopElementHandler; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.provider.PolicyModelsProviderFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ParticipantConfig { + + /** + * Create ParticipantIntermediaryApi. + * + * @param parameters the Participant Policy Parameters + * @param controlLoopElementHandler the ControlLoop Element Handler + * @return ParticipantIntermediaryApi + */ + @Bean + public ParticipantIntermediaryApi participantIntermediaryApi(ParticipantPolicyParameters parameters, + ControlLoopElementHandler controlLoopElementHandler) { + ParticipantIntermediaryApi intermediaryApi = new ParticipantIntermediaryFactory().createApiImplementation(); + intermediaryApi.init(parameters.getIntermediaryParameters()); + intermediaryApi.registerControlLoopElementListener(controlLoopElementHandler); + controlLoopElementHandler.setIntermediaryApi(intermediaryApi); + return intermediaryApi; + } + + /** + * Create PolicyModelsProvider. + * + * @param parameters the Participant Policy Parameters + * @return PolicyModelsProvider + * @throws PfModelException in case of an exception + */ + @Bean + public PolicyModelsProvider databaseProvider(ParticipantPolicyParameters parameters) throws PfModelException { + return new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters.getDatabaseProviderParameters()); + } + +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java index 932ebbece..5b07568da 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java @@ -25,11 +25,13 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; +import lombok.Setter; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.provider.PolicyModelsProvider; @@ -38,15 +40,31 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; /** * This class handles implementation of controlLoopElement updates. */ +@Component public class ControlLoopElementHandler implements ControlLoopElementListener { private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class); - private static final Map<String, String> policyTypeMap = new LinkedHashMap<>(); - private static final Map<String, String> policyMap = new LinkedHashMap<>(); + private final Map<String, String> policyTypeMap = new LinkedHashMap<>(); + private final Map<String, String> policyMap = new LinkedHashMap<>(); + + private final PolicyModelsProvider databaseProvider; + + @Setter + private ParticipantIntermediaryApi intermediaryApi; + + /** + * constructor. + * + * @param databaseProvider the Policy Models Provider + */ + public ControlLoopElementHandler(PolicyModelsProvider databaseProvider) { + this.databaseProvider = databaseProvider; + } /** * Callback method to handle a control loop element state change. @@ -55,12 +73,10 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { * @param currentState the current state of the control loop element * @param newState the state to which the control loop element is changing to * @throws PfModelException in case of an exception - */ + */ @Override - public void controlLoopElementStateChange(UUID controlLoopElementId, - ControlLoopState currentState, + public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState, ControlLoopOrderedState newState) throws PfModelException { - PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); switch (newState) { case UNINITIALISED: try { @@ -70,14 +86,10 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { } break; case PASSIVE: - policyProvider.getIntermediaryApi() - .updateControlLoopElementState(controlLoopElementId, newState, - ControlLoopState.PASSIVE); + intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE); break; case RUNNING: - policyProvider.getIntermediaryApi() - .updateControlLoopElementState(controlLoopElementId, newState, - ControlLoopState.RUNNING); + intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING); break; default: LOGGER.debug("Unknown orderedstate {}", newState); @@ -85,25 +97,18 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { } } - private void deletePolicyData(UUID controlLoopElementId, - ControlLoopOrderedState newState) throws PfModelException { - PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider(); - PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); - if (policyMap != null) { - // Delete all policies of this controlLoop from policy framework - for (Entry<String, String> policy : policyMap.entrySet()) { - dbProvider.deletePolicy(policy.getKey(), policy.getValue()); - } + private void deletePolicyData(UUID controlLoopElementId, ControlLoopOrderedState newState) throws PfModelException { + // Delete all policies of this controlLoop from policy framework + for (Entry<String, String> policy : policyMap.entrySet()) { + databaseProvider.deletePolicy(policy.getKey(), policy.getValue()); } - if (policyTypeMap != null) { - // Delete all policy types of this control loop from policy framework - for (Entry<String, String> policy : policyTypeMap.entrySet()) { - dbProvider.deletePolicyType(policy.getKey(), policy.getValue()); - } + policyMap.clear(); + // Delete all policy types of this control loop from policy framework + for (Entry<String, String> policyType : policyTypeMap.entrySet()) { + databaseProvider.deletePolicyType(policyType.getKey(), policyType.getValue()); } - policyProvider.getIntermediaryApi() - .updateControlLoopElementState(controlLoopElementId, newState, - ControlLoopState.UNINITIALISED); + policyTypeMap.clear(); + intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.UNINITIALISED); } /** @@ -114,27 +119,25 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { * @throws PfModelException in case of an exception */ @Override - public void controlLoopElementUpdate(ControlLoopElement element, - ToscaServiceTemplate controlLoopDefinition) throws PfModelException { - PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider(); - PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); - - policyProvider.getIntermediaryApi() - .updateControlLoopElementState(element.getId(), element.getOrderedState(), ControlLoopState.PASSIVE); + public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition) + throws PfModelException { + intermediaryApi.updateControlLoopElementState(element.getId(), element.getOrderedState(), + ControlLoopState.PASSIVE); if (controlLoopDefinition.getPolicyTypes() != null) { for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) { policyTypeMap.put(policyType.getName(), policyType.getVersion()); } - dbProvider.createPolicyTypes(controlLoopDefinition); + databaseProvider.createPolicyTypes(controlLoopDefinition); } if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) { - for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition - .getToscaTopologyTemplate().getPolicies()) { - for (ToscaPolicy policy : foundPolicyMap.values()) { + for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition.getToscaTopologyTemplate() + .getPolicies()) { + for (Entry<String, ToscaPolicy> policyEntry : foundPolicyMap.entrySet()) { + ToscaPolicy policy = policyEntry.getValue(); policyMap.put(policy.getName(), policy.getVersion()); } } - dbProvider.createPolicies(controlLoopDefinition); + databaseProvider.createPolicies(controlLoopDefinition); } } @@ -144,16 +147,13 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { * @param controlLoopElementId controlloop element id */ @Override - public void handleStatistics(UUID controlLoopElementId) { - PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); - ControlLoopElement clElement = policyProvider.getIntermediaryApi() - .getControlLoopElement(controlLoopElementId); + public void handleStatistics(UUID controlLoopElementId) throws PfModelException { + ControlLoopElement clElement = intermediaryApi.getControlLoopElement(controlLoopElementId); if (clElement != null) { ClElementStatistics clElementStatistics = new ClElementStatistics(); clElementStatistics.setControlLoopState(clElement.getState()); clElementStatistics.setTimeStamp(Instant.now()); - policyProvider.getIntermediaryApi() - .updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); + intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); } } } diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java deleted file mode 100644 index d62e5f9f3..000000000 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java +++ /dev/null @@ -1,100 +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.participant.policy.main.handler; - -import java.io.IOException; -import java.util.List; -import java.util.Set; -import javax.ws.rs.core.Response; -import lombok.Getter; -import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler; -import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.common.endpoints.event.comm.TopicSink; -import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; - -/** - * This class handles policy participant and control loop elements. - * - * <p/>It is effectively a singleton that is started at system start. - */ -public class PolicyHandler extends ControlLoopHandler { - - private final ParticipantIntermediaryParameters participantParameters; - private final ParticipantPolicyParameters policyParameters; - - @Getter - private PolicyProvider policyProvider; - @Getter - private PolicyModelsProvider databaseProvider; - - /** - * Create a handler. - * - * @param parameters the parameters for access to the database - * @throws PfModelException in case of an exception - */ - public PolicyHandler(ParticipantPolicyParameters parameters) throws PfModelException { - super(parameters.getDatabaseProviderParameters()); - participantParameters = parameters.getIntermediaryParameters(); - policyParameters = parameters; - } - - public static PolicyHandler getInstance() { - return Registry.get(PolicyHandler.class.getName()); - } - - @Override - public Set<Class<?>> getProviderClasses() { - return null; - } - - @Override - public void startProviders() { - try { - policyProvider = new PolicyProvider(participantParameters); - databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider( - policyParameters.getDatabaseProviderParameters()); - } catch (PfModelException e) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Start providers failed ", e); - } - } - - @Override - public void stopProviders() { - try { - policyProvider.close(); - } catch (IOException e) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e); - } - - try { - databaseProvider.close(); - } catch (PfModelException e) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e); - } - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java deleted file mode 100644 index 420c77ee3..000000000 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java +++ /dev/null @@ -1,57 +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.participant.policy.main.handler; - -import java.io.Closeable; -import java.io.IOException; -import lombok.Getter; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryFactory; -import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; - -/** - * Provider class for policy participant. - */ -public class PolicyProvider implements Closeable { - @Getter - private final ParticipantIntermediaryApi intermediaryApi; - - private final ControlLoopElementHandler controlLoopElementHandler; - - /** - * Create a policy participant provider. - * - * @throws ControlLoopRuntimeException on errors creating the provider - */ - public PolicyProvider(ParticipantIntermediaryParameters participantParameters) - throws ControlLoopRuntimeException { - intermediaryApi = new ParticipantIntermediaryFactory().createApiImplementation(); - intermediaryApi.init(participantParameters); - controlLoopElementHandler = new ControlLoopElementHandler(); - intermediaryApi.registerControlLoopElementListener(controlLoopElementHandler); - } - - @Override - public void close() throws IOException { - intermediaryApi.close(); - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java index 98cea821a..0d8e70013 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java @@ -23,8 +23,7 @@ package org.onap.policy.clamp.controlloop.participant.policy.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.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyCommandLineArguments; -import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -39,37 +38,34 @@ public class ParticipantPolicyParameterHandler { /** * Read the parameters from the parameter file. * - * @param arguments the arguments passed to policy + * @param path the path passed to policy * @return the parameters read from the configuration file * @throws ControlLoopException on parameter exceptions */ - public ParticipantPolicyParameters getParameters(final ParticipantPolicyCommandLineArguments arguments) - throws ControlLoopException { + public ParticipantPolicyParameters toParticipantDcaeParameters(final String path) throws ControlLoopException { ParticipantPolicyParameters parameters = null; // Read the parameters try { // Read the parameters from JSON - File file = new File(arguments.getFullConfigurationFilePath()); + File file = new File(path); parameters = CODER.decode(file, ParticipantPolicyParameters.class); } catch (final CoderException e) { - final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath() - + "\"\n" + "(" + e.getClass().getSimpleName() + ")"; + final String errorMessage = + "error reading parameters from \"" + path + "\"\n" + "(" + e.getClass().getSimpleName() + ")"; throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage, e); } // The JSON processing returns null if there is an empty file if (parameters == null) { - final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\""; - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage); + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, "no parameters found in \"" + path + "\""); } // validate the parameters - final ValidationResult validationResult = parameters.validate(); + final BeanValidationResult validationResult = parameters.validate(); if (!validationResult.isValid()) { - String returnMessage = - "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n"; - returnMessage += validationResult.getResult(); + final String returnMessage = + "validation error(s) on parameters from \"" + path + "\"\n" + validationResult.getResult(); throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, returnMessage); } diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java deleted file mode 100644 index 9a6bfdf7d..000000000 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java +++ /dev/null @@ -1,141 +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.participant.policy.main.startstop; - -import java.util.Arrays; -import javax.ws.rs.core.Response; -import lombok.Getter; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.common.utils.resources.MessageConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class initiates Policy Participant. - */ -public class Main { - - private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); - - private ParticipantPolicyActivator activator; - - @Getter - private ParticipantPolicyParameters parameterGroup; - - /** - * Instantiates policy participant. - * - * @param args the command line arguments - */ - public Main(final String[] args) { - final String argumentString = Arrays.toString(args); - LOGGER.info("Starting the control loop participant service with arguments - {}", argumentString); - - // Check the arguments - final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); - try { - // The arguments return a string if there is a message to print and we should exit - final String argumentMessage = arguments.parse(args); - if (argumentMessage != null) { - LOGGER.info(argumentMessage); - return; - } - // Validate that the arguments are sane - arguments.validate(); - - // Read the parameters - parameterGroup = new ParticipantPolicyParameterHandler().getParameters(arguments); - - // Now, create the activator for the service - activator = new ParticipantPolicyActivator(parameterGroup); - - // Start the activator - activator.start(); - } catch (Exception exp) { - throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, - String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp); - } - - // Add a shutdown hook to shut everything down in an orderly manner - Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass()); - String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP); - LOGGER.info(successMsg); - } - - /** - * Check if main is running. - */ - public boolean isRunning() { - return activator != null && activator.isAlive(); - } - - /** - * Shut down Execution. - * - * @throws ControlLoopException on shutdown errors - */ - public void shutdown() throws ControlLoopException { - // clear the parameterGroup variable - parameterGroup = null; - - // clear the cl participant activator - if (activator != null) { - activator.stop(); - } - } - - /** - * The Class ClRuntimeShutdownHookClass terminates the policy participant - * when its run method is called. - */ - private class ClRuntimeShutdownHookClass extends Thread { - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() - */ - @Override - public void run() { - try { - // Shutdown the control loop participant service and wait for everything to stop - shutdown(); - } catch (final RuntimeException | ControlLoopException e) { - LOGGER.warn("error occured during shut down of the policy participant", e); - } - } - } - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(final String[] args) { // NOSONAR - /* - * NOTE: arguments are validated by the constructor, thus sonar is disabled. - */ - - new Main(args); - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java deleted file mode 100644 index 760f8267d..000000000 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java +++ /dev/null @@ -1,57 +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.participant.policy.main.startstop; - -import java.util.concurrent.atomic.AtomicReference; -import lombok.Getter; -import org.onap.policy.clamp.controlloop.participant.policy.main.handler.PolicyHandler; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.common.utils.services.ServiceManagerContainer; - -/** - * This class activates the policy participant. - * - */ -public class ParticipantPolicyActivator extends ServiceManagerContainer { - @Getter - private final ParticipantPolicyParameters parameters; - - /** - * Instantiate the activator for the policy participant. - * - * @param parameters the parameters for the policy participant - */ - public ParticipantPolicyActivator(final ParticipantPolicyParameters parameters) { - this.parameters = parameters; - - final AtomicReference<PolicyHandler> policyHandler = new AtomicReference<>(); - - // @formatter:off - addAction("Policy Handler", - () -> policyHandler.set(new PolicyHandler(parameters)), - () -> policyHandler.get().close()); - - addAction("Policy Providers", - () -> policyHandler.get().startProviders(), - () -> policyHandler.get().stopProviders()); - // @formatter:on - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java deleted file mode 100644 index af7a189f6..000000000 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java +++ /dev/null @@ -1,145 +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.participant.policy.main.startstop; - -import java.util.Arrays; -import javax.ws.rs.core.Response; -import lombok.Getter; -import lombok.Setter; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.apache.commons.lang3.StringUtils; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * This class reads and handles command line parameters for the policy participant. - * - */ -public class ParticipantPolicyCommandLineArguments { - private static final String FILE_MESSAGE_PREAMBLE = " file \""; - private static final int HELP_LINE_LENGTH = 120; - - private final Options options; - private final CommonCommandLineArguments commonCommandLineArguments; - - @Getter() - @Setter() - private String configurationFilePath = null; - - /** - * Construct the options for the policy participant. - */ - public ParticipantPolicyCommandLineArguments() { - options = new Options(); - commonCommandLineArguments = new CommonCommandLineArguments(options); - } - - /** - * Construct the options for the CLI editor and parse in the given arguments. - * - * @param args The command line arguments - */ - public ParticipantPolicyCommandLineArguments(final String[] args) { - // Set up the options with the default constructor - this(); - - // Parse the arguments - try { - parse(args); - } catch (final ControlLoopException e) { - throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, - "parse error on policy participant parameters", e); - } - } - - /** - * Parse the command line options. - * - * @param args The command line arguments - * @return a string with a message for help and version, or null if there is no message - * @throws ControlLoopException on command argument errors - */ - public String parse(final String[] args) throws ControlLoopException { - // Clear all our arguments - setConfigurationFilePath(null); - CommandLine commandLine = null; - try { - commandLine = new DefaultParser().parse(options, args); - } catch (final ParseException e) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - "invalid command line arguments specified : " + e.getMessage()); - } - - // Arguments left over after Commons CLI does its stuff - final String[] remainingArgs = commandLine.getArgs(); - - if (remainingArgs.length > 0) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - "too many command line arguments specified : " + Arrays.toString(args)); - } - - if (commandLine.hasOption('h')) { - return commonCommandLineArguments.help(Main.class.getName(), options); - } - - if (commandLine.hasOption('v')) { - return commonCommandLineArguments.version(); - } - - if (commandLine.hasOption('c')) { - setConfigurationFilePath(commandLine.getOptionValue('c')); - } - - return null; - } - - /** - * Validate the command line options. - * - * @throws ControlLoopException on command argument validation errors - */ - public void validate() throws ControlLoopException { - commonCommandLineArguments.validate(configurationFilePath); - } - - /** - * Gets the full expanded configuration file path. - * - * @return the configuration file path - */ - public String getFullConfigurationFilePath() { - return ResourceUtils.getFilePath4Resource(getConfigurationFilePath()); - } - - /** - * Check set configuration file path. - * - * @return true, if check set configuration file path - */ - public boolean checkSetConfigurationFilePath() { - return !StringUtils.isEmpty(configurationFilePath); - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml new file mode 100644 index 000000000..79eca8f92 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml @@ -0,0 +1,3 @@ + +participant: + file: src/main/resources/config/PolicyParticipantConfig.json diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/TestPolicyHandler.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java index abc3e71b8..2a6f4b005 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/TestPolicyHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java @@ -18,117 +18,112 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.main.handler; +package org.onap.policy.clamp.controlloop.participant.policy.endtoend; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.clamp.controlloop.common.ControlLoopConstants; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData; -import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.Main; -import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyActivator; import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; -import org.onap.policy.common.utils.services.Registry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; -public class TestPolicyHandler { +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(properties = "participant.file=src/test/resources/parameters/TestParameters.json") +class ParticipantPolicyTest { - private static ControlLoopUpdateListener clUpdateListener; - private ControlLoopStateChangeListener clStateChangeListener; - private static ParticipantControlLoopUpdate participantControlLoopUpdateMsg; - private ParticipantControlLoopStateChange participantControlLoopStateChangeMsg; - private static final String PARTICIPANTS_ENDPOINT = "participants"; - private static final String ELEMENTS_ENDPOINT = "elements"; + private static final Object lockit = new Object(); private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; - private static final Object lockit = new Object(); - static CommonTestData commonTestData = new CommonTestData(); - - /** - * Setup before class, instantiate Main. - * - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - Registry.newRegistry(); - final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"}; - Main main = new Main(configParameters); - assertTrue(main.isRunning()); - TestListenerUtils.initParticipantHandler(); - - clUpdateListener = new ControlLoopUpdateListener( - PolicyHandler.getInstance() - .getPolicyProvider() - .getIntermediaryApi() - .getParticipantHandler()); - participantControlLoopUpdateMsg = - TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); - } + + @Autowired + private ParticipantIntermediaryApi participantIntermediaryApi; @Test - public void testUpdatePolicyTypes() throws Exception { + void testUpdatePolicyTypes() { + ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + // Verify that the ToscaServicetemplate has policy_types assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes()); synchronized (lockit) { + ControlLoopUpdateListener clUpdateListener = + new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler()); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", - TestListenerUtils.getParticipantHandler().getParticipantId().getName()); + participantIntermediaryApi.getParticipantHandler().getParticipantId().getName()); } @Test - public void testUpdatePolicies() throws Exception { + void testUpdatePolicies() throws Exception { + ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + // Add policies to the toscaServiceTemplate TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition()); // Verify that the ToscaServicetemplate has policies - assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition() - .getToscaTopologyTemplate().getPolicies()); + assertNotNull( + participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies()); synchronized (lockit) { + ControlLoopUpdateListener clUpdateListener = + new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler()); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", - TestListenerUtils.getParticipantHandler().getParticipantId().getName()); + participantIntermediaryApi.getParticipantHandler().getParticipantId().getName()); } @Test - public void testDeletePoliciesAndPolicyTypes() throws Exception { + void testDeletePoliciesAndPolicyTypes() throws Exception { + ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + // Add policies to the toscaServiceTemplate TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition()); // Verify that the ToscaServicetemplate has policies - assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition() - .getToscaTopologyTemplate().getPolicies()); + assertNotNull( + participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies()); synchronized (lockit) { + ControlLoopUpdateListener clUpdateListener = + new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler()); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", - TestListenerUtils.getParticipantHandler().getParticipantId().getName()); + participantIntermediaryApi.getParticipantHandler().getParticipantId().getName()); - clStateChangeListener = new ControlLoopStateChangeListener(TestListenerUtils.getParticipantHandler()); - participantControlLoopStateChangeMsg = + ControlLoopStateChangeListener clStateChangeListener = + new ControlLoopStateChangeListener(participantIntermediaryApi.getParticipantHandler()); + ParticipantControlLoopStateChange participantControlLoopStateChangeMsg = TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED); participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED); clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg); // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", - TestListenerUtils.getParticipantHandler().getParticipantId().getName()); + participantIntermediaryApi.getParticipantHandler().getParticipantId().getName()); } } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/TestParticipantPolicyParameterHandler.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/TestParticipantPolicyParameterHandler.java index dd62dbfc4..8c12a4038 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/TestParticipantPolicyParameterHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/TestParticipantPolicyParameterHandler.java @@ -20,79 +20,39 @@ package org.onap.policy.clamp.controlloop.participant.policy.main.parameters; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.io.FileNotFoundException; -import org.apache.commons.io.DirectoryWalker.CancelException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyCommandLineArguments; import org.onap.policy.common.utils.coder.CoderException; -public class TestParticipantPolicyParameterHandler { +class TestParticipantPolicyParameterHandler { @Test - public void testParameterHandlerNoParameterFile() throws ControlLoopException { - final String[] emptyArgumentString = { "-c", "src/test/resources/parameters/NoParametersFile.json" }; + void testParameterHandlerNoParameterFile() throws ControlLoopException { + final String path = "src/test/resources/parameters/NoParametersFile.json"; - final ParticipantPolicyCommandLineArguments emptyArguments = new ParticipantPolicyCommandLineArguments(); - emptyArguments.parse(emptyArgumentString); - - assertThatThrownBy(() -> new ParticipantPolicyParameterHandler().getParameters(emptyArguments)) + assertThatThrownBy(() -> new ParticipantPolicyParameterHandler().toParticipantDcaeParameters(path)) .hasCauseInstanceOf(CoderException.class) .hasRootCauseInstanceOf(FileNotFoundException.class); } @Test - public void testParameterHandlerInvalidParameters() throws ControlLoopException { - final String[] invalidArgumentString = { "-c", "src/test/resources/parameters/InvalidParameters.json" }; - - final ParticipantPolicyCommandLineArguments invalidArguments = - new ParticipantPolicyCommandLineArguments(); - invalidArguments.parse(invalidArgumentString); + void testParameterHandlerInvalidParameters() throws ControlLoopException { + final String path = "src/test/resources/parameters/InvalidParameters.json"; - assertThatThrownBy(() -> new ParticipantPolicyParameterHandler().getParameters(invalidArguments)) + assertThatThrownBy(() -> new ParticipantPolicyParameterHandler().toParticipantDcaeParameters(path)) .hasMessageStartingWith("error reading parameters from") .hasCauseInstanceOf(CoderException.class); } @Test - public void testParticipantPolicyParameters() throws ControlLoopException { - final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json" }; - - final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); - arguments.parse(participantConfigParameters); - + void testParticipantParameterGroup() throws ControlLoopException { + final String path = "src/test/resources/parameters/TestParameters.json"; final ParticipantPolicyParameters parGroup = new ParticipantPolicyParameterHandler() - .getParameters(arguments); - assertTrue(arguments.checkSetConfigurationFilePath()); + .toParticipantDcaeParameters(path); assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, parGroup.getName()); } - - @Test - public void testParticipantVersion() throws ControlLoopException { - final String[] participantConfigParameters = { "-v" }; - final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); - final String version = arguments.parse(participantConfigParameters); - assertThat(arguments.parse(participantConfigParameters)).startsWith( - "ONAP Tosca defined control loop Participant"); - } - - @Test - public void testParticipantHelp() throws ControlLoopException { - final String[] participantConfigParameters = { "-h" }; - final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); - assertThat(arguments.parse(participantConfigParameters)).startsWith("usage:"); - } - - @Test - public void testParticipantInvalidOption() throws ControlLoopException { - final String[] participantConfigParameters = { "-d" }; - final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); - assertThatThrownBy(() -> arguments.parse(participantConfigParameters)) - .hasMessageStartingWith("invalid command line arguments specified"); - } } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestMain.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestMain.java deleted file mode 100644 index f77b678c0..000000000 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestMain.java +++ /dev/null @@ -1,143 +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.participant.policy.main.startstop; - -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.clamp.controlloop.common.ControlLoopConstants; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.common.utils.resources.MessageConstants; -import org.onap.policy.common.utils.services.Registry; - -public class TestMain { - - /** - * Set up. - */ - @BeforeClass - public static void setUp() { - Registry.newRegistry(); - } - - /** - * Shuts "main" down. - * - * @throws Exception if an error occurs - */ - @AfterClass - public static void tearDown() throws Exception { - // shut down activator - final ParticipantPolicyActivator activator = - Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, - ParticipantPolicyActivator.class, null); - if (activator != null && activator.isAlive()) { - activator.shutdown(); - } - } - - @Test - public void testMain_Help() { - final String[] configParameters = {"-h"}; - Main main = new Main(configParameters); - assertFalse(main.isRunning()); - } - - @Test - public void testMain_Version() { - final String[] configParameters = {"-v"}; - Main main = new Main(configParameters); - assertFalse(main.isRunning()); - } - - @Test - public void testMain_Valid() { - final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"}; - Main main = new Main(configParameters); - assertTrue(main.isRunning()); - - assertThatCode(() -> main.shutdown()).doesNotThrowAnyException(); - - assertFalse(main.isRunning()); - } - - @Test - public void testMain_NoParameter() { - assertThatConfigParameterThrownException(new String[] {}); - } - - @Test - public void testMain_FilePathNotDefined() { - assertThatConfigParameterThrownException(new String[] {"-c"}); - } - - @Test - public void testMain_TooManyCommand() { - assertThatConfigParameterThrownException(new String[] {"-h", "d"}); - } - - @Test - public void testMain_WrongParameter() { - assertThatConfigParameterThrownException(new String[] {"-d"}); - } - - private void assertThatConfigParameterThrownException(final String[] configParameters) { - assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class) - .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP)); - } - - @Test - public void testParticipant_NoFileWithThisName() { - assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json"); - } - - @Test - public void testParticipant_NotValidFile() { - assertThatConfigFileThrownException("src/test/resources/parameters"); - } - - @Test - public void testParticipant_NoParameters() { - assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json"); - } - - @Test - public void testParticipant_InvalidParameters() { - assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json"); - } - - @Test - public void testParticipant_WrongJsonFormat() { - assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json"); - } - - private void assertThatConfigFileThrownException(final String configFilePath) { - final String[] configParameters = new String[] {"-c", configFilePath}; - assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class) - .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP)); - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestParticipantPolicyActivator.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestParticipantPolicyActivator.java deleted file mode 100644 index afb0f6cb4..000000000 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/TestParticipantPolicyActivator.java +++ /dev/null @@ -1,88 +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.participant.policy.main.startstop; - -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.common.utils.services.Registry; - -public class TestParticipantPolicyActivator { - - private static ParticipantPolicyActivator activator; - - /** - * Initializes an activator. - * - * @throws Exception if an error occurs - */ - @BeforeClass - public static void setUp() throws Exception { - Registry.newRegistry(); - final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json"}; - final ParticipantPolicyCommandLineArguments arguments = - new ParticipantPolicyCommandLineArguments(participantConfigParameters); - final ParticipantPolicyParameters parGroup = - new ParticipantPolicyParameterHandler().getParameters(arguments); - activator = new ParticipantPolicyActivator(parGroup); - } - - /** - * Method for cleanup after each test. - * - * @throws Exception if an error occurs - */ - @AfterClass - public static void teardown() throws Exception { - // shut down activator - if (activator != null && activator.isAlive()) { - activator.shutdown(); - } - } - - @Test - public void testParticipantActivator() { - activator.start(); - assertTrue(activator.isAlive()); - assertTrue(activator.getParameters().isValid()); - assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, activator.getParameters().getName()); - - // repeat - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.start()); - assertTrue(activator.isAlive()); - assertTrue(activator.getParameters().isValid()); - - activator.shutdown(); - assertFalse(activator.isAlive()); - - // repeat - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.shutdown()); - assertFalse(activator.isAlive()); - } -} diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java index 4f3d6d62c..794b9ff69 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java @@ -29,7 +29,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.UUID; -import lombok.Getter; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; @@ -39,10 +38,7 @@ import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.Parti import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange; -import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; -import org.onap.policy.clamp.controlloop.participant.policy.main.handler.PolicyProvider; import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData; -import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -61,27 +57,9 @@ public class TestListenerUtils { static CommonTestData commonTestData = new CommonTestData(); private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); - @Getter - private static ParticipantHandler participantHandler; - private TestListenerUtils() {} /** - * Method to initialize participantHandler. - */ - public static void initParticipantHandler() { - - final ParticipantPolicyParameters participantParameters = commonTestData.toObject( - commonTestData.getParticipantPolicyParametersMap(CommonTestData.PARTICIPANT_GROUP_NAME), - ParticipantPolicyParameters.class); - - PolicyProvider policyProvider = - new PolicyProvider(participantParameters.getIntermediaryParameters()); - - participantHandler = policyProvider.getIntermediaryApi().getParticipantHandler(); - } - - /** * Method to create a controlLoop from a yaml file. * * @return ControlLoop controlloop |