diff options
Diffstat (limited to 'participant')
41 files changed, 1566 insertions, 518 deletions
diff --git a/participant/participant-impl/participant-impl-dcae/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-dcae/src/main/resources/config/application.yaml index 44ba5b3e8..a873f76b3 100644 --- a/participant/participant-impl/participant-impl-dcae/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-dcae/src/main/resources/config/application.yaml @@ -1,3 +1,5 @@ +server: + port: 8081 participant: name: ControlLoopParticipantDcae @@ -28,7 +30,7 @@ participant: aaf: false intermediaryParameters: name: Participant parameters - reportingTimeInterval: 120000 + reportingTimeIntervalMs: 120000 description: Participant Description participantId: name: DCAEParticipant0 @@ -37,15 +39,19 @@ participant: name: org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant version: 2.3.4 clampControlLoopTopics: - topicSources[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap - fetchTimeout: 15000 - topicSinks[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap + topicSources: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap checkCount: 10 secCount: 10 jsonBodyConsulPath: src/main/resources/parameters/consul.json diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/PartecipantDcaeTest.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java index 3f5e42542..a3fcfdd9e 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/PartecipantDcaeTest.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java @@ -32,8 +32,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.integration.ClientAndServer; 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.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData; import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener; @@ -50,7 +50,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @TestPropertySource(locations = {"classpath:application_test.properties"}) -class PartecipantDcaeTest { +class ParticipantDcaeTest { private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; @@ -103,74 +103,74 @@ class PartecipantDcaeTest { } @Test - void testParticipantControlLoopStateChangeMessageListener() { - ParticipantControlLoopStateChange participantControlLoopStateChangeMsg = + void testControlLoopStateChangeMessageListener() { + ControlLoopStateChange controlLoopStateChangeMsg = TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED); - participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE); + controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE); ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler); - clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg); - assertEquals(ControlLoopOrderedState.PASSIVE, participantControlLoopStateChangeMsg.getOrderedState()); + clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg); + assertEquals(ControlLoopOrderedState.PASSIVE, controlLoopStateChangeMsg.getOrderedState()); - participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING); - clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg); - assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState()); + controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING); + clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg); + assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState()); - participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING); - clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg); - assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState()); + controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING); + clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg); + assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState()); } @Test void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getParticipantId().setName("DummyName"); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getParticipantId().setName("DummyName"); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); // Verify the content in participantHandler - assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(), + assertNotEquals(controlLoopUpdateMsg.getParticipantId().getName(), participantHandler.getParticipantId().getName()); } @Test void testControlLoopUpdateListenerPassive() throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); // Verify the content in participantHandler - assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId()); + assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId()); assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size()); } @Test void testControlLoopUpdateListenerUninitialised() throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); // Verify the content in participantHandler - assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId()); + assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId()); assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size()); } @Test void testControlLoopUpdateListenerString() throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); - - assertThat(participantControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED"); - ParticipantControlLoopUpdate copyParticipantControlLoopUpdateMsg = - TestListenerUtils.createCopyControlLoopUpdateMsg(participantControlLoopUpdateMsg); - assertThat(copyParticipantControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED"); - assertNotEquals(participantControlLoopUpdateMsg, copyParticipantControlLoopUpdateMsg); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); + + assertThat(controlLoopUpdateMsg.toString()).contains("state=UNINITIALISED"); + ControlLoopUpdate copyControlLoopUpdateMsg = + TestListenerUtils.createCopyControlLoopUpdateMsg(controlLoopUpdateMsg); + assertThat(copyControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED"); + assertNotEquals(controlLoopUpdateMsg, copyControlLoopUpdateMsg); } } diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/parameters/CommonTestData.java index 4b6dcd619..a28a6ece1 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/parameters/CommonTestData.java @@ -172,7 +172,7 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); if (!isEmpty) { map.put("name", "Participant parameters"); - map.put("reportingTimeInterval", TIME_INTERVAL); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); map.put("description", DESCRIPTION); map.put("participantId", getParticipantId()); map.put("participantType", getParticipantId()); diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java index 4579b52bf..136d0e500 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java @@ -30,10 +30,8 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop 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.models.controlloop.concepts.ParticipantState; -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.models.messages.dmaap.participant.ParticipantHealthCheck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -88,35 +86,15 @@ public class TestListenerUtils { } /** - * Method to create ParticipantStateChange message from the arguments passed. - * - * @param participantState participant State - * - * @return ParticipantStateChange message - */ - public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) { - final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange(); - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("DCAEParticipant0"); - participantId.setVersion("1.0.0"); - - participantStateChangeMsg.setParticipantId(participantId); - participantStateChangeMsg.setTimestamp(Instant.now()); - participantStateChangeMsg.setState(participantState); - - return participantStateChangeMsg; - } - - /** * Method to create ControlLoopStateChange message from the arguments passed. * * @param controlLoopOrderedState controlLoopOrderedState * - * @return ParticipantControlLoopStateChange message + * @return ControlLoopStateChange message */ - public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg( + public static ControlLoopStateChange createControlLoopStateChangeMsg( final ControlLoopOrderedState controlLoopOrderedState) { - final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange(); + final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); controlLoopId.setName("PMSHInstance0"); @@ -126,21 +104,21 @@ public class TestListenerUtils { participantId.setName("DCAEParticipant0"); participantId.setVersion("1.0.0"); - participantClStateChangeMsg.setControlLoopId(controlLoopId); - participantClStateChangeMsg.setParticipantId(participantId); - participantClStateChangeMsg.setTimestamp(Instant.now()); - participantClStateChangeMsg.setOrderedState(controlLoopOrderedState); + clStateChangeMsg.setControlLoopId(controlLoopId); + clStateChangeMsg.setParticipantId(participantId); + clStateChangeMsg.setTimestamp(Instant.now()); + clStateChangeMsg.setOrderedState(controlLoopOrderedState); - return participantClStateChangeMsg; + return clStateChangeMsg; } /** * Method to create ControlLoopUpdateMsg. * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message */ - public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() { - final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate(); + public static ControlLoopUpdate createControlLoopUpdateMsg() { + final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); controlLoopId.setName("PMSHInstance0"); controlLoopId.setVersion("1.0.0"); @@ -177,7 +155,6 @@ public class TestListenerUtils { controlLoop.setVersion("1.0.0"); controlLoop.setDefinition(controlLoopId); clUpdateMsg.setControlLoop(controlLoop); - clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate); return clUpdateMsg; } @@ -185,48 +162,25 @@ public class TestListenerUtils { /** * Method to create a deep copy of ControlLoopUpdateMsg. * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message */ - public static ParticipantControlLoopUpdate createCopyControlLoopUpdateMsg(ParticipantControlLoopUpdate cpy) { - return new ParticipantControlLoopUpdate(cpy); - } - - /** - * Method to create ParticipantHealthCheck message. - * - * @return ParticipantHealthCheck message - */ - public static ParticipantHealthCheck createParticipantHealthCheckMsg() { - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("DCAEParticipant0"); - participantId.setVersion("1.0.0"); - - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); - controlLoopId.setName("PMSHInstance0"); - controlLoopId.setVersion("1.0.0"); - - final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck(); - participantHealthCheckMsg.setParticipantId(participantId); - participantHealthCheckMsg.setControlLoopId(controlLoopId); - participantHealthCheckMsg.setTimestamp(Instant.now()); - participantHealthCheckMsg.setState(ParticipantState.PASSIVE); - - return participantHealthCheckMsg; + public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) { + return new ControlLoopUpdate(cpy); } /** - * Method to create ParticipantControlLoopUpdate using the arguments passed. + * Method to create ControlLoopUpdate using the arguments passed. * * @param jsonFilePath the path of the controlloop content * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message * @throws CoderException exception while reading the file to object */ - public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) + public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = - CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class); - return participantControlLoopUpdateMsg; + ControlLoopUpdate controlLoopUpdateMsg = + CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); + return controlLoopUpdateMsg; } private static ToscaServiceTemplate testControlLoopRead() { diff --git a/participant/participant-impl/participant-impl-http/pom.xml b/participant/participant-impl/participant-impl-http/pom.xml new file mode 100644 index 000000000..85d65e707 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/pom.xml @@ -0,0 +1,50 @@ +<!-- + ============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========================================================= +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.policy.clamp.participant</groupId> + <artifactId>policy-clamp-participant-impl</artifactId> + <version>6.1.2-SNAPSHOT</version> + </parent> + + <artifactId>policy-clamp-participant-impl-http</artifactId> + <name>${project.artifactId}</name> + <description>HTTP participant, that performs CRUD operations on Microservices through REST</description> + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + <phase>package</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/Application.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/Application.java new file mode 100644 index 000000000..dc36392f7 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/Application.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.http; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Starter. + * + */ +@SpringBootApplication +public class Application { + /** + * Main class. + * + * @param args args + */ + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantStateChangeListener.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/config/ParticipantConfig.java index ec6548a7c..ce013a765 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantStateChangeListener.java +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/config/ParticipantConfig.java @@ -18,25 +18,26 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.intermediary.comm; +package org.onap.policy.clamp.controlloop.participant.http.config; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange; -import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; -import org.springframework.stereotype.Component; +import org.onap.policy.clamp.controlloop.participant.http.main.handler.ControlLoopElementHandler; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; -/** - * Listener for Participant State Change messages sent by CLAMP. - * - */ -@Component -public class ParticipantStateChangeListener extends ParticipantListener<ParticipantStateChange> { +@Configuration +public class ParticipantConfig { /** - * Constructs the object. + * Register ControlLoopElementListener. * - * @param participantHandler the handler for managing the state of the participant + * @param intermediaryApi the ParticipantIntermediaryApi + * @param clElementHandler the ControlLoop Element Handler */ - public ParticipantStateChangeListener(final ParticipantHandler participantHandler) { - super(ParticipantStateChange.class, participantHandler, participantHandler::handleParticipantStateChange); + @Autowired + public void registerControlLoopElementListener(ParticipantIntermediaryApi intermediaryApi, + ControlLoopElementHandler clElementHandler) { + intermediaryApi.registerControlLoopElementListener(clElementHandler); + clElementHandler.setIntermediaryApi(intermediaryApi); } } diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/exception/HttpWebClientException.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/exception/HttpWebClientException.java new file mode 100644 index 000000000..b10af884c --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/exception/HttpWebClientException.java @@ -0,0 +1,31 @@ +/*- + * ============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.http.main.exception; + + +import org.springframework.web.reactive.function.client.WebClientResponseException; + +public class HttpWebClientException extends WebClientResponseException { + + public HttpWebClientException(int statusCode, String statusText) { + super(statusCode, statusText, null, null, null); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/handler/ControlLoopElementHandler.java new file mode 100644 index 000000000..67948a95c --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/handler/ControlLoopElementHandler.java @@ -0,0 +1,148 @@ +/*- + * ============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.http.main.handler; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidationException; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.tuple.Pair; +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.http.main.models.ConfigRequest; +import org.onap.policy.clamp.controlloop.participant.http.main.webclient.ClHttpClient; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +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, Closeable { + + private static final Coder CODER = new StandardCoder(); + + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + + private Map<ToscaConceptIdentifier, Pair<Integer, String>> restResponseMap = new ConcurrentHashMap<>(); + + @Setter + private ParticipantIntermediaryApi intermediaryApi; + + /** + * Handle controlLoopElement statistics. + * + * @param controlLoopElementId controlloop element id + */ + @Override + public void handleStatistics(UUID controlLoopElementId) throws PfModelException { + // Implementation not needed for http participant + + } + + /** + * Handle a control loop element state change. + * + * @param controlLoopElementId the ID of the control loop element + * @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 a model exception + */ + @Override + public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState, + ControlLoopOrderedState newState) throws PfModelException { + // Implementation not needed for http participant + } + + /** + * Callback method to handle an update on a control loop element. + * + * @param element the information on the control loop element + * @param controlLoopDefinition toscaServiceTemplate + */ + @Override + public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition) { + + for (Map.Entry<String, ToscaNodeTemplate> nodeTemplate : controlLoopDefinition.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + // Fetching the node template of corresponding CL element + if (element.getDefinition().getName().equals(nodeTemplate.getKey())) { + try { + var configRequest = CODER.convert(nodeTemplate.getValue().getProperties(), ConfigRequest.class); + Set<ConstraintViolation<ConfigRequest>> violations = Validation.buildDefaultValidatorFactory() + .getValidator().validate(configRequest); + if (violations.isEmpty()) { + invokeHttpClient(configRequest); + } else { + LOGGER.error("Violations found in the config request parameters: {}", violations); + throw new ValidationException("Constraint violations in the config request"); + } + } catch (CoderException | ValidationException e) { + LOGGER.error("Error invoking the http request for the config ", e); + } + } + } + } + + /** + * Invoke a runnable thread to execute http requests. + * @param configRequest ConfigRequest + */ + public void invokeHttpClient(ConfigRequest configRequest) { + // Invoke runnable thread to execute https requests of all config entities + executor.execute(new ClHttpClient(configRequest, restResponseMap)); + } + + /** + * Closes this stream and releases any system resources associated + * with it. If the stream is already closed then invoking this + * method has no effect. + * + * @throws IOException if an I/O error occurs + */ + @Override + public void close() throws IOException { + executor.shutdown(); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigRequest.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigRequest.java new file mode 100644 index 000000000..87cab88a7 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigRequest.java @@ -0,0 +1,51 @@ +/*- + * ============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.http.main.models; + +import java.util.List; +import java.util.Map; +import javax.validation.Valid; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.ws.rs.DefaultValue; +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ConfigRequest { + + @NotBlank + private String baseUrl; + + @NotNull + private Map<String, String> httpHeaders; + + @NotNull + @Valid + private List<ConfigurationEntity> configurationEntities; + + @Min(value = 1) + @DefaultValue("20") + private int uninitializedToPassiveTimeout; + +} diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigurationEntity.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigurationEntity.java new file mode 100644 index 000000000..8703a9d21 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/ConfigurationEntity.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.http.main.models; + +import java.util.List; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +@Data +@AllArgsConstructor +public class ConfigurationEntity { + + @NotNull + private ToscaConceptIdentifier configurationEntityId; + + @NotNull + @Valid + private List<RestParams> restSequence; +} diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/RestParams.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/RestParams.java new file mode 100644 index 000000000..fc4e02897 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/models/RestParams.java @@ -0,0 +1,54 @@ +/*- + * ============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.http.main.models; + +import java.util.Map; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +@Data +@AllArgsConstructor +public class RestParams { + + @NotNull + private ToscaConceptIdentifier restRequestId; + + @NotNull + private String httpMethod; + + @NotNull + private String path; + + @Min(100) + @Max(599) + private int expectedResponse; + + private Map<String, Object> pathParams; + + private Map<String, String> queryParams; + + private String body; + +} diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/webclient/ClHttpClient.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/webclient/ClHttpClient.java new file mode 100644 index 000000000..0e0ea557e --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/controlloop/participant/http/main/webclient/ClHttpClient.java @@ -0,0 +1,135 @@ +/*- + * ============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.http.main.webclient; + +import java.lang.invoke.MethodHandles; +import java.time.Duration; +import java.util.Map; +import java.util.Objects; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.clamp.controlloop.participant.http.main.exception.HttpWebClientException; +import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest; +import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigurationEntity; +import org.onap.policy.clamp.controlloop.participant.http.main.models.RestParams; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; + +public class ClHttpClient implements Runnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final ConfigRequest configRequest; + + private Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap; + + /** + * Constructor. + */ + public ClHttpClient(ConfigRequest configRequest, Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap) { + this.configRequest = configRequest; + this.responseMap = responseMap; + } + + /** + * Runnable to execute http requests. + */ + @Override + public void run() { + + var webClient = WebClient.builder() + .baseUrl(configRequest.getBaseUrl()) + .defaultHeaders(httpHeaders -> httpHeaders.addAll(createHeaders(configRequest))) + .build(); + + for (ConfigurationEntity configurationEntity : configRequest.getConfigurationEntities()) { + LOGGER.info("Executing http requests for the config entity {}", + configurationEntity.getConfigurationEntityId()); + + executeRequest(webClient, configurationEntity); + } + } + + private void executeRequest(WebClient client, ConfigurationEntity configurationEntity) { + + // Iterate the sequence of http requests + for (RestParams request: configurationEntity.getRestSequence()) { + String response = null; + try { + var httpMethod = Objects.requireNonNull(HttpMethod.resolve(request.getHttpMethod())); + var uri = createUriString(request); + LOGGER.info("Executing HTTP request: {} for the Rest request id: {}", httpMethod, + request.getRestRequestId()); + + response = client.method(httpMethod) + .uri(uri) + .body(request.getBody() == null ? BodyInserters.empty() + : BodyInserters.fromValue(request.getBody())) + .exchangeToMono(clientResponse -> + clientResponse.statusCode().value() == request.getExpectedResponse() + ? clientResponse.bodyToMono(String.class) + : Mono.error(new HttpWebClientException(clientResponse.statusCode().value(), + clientResponse.bodyToMono(String.class).toString()))) + .block(Duration.ofMillis(configRequest.getUninitializedToPassiveTimeout() * 1000L)); + + LOGGER.info("HTTP response for the {} request : {}", httpMethod, response); + responseMap.put(request.getRestRequestId(), new ImmutablePair<>(request.getExpectedResponse(), + response)); + + } catch (HttpWebClientException ex) { + LOGGER.error("Error occurred on the HTTP request ", ex); + responseMap.put(request.getRestRequestId(), new ImmutablePair<>(ex.getStatusCode().value(), + ex.getResponseBodyAsString())); + } + } + } + + private HttpHeaders createHeaders(ConfigRequest request) { + var headers = new HttpHeaders(); + for (Map.Entry<String, String> entry: request.getHttpHeaders().entrySet()) { + headers.add(entry.getKey(), entry.getValue()); + } + return headers; + } + + private String createUriString(RestParams restParams) { + var uriComponentsBuilder = UriComponentsBuilder.fromUriString(restParams.getPath()); + // Add path params if present + if (restParams.getPathParams() != null) { + uriComponentsBuilder.uriVariables(restParams.getPathParams()); + } + // Add query params if present + if (restParams.getQueryParams() != null) { + for (Map.Entry<String, String> entry : restParams.getQueryParams().entrySet()) { + uriComponentsBuilder.queryParam(entry.getKey(), entry.getValue()); + } + } + return uriComponentsBuilder.build().toUriString(); + } + +} diff --git a/participant/participant-impl/participant-impl-http/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-http/src/main/resources/config/application.yaml new file mode 100644 index 000000000..1fd528458 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/main/resources/config/application.yaml @@ -0,0 +1,27 @@ +participant: + intermediaryParameters: + reportingTimeInterval: 120000 + description: Participant Description + participantId: + name: HttpParticipant0 + version: 1.0.0 + participantType: + name: org.onap.k8s.controlloop.HttpControlLoopParticipant + version: 2.3.4 + clampControlLoopTopics: + topicSources: + - topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:message-router} + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:message-router} + topicCommInfrastructure: dmaap + + - topic: POLICY-NOTIFICATION + servers: + - ${topicServer:message-router} + topicCommInfrastructure: dmaap diff --git a/participant/participant-impl/participant-impl-http/src/test/java/handler/ClElementHandlerTest.java b/participant/participant-impl/participant-impl-http/src/test/java/handler/ClElementHandlerTest.java new file mode 100644 index 000000000..10b8103cb --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/handler/ClElementHandlerTest.java @@ -0,0 +1,63 @@ +/*- + * ============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 handler; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.participant.http.main.handler.ControlLoopElementHandler; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import utils.CommonTestData; +import utils.ToscaUtils; + +@ExtendWith(SpringExtension.class) +class ClElementHandlerTest { + + @InjectMocks + @Spy + private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler(); + + private CommonTestData commonTestData = new CommonTestData(); + + private static ToscaServiceTemplate serviceTemplate; + + @BeforeAll + static void init() throws CoderException { + serviceTemplate = ToscaUtils.readControlLoopFromTosca(); + } + + @Test + void test_ControlLoopElementUpdate() { + doNothing().when(controlLoopElementHandler).invokeHttpClient(any()); + ControlLoopElement element = commonTestData.getControlLoopElement(); + assertDoesNotThrow(() -> controlLoopElementHandler + .controlLoopElementUpdate(element, serviceTemplate)); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/utils/CommonTestData.java b/participant/participant-impl/participant-impl-http/src/test/java/utils/CommonTestData.java new file mode 100644 index 000000000..84957edfa --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/utils/CommonTestData.java @@ -0,0 +1,146 @@ +/*- + * ============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 utils; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +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.participant.http.main.models.ConfigurationEntity; +import org.onap.policy.clamp.controlloop.participant.http.main.models.RestParams; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +public class CommonTestData { + + private static final Coder CODER = new StandardCoder(); + private static final String TEST_KEY_NAME = "org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement"; + + + /** + * Get a controlLoop Element. + * @return controlLoopElement object + */ + public ControlLoopElement getControlLoopElement() { + ControlLoopElement element = new ControlLoopElement(); + element.setId(UUID.randomUUID()); + element.setDefinition(new ToscaConceptIdentifier(TEST_KEY_NAME, "1.0.1")); + element.setOrderedState(ControlLoopOrderedState.PASSIVE); + return element; + } + + /** + * Get query params. + * @return Map of query params + */ + public Map<String, String> getQueryParams() { + return Map.of("name", "dummy", "version", "1.0"); + } + + /** + * Get path params. + * @return Map of path params + */ + public Map<String, Object> getPathParams() { + return Map.of("id", "123", "name", "dummy"); + } + + /** + * Rest params with GET request. + * @return RestParams obj + */ + public RestParams restParamsWithGet() { + return new RestParams( + new ToscaConceptIdentifier("getRequest", "1.0"), + "GET", + "get", + 200, + null, + getQueryParams(), + null + ); + } + + /** + * Rest params with POST request. + * @return RestParams obj + */ + public RestParams restParamsWithPost() { + return new RestParams( + new ToscaConceptIdentifier("postRequest", "1.0"), + "POST", + "post", + 200, + null, + getQueryParams(), + "Test body" + ); + } + + /** + * Rest params with POST request. + * @return RestParams obj + */ + public RestParams restParamsWithInvalidPost() { + return new RestParams( + new ToscaConceptIdentifier("postRequest", "1.0"), + "POST", + "post/{id}/{name}", + 200, + getPathParams(), + getQueryParams(), + "Test body" + ); + } + + /** + * Get invalid configuration entity. + * @return ConfigurationEntity obj + */ + public ConfigurationEntity getInvalidConfigurationEntity() { + return new ConfigurationEntity( + new ToscaConceptIdentifier("config1", "1.0.1"), + List.of(restParamsWithGet(), restParamsWithInvalidPost()) + ); + } + + /** + * Get configuration entity. + * @return ConfigurationEntity obj + */ + public ConfigurationEntity getConfigurationEntity() { + return new ConfigurationEntity( + new ToscaConceptIdentifier("config1", "1.0.1"), + List.of(restParamsWithGet(), restParamsWithPost()) + ); + } + + /** + * Get headers for config request. + * @return Map of headers + */ + public Map<String, String> getHeaders() { + return Map.of("Content-Type", "application/json", "Accept", "application/json"); + } + +} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java b/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java new file mode 100644 index 000000000..cf71248c3 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java @@ -0,0 +1,48 @@ +/*- + * ============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 utils; + +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +/** + * Util class for Test scope. + */ +public class ToscaUtils { + + private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); + private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/HttpParticipantConfig.yaml"; + + + /** + * Read a service template yaml. + * @return ToscaServiceTemplate + */ + public static ToscaServiceTemplate readControlLoopFromTosca() { + return serializeControlLoopYaml(TOSCA_TEMPLATE_YAML); + } + + private static ToscaServiceTemplate serializeControlLoopYaml(String controlLoopFilePath) { + String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath); + return yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/webclient/ClHttpClientTest.java b/participant/participant-impl/participant-impl-http/src/test/java/webclient/ClHttpClientTest.java new file mode 100644 index 000000000..411974a04 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/webclient/ClHttpClientTest.java @@ -0,0 +1,85 @@ +/*- + * ============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 webclient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest; +import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigurationEntity; +import org.onap.policy.clamp.controlloop.participant.http.main.webclient.ClHttpClient; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import utils.CommonTestData; + +@ExtendWith(SpringExtension.class) +class ClHttpClientTest { + + private CommonTestData commonTestData = new CommonTestData(); + + private String testBaseUrl = "https://httpbin.org"; + + private Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>(); + + + @Test + void test_validRequest() { + //Add valid rest requests POST, GET + ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity(); + + Map<String, String> headers = commonTestData.getHeaders(); + ConfigRequest configRequest = new ConfigRequest(testBaseUrl, headers, + List.of(configurationEntity), 10); + + ClHttpClient client = new ClHttpClient(configRequest, responseMap); + assertDoesNotThrow(client::run); + assertThat(responseMap).hasSize(2).containsKey(commonTestData + .restParamsWithGet().getRestRequestId()); + + Pair<Integer, String> restResponseMap = responseMap.get(commonTestData + .restParamsWithGet().getRestRequestId()); + assertThat(restResponseMap.getKey()).isEqualTo(200); + } + + @Test + void test_invalidRequest() { + //Add rest requests Invalid POST, Valid GET + ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity(); + + Map<String, String> headers = commonTestData.getHeaders(); + ConfigRequest configRequest = new ConfigRequest(testBaseUrl, headers, + List.of(configurationEntity), 10); + + ClHttpClient client = new ClHttpClient(configRequest, responseMap); + assertDoesNotThrow(client::run); + assertThat(responseMap).hasSize(2).containsKey(commonTestData + .restParamsWithGet().getRestRequestId()); + Pair<Integer, String> response = responseMap + .get(commonTestData.restParamsWithInvalidPost().getRestRequestId()); + assertThat(response.getKey()).isEqualTo(404); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/test/resources/HttpParticipantConfig.yaml b/participant/participant-impl/participant-impl-http/src/test/resources/HttpParticipantConfig.yaml new file mode 100644 index 000000000..43d0c269c --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/resources/HttpParticipantConfig.yaml @@ -0,0 +1,208 @@ +# ============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========================================================= +tosca_definitions_version: tosca_simple_yaml_1_3 +data_types: + onap.datatypes.ToscaConceptIdentifier: + derived_from: tosca.datatypes.Root + properties: + name: + type: string + required: true + version: + type: string + required: true + + org.onap.datatypes.policy.clamp.controlloop.httpControlLoopElement.RestRequest: + version: 1.0.0 + derived_from: tosca.datatypes.Root + properties: + restRequestId: + type: onap.datatypes.ToscaConceptIdentifier + typeVersion: 1.0.0 + required: true + description: The name and version of a REST request to be sent to a REST endpoint + httpMethod: + type: string + required: true + constraints: + - valid_values: [POST, PUT, GET, DELETE] + description: The REST method to use + path: + type: string + required: true + description: The path of the REST request relative to the base URL + body: + type: string + required: false + description: The body of the REST request for PUT and POST requests + expectedResponse: + type: integer + required: true + constraints: + - in_range: [100, 599] + description: THe expected HTTP status code for the REST request + org.onap.datatypes.policy.clamp.controlloop.httpControlLoopElement.ConfigurationEntity: + version: 1.0.0 + derived_from: tosca.datatypes.Root + properties: + configurationEntityId: + type: onap.datatypes.ToscaConceptIdentifier + typeVersion: 1.0.0 + required: true + description: The name and version of a Configuration Entity to be handled by the HTTP Control Loop Element + restSequence: + type: list + entry_schema: + type: org.onap.datatypes.policy.clamp.controlloop.httpControlLoopElement.RestRequest + typeVersion: 1.0.0 + description: A sequence of REST commands to send to the REST endpoint + + +node_types: + org.onap.policy.clamp.controlloop.Participant: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + org.onap.policy.clamp.controlloop.ControlLoopElement: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + participant_id: + type: onap.datatypes.ToscaConceptIdentifier + requred: true + org.onap.policy.clamp.controlloop.ControlLoop: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + elements: + type: list + required: true + entry_schema: + type: onap.datatypes.ToscaConceptIdentifier + org.onap.policy.clamp.controlloop.HttpControlLoopElement: + version: 1.0.1 + derived_from: org.onap.policy.clamp.controlloop.ControlLoopElement + properties: + baseUrl: + type: string + required: true + description: The base URL to be prepended to each path, identifies the host for the REST endpoints. + httpHeaders: + type: map + required: false + entry_schema: + type: string + description: HTTP headers to send on REST requests + configurationEntities: + type: map + required: true + entry_schema: + type: org.onap.datatypes.policy.clamp.controlloop.httpControlLoopElement.ConfigurationEntity + typeVersion: 1.0.0 + description: The connfiguration entities the Control Loop Element is managing and their associated REST requests +topology_template: + node_templates: + org.onap.k8s.controlloop.HttpControlLoopParticipant: + version: 2.3.4 + type: org.onap.policy.clamp.controlloop.Participant + type_version: 1.0.1 + description: Participant for Http requests + properties: + provider: ONAP + + org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement: + # Http config for PMSH. + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.HttpControlLoopElement + type_version: 1.0.1 + description: Control loop element for the http requests of PMSH microservice + properties: + provider: ONAP + participant_id: + name: org.onap.controlloop.HttpControlLoopParticipant + version: 2.3.4 + uninitializedToPassiveTimeout: 180 + baseUrl: https://httpbin.org + httpHeaders: + Content-Type: application/json + configurationEntities: + - configurationEntityId: + name: entity1 + version: 1.0.1 + restSequence: + - restRequestId: + name: request1 + version: 1.0.1 + httpMethod: POST + path: post + body: '{"name":"ob1","port_type"}' + expectedResponse: 200 + + - restRequestId: + name: request1 + version: 1.0.1 + httpMethod: GET + path: get + expectedResponse: 200 + + # Rest path with path params and query params + - configurationEntityId: + name: entity2 + version: 1.0.1 + restSequence: + - restRequestId: + name: request1 + version: 1.0.1 + httpMethod: POST + path: post/{id}/{name} + pathParams: + id: 123 + name: dummyName + body: this is a test body + expectedResponse: 200 + + - restRequestId: + name: request1 + version: 1.0.1 + httpMethod: GET + path: get + queryParams: + id: 123 + name: dummyName + expectedResponse: 200 + + + org.onap.domain.sample.GenericK8s_ControlLoopDefinition: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.ControlLoop + type_version: 1.0.0 + description: Control loop for HTTP request + properties: + provider: ONAP + elements: + - name: org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement + version: 1.2.3 diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml index 9d3523737..a27c33d44 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml @@ -10,7 +10,7 @@ participant: localChartDirectory: /var/helm-manager/local-charts infoFileName: CHART_INFO.json intermediaryParameters: - reportingTimeInterval: 120000 + reportingTimeIntervalMs: 120000 description: Participant Description participantId: name: K8sParticipant0 @@ -19,15 +19,19 @@ participant: name: org.onap.k8s.controlloop.K8SControlLoopParticipant version: 2.3.4 clampControlLoopTopics: - topicSources[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap - fetchTimeout: 15000 - topicSinks[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap + topicSources: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap management: endpoints: diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org.onap.policy.clamp.controlloop.participant.kubernetes/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org.onap.policy.clamp.controlloop.participant.kubernetes/parameters/CommonTestData.java index d8d477d1b..9255ee6dc 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org.onap.policy.clamp.controlloop.participant.kubernetes/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org.onap.policy.clamp.controlloop.participant.kubernetes/parameters/CommonTestData.java @@ -99,7 +99,7 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); if (!isEmpty) { map.put("name", "Participant parameters"); - map.put("reportingTimeInterval", TIME_INTERVAL); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); map.put("description", DESCRIPTION); map.put("participantId", getParticipantId()); map.put("participantType", getParticipantId()); 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 index d4c7d7561..32cd28cd3 100644 --- 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 @@ -1,3 +1,5 @@ +server: + port: 8082 participant: policyApiParameters: @@ -9,7 +11,7 @@ participant: https: true allowSelfSignedCerts: true intermediaryParameters: - reportingTimeInterval: 120000 + reportingTimeIntervalMs: 120000 description: Participant Description participantId: name: org.onap.PM_Policy @@ -18,12 +20,16 @@ participant: name: org.onap.policy.controlloop.PolicyControlLoopParticipant version: 2.3.1 clampControlLoopTopics: - topicSources[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap - fetchTimeout: 15000 - topicSinks[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap + topicSources: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java index 4b4558b89..aed03556a 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java @@ -20,16 +20,27 @@ package org.onap.policy.clamp.controlloop.participant.policy.endtoend; +import static org.assertj.core.api.Assertions.assertThatCode; + import java.time.Instant; import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener; @@ -41,6 +52,7 @@ import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListe import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -139,6 +151,26 @@ class ParticipantMessagesTest { } } + @Test + void testParticipantStatusHeartbeat() throws Exception { + final ParticipantStatus heartbeat = new ParticipantStatus(); + heartbeat.setParticipantId(getParticipantId()); + ControlLoopInfo clInfo = getControlLoopInfo(getControlLoopId()); + heartbeat.setControlLoopInfoMap(Map.of(getControlLoopId(), clInfo)); + + ControlLoopElementDefinition clDefinition = getClElementDefinition(); + Map<UUID, ControlLoopElementDefinition> clElementDefinitionMap = Map.of(UUID.randomUUID(), clDefinition); + Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>> + participantDefinitionUpdateMap = Map.of(getParticipantId(), clElementDefinitionMap); + heartbeat.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap); + + synchronized (lockit) { + ParticipantMessagePublisher publisher = + new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class))); + assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException(); + } + } + private ToscaConceptIdentifier getParticipantId() { return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0"); } @@ -146,4 +178,47 @@ class ParticipantMessagesTest { private ToscaConceptIdentifier getParticipantType() { return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); } + + private ToscaConceptIdentifier getControlLoopId() { + return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + } + + private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) { + ControlLoopInfo clInfo = new ControlLoopInfo(); + clInfo.setState(ControlLoopState.PASSIVE2RUNNING); + + ControlLoopStatistics clStatistics = new ControlLoopStatistics(); + clStatistics.setControlLoopId(id); + clStatistics.setAverageExecutionTime(12345); + clStatistics.setEventCount(12345); + clStatistics.setLastEnterTime(12345); + clStatistics.setLastExecutionTime(12345); + clStatistics.setLastStart(12345); + clStatistics.setTimeStamp(Instant.ofEpochMilli(3000)); + clStatistics.setUpTime(12345); + ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList(); + ClElementStatistics clElementStatistics = new ClElementStatistics(); + clElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1")); + clElementStatistics.setTimeStamp(Instant.now()); + clElementStatisticsList.setClElementStatistics(List.of(clElementStatistics)); + clStatistics.setClElementStatisticsList(clElementStatisticsList); + + clInfo.setControlLoopStatistics(clStatistics); + return clInfo; + } + + private ControlLoopElementDefinition getClElementDefinition() { + ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate(); + toscaServiceTemplate.setName("serviceTemplate"); + toscaServiceTemplate.setDerivedFrom("parentServiceTemplate"); + toscaServiceTemplate.setDescription("Description of serviceTemplate"); + toscaServiceTemplate.setVersion("1.2.3"); + + ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition(); + clDefinition.setId(UUID.randomUUID()); + clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate); + Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue"); + clDefinition.setCommonPropertiesMap(commonPropertiesMap); + return clDefinition; + } } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java index 45674f4c8..10563cde2 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantPolicyTest.java @@ -21,17 +21,14 @@ package org.onap.policy.clamp.controlloop.participant.policy.endtoend; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; 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.models.messages.dmaap.participant.ParticipantUpdate; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; 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.intermediary.comm.ParticipantUpdateListener; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; @@ -55,16 +52,13 @@ class ParticipantPolicyTest { @Test void testUpdatePolicyTypes() { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); - - // Verify that the ToscaServicetemplate has policy_types - assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes()); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); synchronized (lockit) { ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName()); @@ -72,20 +66,13 @@ class ParticipantPolicyTest { @Test 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()); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); synchronized (lockit) { ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName()); @@ -93,29 +80,22 @@ class ParticipantPolicyTest { @Test 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()); + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); synchronized (lockit) { ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); } // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName()); ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler); - ParticipantControlLoopStateChange participantControlLoopStateChangeMsg = + ControlLoopStateChange controlLoopStateChangeMsg = TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED); - participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED); - clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg); + controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED); + clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg); // Verify the result of GET participants with what is stored assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName()); diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java index 38861472c..a34c9cda5 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java @@ -99,7 +99,7 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); if (!isEmpty) { map.put("name", "Participant parameters"); - map.put("reportingTimeInterval", TIME_INTERVAL); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); map.put("description", DESCRIPTION); map.put("participantId", getParticipantId()); map.put("participantType", getParticipantId()); 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 d439c9daf..b91cff22c 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 @@ -35,10 +35,8 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop 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.models.controlloop.concepts.ParticipantState; -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.models.messages.dmaap.participant.ParticipantHealthCheck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData; import org.onap.policy.common.utils.coder.Coder; @@ -100,35 +98,15 @@ public class TestListenerUtils { } /** - * Method to create ParticipantStateChange message from the arguments passed. - * - * @param participantState participant State - * - * @return ParticipantStateChange message - */ - public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) { - final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange(); - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("org.onap.PM_Policy"); - participantId.setVersion("0.0.0"); - - participantStateChangeMsg.setParticipantId(participantId); - participantStateChangeMsg.setTimestamp(Instant.now()); - participantStateChangeMsg.setState(participantState); - - return participantStateChangeMsg; - } - - /** * Method to create ControlLoopStateChange message from the arguments passed. * * @param controlLoopOrderedState controlLoopOrderedState * - * @return ParticipantControlLoopStateChange message + * @return ControlLoopStateChange message */ - public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg( + public static ControlLoopStateChange createControlLoopStateChangeMsg( final ControlLoopOrderedState controlLoopOrderedState) { - final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange(); + final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); controlLoopId.setName("PMSHInstance0"); @@ -138,21 +116,21 @@ public class TestListenerUtils { participantId.setName("org.onap.PM_Policy"); participantId.setVersion("0.0.0"); - participantClStateChangeMsg.setControlLoopId(controlLoopId); - participantClStateChangeMsg.setParticipantId(participantId); - participantClStateChangeMsg.setTimestamp(Instant.now()); - participantClStateChangeMsg.setOrderedState(controlLoopOrderedState); + clStateChangeMsg.setControlLoopId(controlLoopId); + clStateChangeMsg.setParticipantId(participantId); + clStateChangeMsg.setTimestamp(Instant.now()); + clStateChangeMsg.setOrderedState(controlLoopOrderedState); - return participantClStateChangeMsg; + return clStateChangeMsg; } /** * Method to create ControlLoopUpdateMsg. * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message */ - public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() { - final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate(); + public static ControlLoopUpdate createControlLoopUpdateMsg() { + final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); controlLoopId.setName("PMSHInstance0"); controlLoopId.setVersion("1.0.0"); @@ -177,6 +155,7 @@ public class TestListenerUtils { clElementParticipantId.setName(toscaInputEntry.getKey()); clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion()); clElement.setParticipantId(clElementParticipantId); + clElement.setParticipantType(clElementParticipantId); clElement.setDefinition(clElementParticipantId); clElement.setState(ControlLoopState.UNINITIALISED); @@ -189,7 +168,6 @@ public class TestListenerUtils { controlLoop.setVersion("1.0.0"); controlLoop.setDefinition(controlLoopId); clUpdateMsg.setControlLoop(controlLoop); - clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate); return clUpdateMsg; } @@ -211,11 +189,9 @@ public class TestListenerUtils { participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); participantUpdateMsg.setMessageId(UUID.randomUUID()); - ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate(); - toscaServiceTemplate.setName("serviceTemplate"); - toscaServiceTemplate.setDerivedFrom("parentServiceTemplate"); - toscaServiceTemplate.setDescription("Description of serviceTemplate"); - toscaServiceTemplate.setVersion("1.2.3"); + ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); + // Add policies to the toscaServiceTemplate + TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition(); clDefinition.setId(UUID.randomUUID()); @@ -234,41 +210,18 @@ public class TestListenerUtils { } /** - * Method to create ParticipantHealthCheck message. - * - * @return ParticipantHealthCheck message - */ - public static ParticipantHealthCheck createParticipantHealthCheckMsg() { - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("org.onap.PM_Policy"); - participantId.setVersion("0.0.0"); - - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); - controlLoopId.setName("PMSHInstance0"); - controlLoopId.setVersion("1.0.0"); - - final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck(); - participantHealthCheckMsg.setParticipantId(participantId); - participantHealthCheckMsg.setControlLoopId(controlLoopId); - participantHealthCheckMsg.setTimestamp(Instant.now()); - participantHealthCheckMsg.setState(ParticipantState.PASSIVE); - - return participantHealthCheckMsg; - } - - /** - * Method to create ParticipantControlLoopUpdate using the arguments passed. + * Method to create ControlLoopUpdate using the arguments passed. * * @param jsonFilePath the path of the controlloop content * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message * @throws CoderException exception while reading the file to object */ - public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) + public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = - CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class); - return participantControlLoopUpdateMsg; + ControlLoopUpdate controlLoopUpdateMsg = + CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); + return controlLoopUpdateMsg; } private static ToscaServiceTemplate testControlLoopRead() { diff --git a/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml index 5a9cf1497..c26749b4d 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml @@ -5,7 +5,7 @@ spring: password: zb!XztG34 server: - port: 6969 + port: 8084 servlet: context-path: /onap/participantsim error: @@ -13,7 +13,7 @@ server: participant: intermediaryParameters: - reportingTimeInterval: 120000 + reportingTimeIntervalMs: 120000 description: Participant Description participantId: name: org.onap.PM_CDS_Blueprint @@ -22,12 +22,16 @@ participant: name: org.onap.PM_CDS_Blueprint version: 1.0.0 clampControlLoopTopics: - topicSources[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap - fetchTimeout: 15000 - topicSinks[0]: - topic: POLICY-CLRUNTIME-PARTICIPANT - servers[0]: ${topicServer:message-router} - topicCommInfrastructure: dmaap + topicSources: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - + topic: POLICY-CLRUNTIME-PARTICIPANT + servers: + - ${topicServer:localhost} + topicCommInfrastructure: dmaap diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/endtoend/ParticipantSimulatorTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/endtoend/ParticipantSimulatorTest.java index 0b63c395a..beb50da37 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/endtoend/ParticipantSimulatorTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/endtoend/ParticipantSimulatorTest.java @@ -36,7 +36,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener; @@ -103,10 +103,10 @@ class ParticipantSimulatorTest { ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = + ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); - clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg); + controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); + clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); } } diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/parameters/CommonTestData.java index e85b3ebbe..9c41c8bcb 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/parameters/CommonTestData.java @@ -79,7 +79,7 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); if (!isEmpty) { map.put("name", "Participant parameters"); - map.put("reportingTimeInterval", TIME_INTERVAL); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); map.put("description", DESCRIPTION); map.put("participantId", getParticipantId()); map.put("participantType", getParticipantId()); diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java index 8aa40cbd5..999feb1e1 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java @@ -34,9 +34,8 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop 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.models.controlloop.concepts.ParticipantState; -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.models.messages.dmaap.participant.ParticipantStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -98,52 +97,34 @@ public class TestListenerUtils { } /** - * Method to create ParticipantStateChange message from the arguments passed. - * - * @param participantState participant State - * - * @return ParticipantStateChange message - */ - public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) { - final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange(); - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0"); - - participantStateChangeMsg.setParticipantId(participantId); - participantStateChangeMsg.setTimestamp(Instant.now()); - participantStateChangeMsg.setState(participantState); - - return participantStateChangeMsg; - } - - /** * Method to create ControlLoopStateChange message from the arguments passed. * * @param controlLoopOrderedState controlLoopOrderedState * - * @return ParticipantControlLoopStateChange message + * @return ControlLoopStateChange message */ - public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg( + public static ControlLoopStateChange createControlLoopStateChangeMsg( final ControlLoopOrderedState controlLoopOrderedState) { - final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange(); + final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0"); - participantClStateChangeMsg.setControlLoopId(controlLoopId); - participantClStateChangeMsg.setParticipantId(participantId); - participantClStateChangeMsg.setTimestamp(Instant.now()); - participantClStateChangeMsg.setOrderedState(controlLoopOrderedState); + clStateChangeMsg.setControlLoopId(controlLoopId); + clStateChangeMsg.setParticipantId(participantId); + clStateChangeMsg.setTimestamp(Instant.now()); + clStateChangeMsg.setOrderedState(controlLoopOrderedState); - return participantClStateChangeMsg; + return clStateChangeMsg; } /** * Method to create ControlLoopUpdateMsg. * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message */ - public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() { - final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate(); + public static ControlLoopUpdate createControlLoopUpdateMsg() { + final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0"); @@ -177,24 +158,23 @@ public class TestListenerUtils { controlLoop.setVersion("1.0.0"); controlLoop.setDefinition(controlLoopId); clUpdateMsg.setControlLoop(controlLoop); - clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate); return clUpdateMsg; } /** - * Method to create ParticipantControlLoopUpdate using the arguments passed. + * Method to create ControlLoopUpdate using the arguments passed. * * @param jsonFilePath the path of the controlloop content * - * @return ParticipantControlLoopUpdate message + * @return ControlLoopUpdate message * @throws CoderException exception while reading the file to object */ - public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) + public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException { - ParticipantControlLoopUpdate participantControlLoopUpdateMsg = - CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class); - return participantControlLoopUpdateMsg; + ControlLoopUpdate controlLoopUpdateMsg = + CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); + return controlLoopUpdateMsg; } private static ToscaServiceTemplate testControlLoopRead() { diff --git a/participant/participant-impl/pom.xml b/participant/participant-impl/pom.xml index f1f4f0eb9..439bdea90 100644 --- a/participant/participant-impl/pom.xml +++ b/participant/participant-impl/pom.xml @@ -40,6 +40,7 @@ <module>participant-impl-policy</module> <module>participant-impl-cds</module> <module>participant-impl-kubernetes</module> + <module>participant-impl-http</module> </modules> <dependencies> diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopStateChangeListener.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopStateChangeListener.java index e46c6db1b..0b9110bd0 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopStateChangeListener.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopStateChangeListener.java @@ -20,7 +20,7 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.comm; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.springframework.stereotype.Component; @@ -28,7 +28,7 @@ import org.springframework.stereotype.Component; * Listener for Participant State Change messages sent by CLAMP. */ @Component -public class ControlLoopStateChangeListener extends ParticipantListener<ParticipantControlLoopStateChange> { +public class ControlLoopStateChangeListener extends ParticipantListener<ControlLoopStateChange> { /** * Constructs the object. @@ -36,7 +36,7 @@ public class ControlLoopStateChangeListener extends ParticipantListener<Particip * @param participantHandler the handler for managing the state of the participant */ public ControlLoopStateChangeListener(final ParticipantHandler participantHandler) { - super(ParticipantControlLoopStateChange.class, participantHandler, + super(ControlLoopStateChange.class, participantHandler, participantHandler::handleControlLoopStateChange); } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopUpdateListener.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopUpdateListener.java index d15643e0f..56bc1fd9a 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopUpdateListener.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ControlLoopUpdateListener.java @@ -20,7 +20,7 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.comm; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.springframework.stereotype.Component; @@ -28,7 +28,7 @@ import org.springframework.stereotype.Component; * Listener for Control Loop Update messages sent by CLAMP. */ @Component -public class ControlLoopUpdateListener extends ParticipantListener<ParticipantControlLoopUpdate> { +public class ControlLoopUpdateListener extends ParticipantListener<ControlLoopUpdate> { /** * Constructs the object. @@ -36,6 +36,6 @@ public class ControlLoopUpdateListener extends ParticipantListener<ParticipantCo * @param participantHandler the handler for managing the state of the participant */ public ControlLoopUpdateListener(final ParticipantHandler participantHandler) { - super(ParticipantControlLoopUpdate.class, participantHandler, participantHandler::handleControlLoopUpdate); + super(ControlLoopUpdate.class, participantHandler, participantHandler::handleControlLoopUpdate); } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/MessageSender.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/MessageSender.java index 1bfce1374..3ff420ffa 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/MessageSender.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/MessageSender.java @@ -22,7 +22,10 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.comm; import java.io.Closeable; import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.TimerTask; +import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -30,9 +33,9 @@ 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.ControlLoops; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; @@ -67,18 +70,13 @@ public class MessageSender extends TimerTask implements Closeable { // Kick off the timer timerPool = makeTimerPool(); - timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS); + timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.MILLISECONDS); } @Override public void run() { LOGGER.debug("Sent heartbeat to CLAMP"); - - var response = new ParticipantResponseDetails(); - - response.setResponseTo(null); - response.setResponseStatus(ParticipantResponseStatus.PERIODIC); - response.setResponseMessage("Periodic response from participant"); + this.sendHeartbeat(); } @Override @@ -89,48 +87,23 @@ public class MessageSender extends TimerTask implements Closeable { /** * Send a response message for this participant. * - * @param response the details to include in the response message + * @param ackMessage the details to include in the response message */ - public void sendResponse(ParticipantResponseDetails response) { - sendResponse(null, response); + public void sendAckResponse(ControlLoopAck ackMessage) { + sendAckResponse(null, ackMessage); } /** * Dispatch a response message for this participant. * * @param controlLoopId the control loop to which this message is a response - * @param response the details to include in the response message + * @param ackMessage the details to include in the response message */ - public void sendResponse(ToscaConceptIdentifier controlLoopId, ParticipantResponseDetails response) { - var status = new ParticipantStatus(); - + public void sendAckResponse(ToscaConceptIdentifier controlLoopId, ControlLoopAck ackMessage) { // Participant related fields - status.setParticipantType(participantHandler.getParticipantType()); - status.setParticipantId(participantHandler.getParticipantId()); - status.setState(participantHandler.getState()); - status.setHealthStatus(participantHandler.getHealthStatus()); - - // Control loop related fields - var controlLoops = participantHandler.getControlLoopHandler().getControlLoops(); - status.setControlLoopId(controlLoopId); - status.setControlLoops(controlLoops); - status.setResponse(response); - - var participantStatistics = new ParticipantStatistics(); - participantStatistics.setTimeStamp(Instant.now()); - participantStatistics.setParticipantId(participantHandler.getParticipantId()); - participantStatistics.setHealthStatus(participantHandler.getHealthStatus()); - participantStatistics.setState(participantHandler.getState()); - status.setParticipantStatistics(participantStatistics); - - for (ControlLoopElementListener clElementListener : - participantHandler.getControlLoopHandler().getListeners()) { - updateClElementStatistics(controlLoops, clElementListener); - } - - status.setControlLoops(controlLoops); - - publisher.sendParticipantStatus(status); + ackMessage.setParticipantType(participantHandler.getParticipantType()); + ackMessage.setParticipantId(participantHandler.getParticipantId()); + publisher.sendControlLoopAck(ackMessage); } /** @@ -161,6 +134,28 @@ public class MessageSender extends TimerTask implements Closeable { } /** + * Send a ParticipantStatus message for this participant. + * + * @param participantStatus the ParticipantStatus message + */ + public void sendParticipantStatus(ParticipantStatus participantStatus) { + var controlLoops = participantHandler.getControlLoopHandler().getControlLoops(); + for (ControlLoopElementListener clElementListener : + participantHandler.getControlLoopHandler().getListeners()) { + updateClElementStatistics(controlLoops, clElementListener); + } + + publisher.sendParticipantStatus(participantStatus); + } + + /** + * Dispatch a heartbeat for this participant. + */ + public void sendHeartbeat() { + publisher.sendHeartbeat(participantHandler.makeHeartbeat()); + } + + /** * Update ControlLoopElement statistics. The control loop elements listening will be * notified to retrieve statistics from respective controlloop elements, and controlloopelements * data on the handler will be updated. diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantMessagePublisher.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantMessagePublisher.java index 9e1b84620..d8cc9eb6b 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantMessagePublisher.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantMessagePublisher.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.comm; import java.util.List; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; @@ -90,4 +91,24 @@ public class ParticipantMessagePublisher { topicSinkClient.send(participantUpdateAck); LOGGER.debug("Sent Participant Update Ack message to CLAMP - {}", participantUpdateAck); } + + /** + * Method to send ControlLoop Update/StateChange Ack message to runtime. + * + * @param controlLoopAck ControlLoop Update/StateChange Ack + */ + public void sendControlLoopAck(final ControlLoopAck controlLoopAck) { + topicSinkClient.send(controlLoopAck); + LOGGER.debug("Sent ControlLoop Update/StateChange Ack to runtime - {}", controlLoopAck); + } + + /** + * Method to send Participant heartbeat to clamp on demand. + * + * @param participantStatus the Participant Status + */ + public void sendHeartbeat(final ParticipantStatus participantStatus) { + topicSinkClient.send(participantStatus); + LOGGER.debug("Sent Participant heartbeat to CLAMP - {}", participantStatus); + } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantHealthCheckListener.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantStatusReqListener.java index 15f5140eb..0881edb19 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantHealthCheckListener.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/comm/ParticipantStatusReqListener.java @@ -20,22 +20,22 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.comm; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.springframework.stereotype.Component; /** - * Listener for Participant health status messages sent by CLAMP. + * Listener for Participant status request messages sent by runtime to all/one participant. */ @Component -public class ParticipantHealthCheckListener extends ParticipantListener<ParticipantHealthCheck> { +public class ParticipantStatusReqListener extends ParticipantListener<ParticipantStatusReq> { /** * Constructs the object. * * @param participantHandler the handler for managing the state and health of the participant */ - public ParticipantHealthCheckListener(final ParticipantHandler participantHandler) { - super(ParticipantHealthCheck.class, participantHandler, participantHandler::handleParticipantHealthCheck); + public ParticipantStatusReqListener(final ParticipantHandler participantHandler) { + super(ParticipantStatusReq.class, participantHandler, participantHandler::handleParticipantStatusReq); } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java index 50048ffc2..876a4cc52 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java @@ -28,16 +28,18 @@ import java.util.UUID; import lombok.Getter; import lombok.NoArgsConstructor; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; 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.ControlLoopElementDefinition; 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.models.controlloop.concepts.ControlLoops; -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.models.messages.dmaap.participant.ParticipantResponseDetails; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; @@ -59,6 +61,7 @@ public class ControlLoopHandler { private final Map<ToscaConceptIdentifier, ControlLoop> controlLoopMap = new LinkedHashMap<>(); + @Getter private final Map<UUID, ControlLoopElement> elementsOnThisParticipant = new LinkedHashMap<>(); @Getter @@ -92,21 +95,23 @@ public class ControlLoopHandler { ControlLoopState newState) { if (id == null) { - return null; + LOGGER.warn("Cannot update Control loop element state, id is null"); } + ControlLoopAck controlLoopStateChangeAck = + new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK); ControlLoopElement clElement = elementsOnThisParticipant.get(id); if (clElement != null) { clElement.setOrderedState(orderedState); clElement.setState(newState); + controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(), + Pair.of(true, "Control loop element {} state changed to {}\", id, newState)")); LOGGER.debug("Control loop element {} state changed to {}", id, newState); - var response = new ParticipantResponseDetails(); - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage("ControlLoopElement state changed to {} " + newState); - messageSender.sendResponse(response); + controlLoopStateChangeAck.setMessage("ControlLoopElement state changed to {} " + newState); + controlLoopStateChangeAck.setResult(true); + messageSender.sendAckResponse(controlLoopStateChangeAck); return clElement; } - return null; } @@ -130,7 +135,7 @@ public class ControlLoopHandler { * * @param stateChangeMsg the state change message */ - public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) { + public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) { if (stateChangeMsg.getControlLoopId() == null) { return; } @@ -142,9 +147,11 @@ public class ControlLoopHandler { return; } - var response = new ParticipantResponseDetails(stateChangeMsg); - handleState(controlLoop, response, stateChangeMsg.getOrderedState()); - messageSender.sendResponse(response); + var controlLoopStateChangeAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK); + controlLoopStateChangeAck.setResponseTo(stateChangeMsg.getMessageId()); + controlLoopStateChangeAck.setControlLoopId(stateChangeMsg.getControlLoopId()); + handleState(controlLoop, controlLoopStateChangeAck, stateChangeMsg.getOrderedState()); + messageSender.sendAckResponse(controlLoopStateChangeAck); } /** @@ -154,7 +161,7 @@ public class ControlLoopHandler { * @param response participant response * @param orderedState controlloop ordered state */ - private void handleState(final ControlLoop controlLoop, final ParticipantResponseDetails response, + private void handleState(final ControlLoop controlLoop, final ControlLoopAck response, ControlLoopOrderedState orderedState) { switch (orderedState) { case UNINITIALISED: @@ -177,7 +184,8 @@ public class ControlLoopHandler { * * @param updateMsg the update message */ - public void handleControlLoopUpdate(ParticipantControlLoopUpdate updateMsg) { + public void handleControlLoopUpdate(ControlLoopUpdate updateMsg, + Map<UUID, ControlLoopElementDefinition> clElementDefinitions) { if (!updateMsg.appliesTo(participantType, participantId)) { return; @@ -185,16 +193,17 @@ public class ControlLoopHandler { var controlLoop = controlLoopMap.get(updateMsg.getControlLoopId()); - var response = new ParticipantResponseDetails(updateMsg); + var controlLoopUpdateAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_UPDATE_ACK); // TODO: Updates to existing ControlLoops are not supported yet (Addition/Removal of ControlLoop // elements to existing ControlLoop has to be supported). if (controlLoop != null) { - response.setResponseStatus(ParticipantResponseStatus.FAIL); - response.setResponseMessage("Control loop " + updateMsg.getControlLoopId() - + " already defined on participant " + participantId); - - messageSender.sendResponse(response); + controlLoopUpdateAck.setResponseTo(updateMsg.getMessageId()); + controlLoopUpdateAck.setControlLoopId(updateMsg.getControlLoopId()); + controlLoopUpdateAck.setMessage("Control loop " + updateMsg.getControlLoopId() + + " already defined on participant " + participantId); + controlLoopUpdateAck.setResult(false); + messageSender.sendAckResponse(controlLoopUpdateAck); return; } @@ -211,18 +220,20 @@ public class ControlLoopHandler { for (ControlLoopElementListener clElementListener : listeners) { try { for (ControlLoopElement element : updateMsg.getControlLoop().getElements().values()) { - clElementListener.controlLoopElementUpdate(element, updateMsg.getControlLoopDefinition()); + clElementListener.controlLoopElementUpdate(element, + clElementDefinitions.get(element.getId()).getControlLoopElementToscaServiceTemplate()); } } catch (PfModelException e) { LOGGER.debug("Control loop element update failed {}", updateMsg.getControlLoopId()); } } - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage( - "Control loop " + updateMsg.getControlLoopId() + " defined on participant " + participantId); - - messageSender.sendResponse(response); + controlLoopUpdateAck.setResponseTo(updateMsg.getMessageId()); + controlLoopUpdateAck.setControlLoopId(updateMsg.getControlLoopId()); + controlLoopUpdateAck.setMessage("Control loop " + updateMsg.getControlLoopId() + + " defined on participant " + participantId); + controlLoopUpdateAck.setResult(true); + messageSender.sendAckResponse(controlLoopUpdateAck); } /** @@ -233,7 +244,7 @@ public class ControlLoopHandler { * @param response participant response */ private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, - final ParticipantResponseDetails response) { + final ControlLoopAck response) { handleStateChange(controlLoop, orderedState, ControlLoopState.UNINITIALISED, response); controlLoopMap.remove(controlLoop.getKey().asIdentifier()); @@ -256,7 +267,7 @@ public class ControlLoopHandler { * @param response participant response */ private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, - final ParticipantResponseDetails response) { + final ControlLoopAck response) { handleStateChange(controlLoop, orderedState, ControlLoopState.PASSIVE, response); } @@ -268,7 +279,7 @@ public class ControlLoopHandler { * @param response participant response */ private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, - final ParticipantResponseDetails response) { + final ControlLoopAck response) { handleStateChange(controlLoop, orderedState, ControlLoopState.RUNNING, response); } @@ -281,11 +292,11 @@ public class ControlLoopHandler { * @param response the response to the state change request */ private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState, - ControlLoopState newState, ParticipantResponseDetails response) { + ControlLoopState newState, ControlLoopAck response) { if (orderedState.equals(controlLoop.getOrderedState())) { - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage("Control loop is already in state " + orderedState); + response.setMessage("Control loop is already in state " + orderedState); + response.setResult(false); return; } @@ -296,9 +307,8 @@ public class ControlLoopHandler { }); } - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage( - "ControlLoop state changed from " + controlLoop.getOrderedState() + " to " + orderedState); + response.setMessage("ControlLoop state changed from " + controlLoop.getOrderedState() + " to " + orderedState); + response.setResult(true); controlLoop.setOrderedState(orderedState); } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java index 0aa536746..4fc0ae1b1 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java @@ -28,9 +28,8 @@ import org.onap.policy.clamp.controlloop.participant.intermediary.api.Participan 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.intermediary.comm.ParticipantDeregisterAckListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantHealthCheckListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStateChangeListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusReqListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters; import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; @@ -120,16 +119,13 @@ public class IntermediaryActivator extends ServiceManagerContainer implements Cl private void registerMsgDispatcher() { MessageTypeDispatcher msgDispatcher = applicationContext.getBean(MessageTypeDispatcher.class); - msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATE_CHANGE.name(), - applicationContext.getBean(ParticipantStateChangeListener.class)); + msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(), + applicationContext.getBean(ParticipantStatusReqListener.class)); - msgDispatcher.register(ParticipantMessageType.PARTICIPANT_HEALTH_CHECK.name(), - applicationContext.getBean(ParticipantHealthCheckListener.class)); - - msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_STATE_CHANGE.name(), + msgDispatcher.register(ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE.name(), applicationContext.getBean(ControlLoopStateChangeListener.class)); - msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_UPDATE.name(), + msgDispatcher.register(ParticipantMessageType.CONTROL_LOOP_UPDATE.name(), applicationContext.getBean(ControlLoopUpdateListener.class)); msgDispatcher.register(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(), diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java index a8913c1f0..6a0e758dd 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java @@ -32,17 +32,18 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; -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.models.messages.dmaap.participant.ControlLoopAck; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender; @@ -85,7 +86,8 @@ public class ParticipantHandler implements Closeable { this.participantType = parameters.getIntermediaryParameters().getParticipantType(); this.participantId = parameters.getIntermediaryParameters().getParticipantId(); this.sender = - new MessageSender(this, publisher, parameters.getIntermediaryParameters().getReportingTimeInterval()); + new MessageSender(this, publisher, + parameters.getIntermediaryParameters().getReportingTimeIntervalMs()); this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender); this.participantStatistics = new ParticipantStatistics(); } @@ -96,56 +98,17 @@ public class ParticipantHandler implements Closeable { } /** - * Method which handles a participant state change event from clamp. - * - * @param stateChangeMsg participant state change message - */ - public void handleParticipantStateChange(final ParticipantStateChange stateChangeMsg) { - - if (!stateChangeMsg.appliesTo(participantType, participantId)) { - return; - } - - var response = new ParticipantResponseDetails(stateChangeMsg); - - switch (stateChangeMsg.getState()) { - case PASSIVE: - handlePassiveState(response); - break; - case ACTIVE: - handleActiveState(response); - break; - case SAFE: - handleSafeState(response); - break; - case TEST: - handleTestState(response); - break; - case TERMINATED: - handleTerminatedState(response); - break; - default: - LOGGER.debug("StateChange message has no state, state is null {}", stateChangeMsg.getParticipantId()); - response.setResponseStatus(ParticipantResponseStatus.FAIL); - response.setResponseMessage( - "StateChange message has invalid state for participantId " + stateChangeMsg.getParticipantId()); - break; - } - - sender.sendResponse(response); - } - - /** * Method which handles a participant health check event from clamp. * - * @param healthCheckMsg participant health check message + * @param participantStatusReqMsg participant participantStatusReq message */ - public void handleParticipantHealthCheck(final ParticipantHealthCheck healthCheckMsg) { - var response = new ParticipantResponseDetails(healthCheckMsg); - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage(healthStatus.toString()); - - sender.sendResponse(response); + public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) { + ParticipantStatus participantStatus = new ParticipantStatus(); + participantStatus.setParticipantId(participantId); + participantStatus.setParticipantStatistics(participantStatistics); + participantStatus.setParticipantType(participantType); + participantStatus.setHealthStatus(healthStatus); + sender.sendParticipantStatus(participantStatus); } /** @@ -153,8 +116,8 @@ public class ParticipantHandler implements Closeable { * * @param updateMsg the update message */ - public void handleControlLoopUpdate(ParticipantControlLoopUpdate updateMsg) { - controlLoopHandler.handleControlLoopUpdate(updateMsg); + public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) { + controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant); } /** @@ -162,62 +125,17 @@ public class ParticipantHandler implements Closeable { * * @param stateChangeMsg the state change message */ - public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) { + public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) { controlLoopHandler.handleControlLoopStateChange(stateChangeMsg); } - /** - * Method to handle when the new state from participant is active. - * - * @param response participant response - */ - private void handleActiveState(final ParticipantResponseDetails response) { - handleStateChange(ParticipantState.ACTIVE, response); - } - - /** - * Method to handle when the new state from participant is passive. - * - * @param response participant response - */ - private void handlePassiveState(final ParticipantResponseDetails response) { - handleStateChange(ParticipantState.PASSIVE, response); - } - - /** - * Method to handle when the new state from participant is safe. - * - * @param response participant response - */ - private void handleSafeState(final ParticipantResponseDetails response) { - handleStateChange(ParticipantState.SAFE, response); - } - - /** - * Method to handle when the new state from participant is TEST. - * - * @param response participant response - */ - private void handleTestState(final ParticipantResponseDetails response) { - handleStateChange(ParticipantState.TEST, response); - } - - /** - * Method to handle when the new state from participant is Terminated. - * - * @param response participant response - */ - private void handleTerminatedState(final ParticipantResponseDetails response) { - handleStateChange(ParticipantState.TERMINATED, response); - } - - private void handleStateChange(ParticipantState newParticipantState, ParticipantResponseDetails response) { + private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) { if (state.equals(newParticipantState)) { - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage("Participant already in state " + newParticipantState); + response.setResult(false); + response.setMessage("Participant already in state " + newParticipantState); } else { - response.setResponseStatus(ParticipantResponseStatus.SUCCESS); - response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState); + response.setResult(true); + response.setMessage("Participant state changed from " + state + " to " + newParticipantState); state = newParticipantState; } } @@ -234,9 +152,10 @@ public class ParticipantHandler implements Closeable { LOGGER.debug("No participant with this ID {}", definition.getName()); return null; } - var response = new ParticipantResponseDetails(); - handleStateChange(participantState, response); - sender.sendResponse(response); + + var participantUpdateAck = new ParticipantUpdateAck(); + handleStateChange(participantState, participantUpdateAck); + sender.sendParticipantUpdateAck(participantUpdateAck); return getParticipant(definition.getName(), definition.getVersion()); } @@ -341,7 +260,21 @@ public class ParticipantHandler implements Closeable { participantUpdateAck.setResponseTo(messageId); participantUpdateAck.setMessage("Participant Update Ack message"); participantUpdateAck.setResult(true); + participantUpdateAck.setParticipantId(participantId); + participantUpdateAck.setParticipantType(participantType); sender.sendParticipantUpdateAck(participantUpdateAck); } + + /** + * Method to send heartbeat to controlloop runtime. + */ + public ParticipantStatus makeHeartbeat() { + ParticipantStatus heartbeat = new ParticipantStatus(); + heartbeat.setParticipantId(participantId); + heartbeat.setParticipantStatistics(participantStatistics); + heartbeat.setParticipantType(participantType); + heartbeat.setHealthStatus(healthStatus); + return heartbeat; + } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/parameters/ParticipantIntermediaryParameters.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/parameters/ParticipantIntermediaryParameters.java index 5627abfe6..fdc451e6b 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/parameters/ParticipantIntermediaryParameters.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/parameters/ParticipantIntermediaryParameters.java @@ -23,6 +23,7 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.parameters; import javax.validation.Valid; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Positive; import lombok.Getter; import lombok.Setter; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; @@ -50,7 +51,9 @@ public class ParticipantIntermediaryParameters { private ToscaConceptIdentifier participantType; // The time interval for periodic reporting of status to the CLAMP control loop server - private long reportingTimeInterval; + @Valid + @Positive + private long reportingTimeIntervalMs; @NotNull @ParameterGroupConstraint diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java index 93ba15846..9353cde81 100644 --- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java @@ -68,7 +68,7 @@ public class CommonTestData { map.put("participantId", getParticipantId()); map.put("description", DESCRIPTION); map.put("participantType", getParticipantId()); - map.put("reportingTimeInterval", TIME_INTERVAL); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); map.put("clampControlLoopTopics", getTopicParametersMap(false)); return map; |