From 2ce68d3c8423d644056becc8ef1e3bb0df731c42 Mon Sep 17 00:00:00 2001 From: Piotr Bocheński Date: Fri, 8 Feb 2019 13:38:02 +0100 Subject: Add git info to PRH + cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id518d56373f6ccab9dd4195ad08b67589e0d9b4f Issue-ID: DCAEGEN2-1030 Signed-off-by: Piotr Bocheński --- .../services/prh/configuration/Config.java | 5 +- .../services/prh/configuration/PrhAppConfig.java | 72 ++++++++++++---------- .../prh/controllers/AppInfoController.java | 63 +++++++++++++++++++ .../prh/controllers/HeartbeatController.java | 65 ------------------- prh-app-server/src/main/resources/application.yaml | 8 --- .../src/main/resources/logback-spring.xml | 2 +- .../prh/configuration/PrhAppConfigTest.java | 32 +++++----- 7 files changed, 122 insertions(+), 125 deletions(-) create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/AppInfoController.java delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java (limited to 'prh-app-server/src') diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java index 613e9a83..d26fbd81 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA 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. @@ -23,12 +23,15 @@ package org.onap.dcaegen2.services.prh.configuration; import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; +import org.springframework.core.io.Resource; /** * @author Przemysław Wąsala on 4/25/18 */ public interface Config { + Resource getGitInfo(); + DmaapConsumerConfiguration getDmaapConsumerConfiguration(); AaiClientConfiguration getAaiClientConfiguration(); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java index 612fab48..85f7e983 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA 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. @@ -20,18 +20,8 @@ package org.onap.dcaegen2.services.prh.configuration; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import com.google.gson.TypeAdapterFactory; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.ServiceLoader; -import javax.validation.constraints.NotNull; +import com.google.common.annotations.VisibleForTesting; +import com.google.gson.*; import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; @@ -42,6 +32,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; +import org.springframework.util.StreamUtils; + +import javax.annotation.PostConstruct; +import javax.validation.constraints.NotNull; +import java.io.*; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.ServiceLoader; /** * @author Przemysław Wąsala on 4/9/18 @@ -50,6 +48,7 @@ import org.springframework.core.io.Resource; @EnableConfigurationProperties @ConfigurationProperties("app") public abstract class PrhAppConfig implements Config { + private static final Logger LOGGER = LoggerFactory.getLogger(PrhAppConfig.class); private static final String CONFIG = "configs"; private static final String AAI = "aai"; @@ -59,8 +58,6 @@ public abstract class PrhAppConfig implements Config { private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration"; private static final String SECURITY = "security"; - private static final Logger LOGGER = LoggerFactory.getLogger(PrhAppConfig.class); - AaiClientConfiguration aaiClientConfiguration; DmaapConsumerConfiguration dmaapConsumerConfiguration; @@ -68,7 +65,20 @@ public abstract class PrhAppConfig implements Config { DmaapPublisherConfiguration dmaapPublisherConfiguration; @Value("classpath:prh_endpoints.json") - private Resource resourceFile; + private Resource prhEndpoints; + + @Value("classpath:git_info.json") + private Resource gitInfo; + + @PostConstruct + private void printGitInfo() throws IOException { + LOGGER.debug("Git info={}", StreamUtils.copyToString(gitInfo.getInputStream(), Charset.defaultCharset())); + } + + @Override + public Resource getGitInfo() { + return gitInfo; + } @Override public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { @@ -87,21 +97,19 @@ public abstract class PrhAppConfig implements Config { @Override public void initFileStreamReader() { - GsonBuilder gsonBuilder = new GsonBuilder(); ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); JsonParser parser = new JsonParser(); - try (InputStream inputStream = resourceFile.getInputStream()) { + try (InputStream inputStream = prhEndpoints.getInputStream()) { JsonElement rootElement = getJsonElement(parser, inputStream); if (rootElement.isJsonObject()) { deserializeAaiConfiguration(gsonBuilder, rootElement); deserializeDmaapConsumerConfiguration(gsonBuilder, rootElement); deserializeDmaapPublisherConfiguration(gsonBuilder, rootElement); } - } - catch (IOException e) { - LOGGER.warn("Problem with file loading, file ", e); + } catch (IOException e) { + LOGGER.warn("Failed to load/parse file", e); } } @@ -109,8 +117,8 @@ public abstract class PrhAppConfig implements Config { dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects( rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP) .getAsJsonObject(DMAAP_PRODUCER), - rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)), - DmaapPublisherConfiguration.class); + rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)), + DmaapPublisherConfiguration.class); } private void deserializeDmaapConsumerConfiguration(GsonBuilder gsonBuilder, JsonElement rootElement) { @@ -118,7 +126,7 @@ public abstract class PrhAppConfig implements Config { rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP) .getAsJsonObject(DMAAP_CONSUMER), rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)), - DmaapConsumerConfiguration.class); + DmaapConsumerConfiguration.class); } private void deserializeAaiConfiguration(GsonBuilder gsonBuilder, JsonElement rootElement) { @@ -134,22 +142,22 @@ public abstract class PrhAppConfig implements Config { private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) { source.entrySet() - .forEach(entry -> target.add(entry.getKey(), entry.getValue())); + .forEach(entry -> target.add(entry.getKey(), entry.getValue())); return target; } private T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject, - @NotNull Class type) { + @NotNull Class type) { try { return gsonBuilder.create().fromJson(jsonObject, type); - } catch (JsonSyntaxException e) { - LOGGER.warn("Problem with Json deserialization", e); + } catch (JsonSyntaxException e) { + LOGGER.warn("Failed to parse JSON={}", jsonObject, e); return null; } } - void setResourceFile(Resource resourceFile) { - this.resourceFile = resourceFile; + @VisibleForTesting + void setPrhEndpoints(Resource prhEndpoints) { + this.prhEndpoints = prhEndpoints; } - -} \ No newline at end of file +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/AppInfoController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/AppInfoController.java new file mode 100644 index 00000000..9a49dee5 --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/AppInfoController.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018-2019 NOKIA 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 + * + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.controllers; + +import io.swagger.annotations.*; +import org.onap.dcaegen2.services.prh.configuration.Config; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 4/19/18 + */ +@RestController +@Api(value = "AppInfoController", description = "Provides basic information about application") +public class AppInfoController { + + private static final Logger LOGGER = LoggerFactory.getLogger(AppInfoController.class); + private final Resource gitInfo; + + @Autowired + public AppInfoController(Config config) { + gitInfo = config.getGitInfo(); + } + + @GetMapping(value = "heartbeat", produces = MediaType.TEXT_PLAIN_VALUE) + @ApiOperation("Returns liveness of PRH service") + @ApiResponses(@ApiResponse(code = 200, message = "Service is alive")) + public Mono> heartbeat() { + LOGGER.trace("Heartbeat request received"); + return Mono.defer(() -> Mono.just(new ResponseEntity<>("alive", HttpStatus.OK)) + ); + } + + @GetMapping(value = "version", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation("Returns version information") + public Mono version() { + return Mono.defer(() -> Mono.just(gitInfo)); + } +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java deleted file mode 100644 index c09cc945..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA 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 - * - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.controllers; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; - -/** - * @author Przemysław Wąsala on 4/19/18 - */ -@RestController -@Api(value = "HeartbeatController", description = "Check liveness of PRH service") -public class HeartbeatController { - - private static final Logger LOGGER = LoggerFactory.getLogger(HeartbeatController.class); - - /** - * Endpoint for checking that PRH is alive. - * - * @return HTTP Status Code - */ - @RequestMapping(value = "heartbeat", method = RequestMethod.GET) - @ApiOperation(value = "Returns liveness of PRH service") - @ApiResponses(value = { - @ApiResponse(code = 200, message = "PRH sevice is living"), - @ApiResponse(code = 401, message = "You are not authorized to view the resource"), - @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"), - @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") - } - ) - public Mono> heartbeat() { - LOGGER.trace("Receiving heartbeat request"); - return Mono.defer(() -> - Mono.just(new ResponseEntity<>("alive", HttpStatus.OK)) - ); - } -} \ No newline at end of file diff --git a/prh-app-server/src/main/resources/application.yaml b/prh-app-server/src/main/resources/application.yaml index 88d1e401..a9a89421 100644 --- a/prh-app-server/src/main/resources/application.yaml +++ b/prh-app-server/src/main/resources/application.yaml @@ -9,11 +9,3 @@ server: key-store: classpath:keystore-local key-password: nokiapnf keyAlias: tomcat-localhost -logging: - level: - ROOT: ERROR - org.onap.dcaegen2.services.prh: INFO - reactor.ipc.netty.http.client: WARN - org.springframework: ERROR - org.springframework.data: ERROR - org.springframework.web.reactive: WARN diff --git a/prh-app-server/src/main/resources/logback-spring.xml b/prh-app-server/src/main/resources/logback-spring.xml index 8f9bfd1c..70908b07 100644 --- a/prh-app-server/src/main/resources/logback-spring.xml +++ b/prh-app-server/src/main/resources/logback-spring.xml @@ -1,5 +1,5 @@ - + diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java index 42acc592..249fccc3 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA 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. @@ -20,17 +20,6 @@ package org.onap.dcaegen2.services.prh.configuration; -import static java.lang.ClassLoader.getSystemResource; -import static java.nio.file.Files.readAllBytes; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Paths; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -38,11 +27,18 @@ import org.onap.dcaegen2.services.prh.integration.junit5.mockito.MockitoExtensio import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; +import java.io.*; +import java.nio.file.Paths; + +import static java.lang.ClassLoader.getSystemResource; +import static java.nio.file.Files.readAllBytes; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; /** * @author Przemysław Wąsala on 4/9/18 */ -@ExtendWith({MockitoExtension.class}) +@ExtendWith({ MockitoExtension.class }) class PrhAppConfigTest { private static final String CORRECT_CONFIG_FILE = "correct_config.json"; @@ -58,7 +54,7 @@ class PrhAppConfigTest { @Test void whenTheConfigurationFits() throws Exception { InputStream inputStream = createInputStream(CORRECT_CONFIG_FILE); - appConfig.setResourceFile(new InputStreamResource(inputStream)); + appConfig.setPrhEndpoints(new InputStreamResource(inputStream)); appConfig.initFileStreamReader(); assertNotNull(appConfig.getDmaapConsumerConfiguration()); @@ -71,7 +67,7 @@ class PrhAppConfigTest { InputStream inputStream = createInputStream(CORRECT_CONFIG_FILE); Resource resource = spy(new InputStreamResource(inputStream)); when(resource.getInputStream()).thenThrow(new IOException()); - appConfig.setResourceFile(resource); + appConfig.setPrhEndpoints(resource); appConfig.initFileStreamReader(); assertNull(appConfig.getAaiClientConfiguration()); @@ -82,7 +78,7 @@ class PrhAppConfigTest { @Test void whenFileExistsButDmaapPublisherJsonConfigurationIsIncorrect() throws Exception { InputStream inputStream = createInputStream(INCORRECT_CONFIG_FILE); - appConfig.setResourceFile(new InputStreamResource(inputStream)); + appConfig.setPrhEndpoints(new InputStreamResource(inputStream)); appConfig.initFileStreamReader(); assertNotNull(appConfig.getAaiClientConfiguration()); @@ -93,7 +89,7 @@ class PrhAppConfigTest { @Test void whenRootElementIsNotAJsonObject() throws Exception { InputStream inputStream = createInputStream(NOT_JSON_OBJECT_FILE); - appConfig.setResourceFile(new InputStreamResource(inputStream)); + appConfig.setPrhEndpoints(new InputStreamResource(inputStream)); appConfig.initFileStreamReader(); @@ -105,4 +101,4 @@ class PrhAppConfigTest { private InputStream createInputStream(String jsonFile) throws Exception { return new ByteArrayInputStream(readAllBytes(Paths.get(getSystemResource(jsonFile).toURI()))); } -} \ No newline at end of file +} -- cgit 1.2.3-korg