diff options
Diffstat (limited to 'runtime-controlloop')
13 files changed, 182 insertions, 14 deletions
diff --git a/runtime-controlloop/pom.xml b/runtime-controlloop/pom.xml index 508f44a80..54405548f 100644 --- a/runtime-controlloop/pom.xml +++ b/runtime-controlloop/pom.xml @@ -84,6 +84,16 @@ <version>${version.springfox}</version> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + <version>${version.springboot}</version> + </dependency> + <dependency> + <groupId>io.micrometer</groupId> + <artifactId>micrometer-registry-prometheus</artifactId> + <version>${version.io.micrometer}</version> + </dependency> + <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <scope>runtime</scope> diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java index ee0461994..b14c675df 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java @@ -20,9 +20,12 @@ package org.onap.policy.clamp.controlloop.runtime.config; +import java.util.Arrays; import java.util.List; import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -32,5 +35,9 @@ public class ConverterConfiguration implements WebMvcConfigurer { public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new CoderHttpMesageConverter<>("yaml")); converters.add(new CoderHttpMesageConverter<>("json")); + + StringHttpMessageConverter converter = new StringHttpMessageConverter(); + converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN)); + converters.add(converter); } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/web/RuntimeErrorController.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/web/RuntimeErrorController.java index 2dc7a6b8e..ac2dba355 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/web/RuntimeErrorController.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/web/RuntimeErrorController.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -83,7 +85,7 @@ public class RuntimeErrorController implements ErrorController { Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request), ErrorAttributeOptions.defaults()); - StringBuilder sb = new StringBuilder(); + var sb = new StringBuilder(); final Object error = map.get("error"); if (error != null) { sb.append(error.toString()).append(" "); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java index b39573461..c68b7fd49 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java index a05337991..65149a733 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -204,7 +206,7 @@ public class SupervisionScanner { return; } - boolean completed = true; + var completed = true; for (ControlLoopElement element : controlLoop.getElements().values()) { if (!element.getState().equals(element.getOrderedState().asState())) { completed = false; diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java index 35219bb68..6df030d32 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -59,7 +61,7 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con } controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates); - LOGGER.debug("ControlLoopUpdate message sent", controlLoopUpdateMsg); + LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg); super.send(controlLoopUpdateMsg); } @@ -68,7 +70,7 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con if (participantUpdates.isEmpty()) { participantUpdates.add(getControlLoopElementList(clElement)); } else { - boolean participantExists = false; + var participantExists = false; for (ParticipantUpdates participantUpdate : participantUpdates) { if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) { participantUpdate.getControlLoopElementList().add(clElement); @@ -82,7 +84,7 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con } private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) { - ParticipantUpdates participantUpdate = new ParticipantUpdates(); + var participantUpdate = new ParticipantUpdates(); List<ControlLoopElement> controlLoopElementList = new ArrayList<>(); participantUpdate.setParticipantId(clElement.getParticipantId()); controlLoopElementList.add(clElement); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java index d239f38cb..efd262560 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -36,7 +38,6 @@ import org.onap.policy.models.provider.PolicyModelsProvider; 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.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -117,7 +118,7 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId, controlLoopElementDefinitionList)); } else { - boolean participantExists = false; + var participantExists = false; for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) { participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); diff --git a/runtime-controlloop/src/main/resources/application.yaml b/runtime-controlloop/src/main/resources/application.yaml index ea98aaa8c..cddb3d0fb 100644 --- a/runtime-controlloop/src/main/resources/application.yaml +++ b/runtime-controlloop/src/main/resources/application.yaml @@ -50,3 +50,9 @@ runtime: servers: - ${topicServer:localhost} topicCommInfrastructure: dmaap + +management: + endpoints: + web: + exposure: + include: health, metrics, prometheus diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java new file mode 100644 index 000000000..433e914ae --- /dev/null +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.runtime.main.rest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; +import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@AutoConfigureMetrics +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class ActuatorControllerTest extends CommonRestController { + + private static final String HEALTH_ENDPOINT = "health"; + private static final String METRICS_ENDPOINT = "metrics"; + private static final String PROMETHEUS_ENDPOINT = "prometheus"; + + @LocalServerPort + private int randomServerPort; + + @BeforeEach + public void setUpPort() { + super.setHttpPrefix(randomServerPort); + } + + @Test + void testGetHealth_Unauthorized() throws Exception { + assertUnauthorizedActGet(HEALTH_ENDPOINT); + } + + @Test + void testGetMetrics_Unauthorized() throws Exception { + assertUnauthorizedActGet(METRICS_ENDPOINT); + } + + @Test + void testGetPrometheus_Unauthorized() throws Exception { + assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + } + + @Test + void testGetHealth() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGetMetrics() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGePrometheus() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } +} diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java index 5f885ec97..3ac6b6fa4 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -229,7 +231,7 @@ class TestMonitoringProvider { var element = new ControlLoopElement(); element.setParticipantId(new ToscaConceptIdentifier("name1", "1.001")); controlLoop.setElements(Map.of(UUID.randomUUID(), element)); - when(mockClProvider.getControlLoop(eq(new ToscaConceptIdentifier("testName", "1.001")))) + when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testName", "1.001"))) .thenReturn(controlLoop); ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001"); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionAspectTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionAspectTest.java index 30ee9b1b9..b31aa8e4f 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionAspectTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionAspectTest.java @@ -2,6 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * 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 @@ -37,7 +39,7 @@ class SupervisionAspectTest { var supervisionScanner = spy(mock(SupervisionScanner.class)); try (var supervisionAspect = new SupervisionAspect(supervisionScanner)) { supervisionAspect.schedule(); - verify(supervisionScanner, timeout(500)).run(eq(true)); + verify(supervisionScanner, timeout(500)).run(true); } } @@ -47,7 +49,7 @@ class SupervisionAspectTest { try (var supervisionAspect = new SupervisionAspect(supervisionScanner)) { supervisionAspect.doCheck(); supervisionAspect.doCheck(); - verify(supervisionScanner, timeout(500).times(2)).run(eq(false)); + verify(supervisionScanner, timeout(500).times(2)).run(false); } } @@ -60,7 +62,7 @@ class SupervisionAspectTest { try (var supervisionAspect = new SupervisionAspect(supervisionScanner)) { supervisionAspect.handleParticipantStatus(participantStatusMessage); - verify(supervisionScanner, timeout(500)).handleParticipantStatus(eq(identifier)); + verify(supervisionScanner, timeout(500)).handleParticipantStatus(identifier); } } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java index eebaa5281..ccac0c63b 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java @@ -42,7 +42,9 @@ import org.onap.policy.common.utils.network.NetworkUtil; public class CommonRestController { public static final String SELF = NetworkUtil.getHostname(); - public static final String ENDPOINT_PREFIX = "onap/controlloop/v2/"; + public static final String CONTEXT_PATH = "onap/controlloop"; + public static final String ENDPOINT_PREFIX = CONTEXT_PATH + "/v2/"; + public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/"; private static String httpPrefix; @@ -71,7 +73,18 @@ public class CommonRestController { } /** - * Sends a request to an endpoint, without any authorization header. + * Sends a request to an actuator endpoint. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true); + } + + /** + * Sends a request to an Rest Api endpoint, without any authorization header. * * @param endpoint the target endpoint * @return a request builder @@ -82,6 +95,17 @@ public class CommonRestController { } /** + * Sends a request to an actuator endpoint, without any authorization header. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false); + } + + /** * Sends a request to a fully qualified endpoint. * * @param fullyQualifiedEndpoint the fully qualified target endpoint @@ -102,7 +126,7 @@ public class CommonRestController { final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); + return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); } /** @@ -141,6 +165,17 @@ public class CommonRestController { } /** + * Assert that GET call to actuator endpoint is Unauthorized. + * + * @param endPoint the endpoint + * @throws Exception if an error occurs + */ + protected void assertUnauthorizedActGet(final String endPoint) throws Exception { + Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); + } + + /** * Assert that DELETE call is Unauthorized. * * @param endPoint the endpoint @@ -159,4 +194,8 @@ public class CommonRestController { protected void setHttpPrefix(int port) { httpPrefix = "http://" + SELF + ":" + port + "/"; } + + protected String getHttpPrefix() { + return httpPrefix; + } } diff --git a/runtime-controlloop/src/test/resources/application_test.properties b/runtime-controlloop/src/test/resources/application_test.properties index ad2a4b12c..0074d9f95 100644 --- a/runtime-controlloop/src/test/resources/application_test.properties +++ b/runtime-controlloop/src/test/resources/application_test.properties @@ -28,3 +28,5 @@ runtime.topicParameterGroup.topicSources[0].fetchTimeout=15000 runtime.topicParameterGroup.topicSinks[0].topic=POLICY-CLRUNTIME-PARTICIPANT runtime.topicParameterGroup.topicSinks[0].servers[0]=localhost runtime.topicParameterGroup.topicSinks[0].topicCommInfrastructure=dmaap + +management.endpoints.web.exposure.include=health,metrics,prometheus |