From 4026d98bc9802e41146ff6ef727d349c7ac1e89e Mon Sep 17 00:00:00 2001 From: Jakub Zieba Date: Tue, 4 Sep 2018 07:23:41 +0200 Subject: Add protocols to hosts Add default protocols for Consul hosts Add controller for healt check Fix for merging properties from file and Consul Change-Id: Ifa8c7cdfa333d8ffee175b5eb4446f0c3a986f27 Issue-ID: DCAEGEN2-747 Signed-off-by: Jakub Zieba --- src/main/java/org/onap/dcae/VesApplication.java | 3 +- .../onap/dcae/controller/ConfigFilesFacade.java | 30 +++++++----- .../org/onap/dcae/controller/ConfigSource.java | 10 ++-- .../onap/dcae/controller/EnvPropertiesReader.java | 27 +++++++++-- .../java/org/onap/dcae/controller/EnvProps.java | 14 ++++-- .../onap/dcae/restapi/HealthCheckController.java | 36 ++++++++++++++ .../java/org/onap/dcae/restapi/SwaggerConfig.java | 14 +----- .../java/org/onap/dcae/restapi/WebMvcConfig.java | 56 ++++++++++++++++++++++ 8 files changed, 152 insertions(+), 38 deletions(-) create mode 100644 src/main/java/org/onap/dcae/restapi/HealthCheckController.java create mode 100644 src/main/java/org/onap/dcae/restapi/WebMvcConfig.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/dcae/VesApplication.java b/src/main/java/org/onap/dcae/VesApplication.java index d9d12e35..a7426be2 100644 --- a/src/main/java/org/onap/dcae/VesApplication.java +++ b/src/main/java/org/onap/dcae/VesApplication.java @@ -42,8 +42,7 @@ import org.springframework.context.annotation.Lazy; import java.nio.file.Paths; import java.util.concurrent.*; -@SpringBootApplication -@EnableAutoConfiguration(exclude = {GsonAutoConfiguration.class, SecurityAutoConfiguration.class}) +@SpringBootApplication(exclude = {GsonAutoConfiguration.class, SecurityAutoConfiguration.class}) public class VesApplication { private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics"); diff --git a/src/main/java/org/onap/dcae/controller/ConfigFilesFacade.java b/src/main/java/org/onap/dcae/controller/ConfigFilesFacade.java index 42155eda..9338bf62 100644 --- a/src/main/java/org/onap/dcae/controller/ConfigFilesFacade.java +++ b/src/main/java/org/onap/dcae/controller/ConfigFilesFacade.java @@ -20,26 +20,25 @@ */ package org.onap.dcae.controller; -import static io.vavr.API.Try; -import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.enhanceError; -import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f; -import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.logError; -import static org.onap.dcae.controller.Conversions.toList; - import io.vavr.CheckedRunnable; import io.vavr.Tuple2; import io.vavr.collection.Map; import io.vavr.control.Try; -import java.io.FileNotFoundException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import static io.vavr.API.Try; +import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.*; +import static org.onap.dcae.controller.Conversions.toList; + class ConfigFilesFacade { private static Logger log = LoggerFactory.getLogger(ConfigFilesFacade.class); @@ -100,6 +99,7 @@ class ConfigFilesFacade { private PropertiesConfiguration readProperties() throws ConfigurationException { PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(); + propertiesConfiguration.setDelimiterParsingDisabled(true); propertiesConfiguration.load(propertiesPath.toFile()); return propertiesConfiguration; } @@ -109,12 +109,20 @@ class ConfigFilesFacade { PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(propertiesPath.toFile()); propertiesConfiguration.setEncoding(null); for (Tuple2 property : properties) { - propertiesConfiguration.addProperty(property._1, property._2); + updateProperty(propertiesConfiguration, property); } propertiesConfiguration.save(); }; } + private void updateProperty(PropertiesConfiguration propertiesConfiguration, Tuple2 property) { + if (propertiesConfiguration.containsKey(property._1)) { + propertiesConfiguration.setProperty(property._1, property._2); + } else { + propertiesConfiguration.addProperty(property._1, property._2); + } + } + private String indentConfiguration(String configuration) { return new JSONObject(configuration).toString(4); } diff --git a/src/main/java/org/onap/dcae/controller/ConfigSource.java b/src/main/java/org/onap/dcae/controller/ConfigSource.java index 7e6a9fc8..c20a22a2 100644 --- a/src/main/java/org/onap/dcae/controller/ConfigSource.java +++ b/src/main/java/org/onap/dcae/controller/ConfigSource.java @@ -43,7 +43,7 @@ final class ConfigSource { .peek(strBody -> log.info(f("Received following CBS configuration from Consul '%s'", strBody))) .flatMap(strBody -> toJsonArray(strBody)) .flatMap(json -> withdrawCatalog(json)) - .flatMap(json -> constructFullCBSUrl(json)) + .flatMap(json -> constructFullCBSUrl(envProps, json)) .flatMap(cbsUrl -> callCBSForAppConfig(envProps, cbsUrl)) .flatMap(strBody -> toJson(strBody)) .peek(jsonNode -> log.info(f("Received app configuration: '%s'", jsonNode))) @@ -51,12 +51,14 @@ final class ConfigSource { } private static Try callConsulForCBSConfiguration(EnvProps envProps) { - return executeGet(envProps.consulHost + ":" + envProps.consulPort + "/v1/catalog/service/" + envProps.cbsName) + return executeGet(envProps.consulProtocol + "://" + envProps.consulHost + ":" + + envProps.consulPort + "/v1/catalog/service/" + envProps.cbsName) .mapFailure(enhanceError("Unable to retrieve CBS configuration from Consul")); } - private static Try constructFullCBSUrl(JSONObject json) { - return Try(() -> json.get("ServiceAddress").toString() + ":" + json.get("ServicePort").toString()) + private static Try constructFullCBSUrl(EnvProps envProps, JSONObject json) { + return Try(() -> envProps.cbsProtocol + "://" + json.get("ServiceAddress").toString() + ":" + + json.get("ServicePort").toString()) .mapFailure(enhanceError("ServiceAddress / ServicePort missing from CBS conf: '%s'", json)); } diff --git a/src/main/java/org/onap/dcae/controller/EnvPropertiesReader.java b/src/main/java/org/onap/dcae/controller/EnvPropertiesReader.java index 23bcbda8..5f7a2fbd 100644 --- a/src/main/java/org/onap/dcae/controller/EnvPropertiesReader.java +++ b/src/main/java/org/onap/dcae/controller/EnvPropertiesReader.java @@ -20,15 +20,15 @@ */ package org.onap.dcae.controller; -import static io.vavr.API.List; -import static io.vavr.API.Try; -import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f; - import io.vavr.collection.Map; import io.vavr.control.Option; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static io.vavr.API.List; +import static io.vavr.API.Try; +import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f; + final class EnvPropertiesReader { private final static Logger log = LoggerFactory.getLogger(EnvPropertiesReader.class); @@ -36,11 +36,13 @@ final class EnvPropertiesReader { static Option readEnvProps(Map environmentVariables) { log.info("Loading necessary environment variables for dynamic configuration update"); int consulPort = getConsulPort(environmentVariables); + String consulProtocol = getConsulProtocol(environmentVariables); + String cbsProtocol = getCbsProtocol(environmentVariables); Option consulHost = getConsulHost(environmentVariables); Option cbsServiceName = getCBSName(environmentVariables); Option vesCollectorAppName = getAppName(environmentVariables); return Option.sequence(List(consulHost, cbsServiceName, vesCollectorAppName)) - .map(e -> new EnvProps(e.get(0), consulPort, e.get(1), e.get(2))) + .map(e -> new EnvProps(consulProtocol, e.get(0), consulPort, cbsProtocol, e.get(1), e.get(2))) .onEmpty(() -> log.warn("Some required environment variables are missing")) .peek(props -> log.info(f("Discovered following environment variables: '%s'", props))); } @@ -74,4 +76,19 @@ final class EnvPropertiesReader { + "is missing from environment variables.")); } + private static String getConsulProtocol(Map environmentVariables) { + return getProtocolFrom("CONSUL_PROTOCOL", environmentVariables); + } + + private static String getCbsProtocol(Map environmentVariables) { + return getProtocolFrom("CBS_PROTOCOL", environmentVariables); + } + + private static String getProtocolFrom(String variableName, Map environmentVariables) { + return environmentVariables.get(variableName) + .onEmpty(() -> log.warn("Consul protocol (env var: '" + variableName + "') is missing " + + "from environment variables.")) + .getOrElse("http"); + } + } diff --git a/src/main/java/org/onap/dcae/controller/EnvProps.java b/src/main/java/org/onap/dcae/controller/EnvProps.java index 2ee41cc6..5f7d08d5 100644 --- a/src/main/java/org/onap/dcae/controller/EnvProps.java +++ b/src/main/java/org/onap/dcae/controller/EnvProps.java @@ -26,14 +26,18 @@ import java.util.Objects; */ final class EnvProps { + final String consulProtocol; final String consulHost; final int consulPort; final String cbsName; + final String cbsProtocol; final String appName; - EnvProps(String consulHost, int consulPort, String cbsName, String appName) { + EnvProps(String consulProtocol, String consulHost, int consulPort, String cbsProtocol, String cbsName, String appName) { + this.consulProtocol = consulProtocol; this.consulHost = consulHost; this.consulPort = consulPort; + this.cbsProtocol = cbsProtocol; this.cbsName = cbsName; this.appName = appName; } @@ -41,8 +45,10 @@ final class EnvProps { @Override public String toString() { return "EnvProps{" + - "consulHost='" + consulHost + '\'' + + "consulProtocol='" + consulProtocol + '\'' + + ", consulHost='" + consulHost + '\'' + ", consulPort=" + consulPort + + ", cbsProtocol='" + cbsProtocol + '\'' + ", cbsName='" + cbsName + '\'' + ", appName='" + appName + '\'' + '}'; @@ -58,13 +64,15 @@ final class EnvProps { } EnvProps envProps = (EnvProps) o; return consulPort == envProps.consulPort && + Objects.equals(consulProtocol, envProps.consulProtocol) && Objects.equals(consulHost, envProps.consulHost) && + Objects.equals(cbsProtocol, envProps.cbsProtocol) && Objects.equals(cbsName, envProps.cbsName) && Objects.equals(appName, envProps.appName); } @Override public int hashCode() { - return Objects.hash(consulHost, consulPort, cbsName, appName); + return Objects.hash(consulProtocol, consulHost, consulPort, cbsProtocol, cbsName, appName); } } \ No newline at end of file diff --git a/src/main/java/org/onap/dcae/restapi/HealthCheckController.java b/src/main/java/org/onap/dcae/restapi/HealthCheckController.java new file mode 100644 index 00000000..9c65619c --- /dev/null +++ b/src/main/java/org/onap/dcae/restapi/HealthCheckController.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018 Nokia. All rights reserved.s + * ================================================================================ + * 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.dcae.restapi; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + + +@Controller +public class HealthCheckController { + + @GetMapping("/healthcheck") + public String healthCheck() { + return "hello"; + } + +} diff --git a/src/main/java/org/onap/dcae/restapi/SwaggerConfig.java b/src/main/java/org/onap/dcae/restapi/SwaggerConfig.java index d8554cbe..60740a80 100644 --- a/src/main/java/org/onap/dcae/restapi/SwaggerConfig.java +++ b/src/main/java/org/onap/dcae/restapi/SwaggerConfig.java @@ -22,8 +22,6 @@ package org.onap.dcae.restapi; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; @@ -32,7 +30,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 -public class SwaggerConfig extends WebMvcConfigurationSupport { +public class SwaggerConfig{ @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) @@ -42,14 +40,4 @@ public class SwaggerConfig extends WebMvcConfigurationSupport { .build(); } - @Override - protected void addResourceHandlers(ResourceHandlerRegistry registry) { - registry - .addResourceHandler("swagger-ui.html") - .addResourceLocations("classpath:/META-INF/resources/"); - - registry - .addResourceHandler("/webjars/**") - .addResourceLocations("classpath:/META-INF/resources/webjars/"); - } } diff --git a/src/main/java/org/onap/dcae/restapi/WebMvcConfig.java b/src/main/java/org/onap/dcae/restapi/WebMvcConfig.java new file mode 100644 index 00000000..7059c4e5 --- /dev/null +++ b/src/main/java/org/onap/dcae/restapi/WebMvcConfig.java @@ -0,0 +1,56 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018 Nokia. All rights reserved.s + * ================================================================================ + * 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.dcae.restapi; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +public class WebMvcConfig extends WebMvcConfigurationSupport { + + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/"); + + registry + .addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); + + registry + .addResourceHandler("**") + .addResourceLocations("classpath:/templates/"); + } + + @Bean + public InternalResourceViewResolver jspViewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/"); + resolver.setSuffix(".html"); + return resolver; + } + +} -- cgit 1.2.3-korg