From d44d6c6ea00241f586ee7fb3b173821d98decbab Mon Sep 17 00:00:00 2001 From: Quoc Nghia Nguyen Date: Wed, 16 May 2018 17:19:55 +0200 Subject: [PATCH] Register with MSB after being launched Change-Id: I96c84f8b8e0757d68461a0e631d6ed432f64ecad Issue-ID: EXTAPI-90 Signed-off-by: Quoc-Nghia Nguyen --- .env | 14 ++- docker-compose.yml | 2 + pom.xml | 7 ++ .../java/org/onap/nbi/ServiceRegisterRunner.java | 114 +++++++++++++++++++++ src/main/resources/application.properties | 15 ++- src/test/java/org/onap/nbi/apis/TestConfig.java | 29 ++++++ src/test/resources/application.properties | 13 +++ 7 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/onap/nbi/ServiceRegisterRunner.java create mode 100644 src/test/java/org/onap/nbi/apis/TestConfig.java diff --git a/.env b/.env index b084635..f613e42 100644 --- a/.env +++ b/.env @@ -17,7 +17,6 @@ # APPLICATION SERVER_CONTEXTPATH=/nbi/api/v1 SERVER_PORT=8080 -LOGGING_LEVEL=INFO # ONAP ONAP_LCPCLOUDREGIONID= @@ -44,6 +43,19 @@ SO_HOST=http://localhost:8090 SO_HEADER_AUTHORIZATION= SO_API_ID=SO +# MSB +MSB_DISCOVERY_HOST=msb_discovery +MSB_DISCOVERY_PORT=10081 +MSB_SERVICE_HOST= +MSB_SERVICE_PORT=8080 +MSB_SERVICE_NAME=nbi +MSB_SERVICE_VERSION=v1 +MSB_SERVICE_URL=/nbi/api/v1 +MSB_SERVICE_CUSTOM_PATH= +MSB_SERVICE_PROTOCOL=REST +MSB_SERVICE_VISUAL_RANGE=1 +MSB_SERVICE_ENABLE_SSL=false + # MONGO SPRING_DATA_MONGODB_HOST=localhost SPRING_DATA_MONGODB_PORT=27017 diff --git a/docker-compose.yml b/docker-compose.yml index ead0801..cc73775 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,6 +38,8 @@ services: image: ${NEXUS_DOCKER_REPO}/onap/externalapi/nbi:latest ports: - 8080:8080 + env_file: + - .env environment: SPRING_DATASOURCE_URL: jdbc:mariadb://mariadb:3306/nbi SPRING_DATASOURCE_PASSWORD: toto diff --git a/pom.xml b/pom.xml index ac83f88..8c849e3 100644 --- a/pom.xml +++ b/pom.xml @@ -262,6 +262,13 @@ runtime + + + org.onap.msb.java-sdk + msb-java-sdk + 1.1.1 + + diff --git a/src/main/java/org/onap/nbi/ServiceRegisterRunner.java b/src/main/java/org/onap/nbi/ServiceRegisterRunner.java new file mode 100644 index 0000000..e8cca00 --- /dev/null +++ b/src/main/java/org/onap/nbi/ServiceRegisterRunner.java @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2018 Orange + *

+ * 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. + */ +package org.onap.nbi; + +import com.google.common.base.Strings; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.net.InetAddress; +import java.util.HashSet; +import java.util.Set; + +/** + * Register this NBI instance with MSB discovery when the app is fully started + */ +@Component +public class ServiceRegisterRunner implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterRunner.class); + + @Value("${msb.discovery.host}") + private String DISCOVERY_HOST; + + @Value("${msb.discovery.port}") + private int DISCOVERY_PORT; + + @Value("${msb.service.host}") + private String SERVICE_HOST; + + @Value("${msb.service.port}") + private String SERVICE_PORT; + + @Value("${msb.service.name}") + private String SERVICE_NAME; + + @Value("${msb.service.version}") + private String SERVICE_VERSION; + + @Value("${msb.service.url}") + private String SERVICE_URL; + + @Value("${msb.service.custom_path}") + private String SERVICE_CUSTOM_PATH; + + @Value("${msb.service.protocol}") + private String SERVICE_PROTOCOL; + + @Value("${msb.service.visual_range}") + private String SERVICE_VISUAL_RANGE; + + @Value("${msb.service.enable_ssl}") + private boolean SERVICE_ENABLE_SSL; + + @Override + public void run(String... strings) throws Exception { + MicroServiceInfo msinfo = new MicroServiceInfo(); + msinfo.setServiceName(SERVICE_NAME); + msinfo.setVersion(SERVICE_VERSION); + msinfo.setUrl(SERVICE_URL); + msinfo.setProtocol(SERVICE_PROTOCOL); + msinfo.setVisualRange(SERVICE_VISUAL_RANGE); + msinfo.setEnable_ssl(SERVICE_ENABLE_SSL); + + if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) { + msinfo.setPath(SERVICE_CUSTOM_PATH); + } + + Set nodes = new HashSet<>(); + Node thisNode = new Node(); + thisNode.setIp(Strings.isNullOrEmpty(SERVICE_HOST) ? InetAddress.getLocalHost().getHostAddress() : SERVICE_HOST); + thisNode.setPort(SERVICE_PORT); + thisNode.setCheckType("HTTP"); + thisNode.setCheckUrl(SERVICE_URL + "/status"); + nodes.add(thisNode); + msinfo.setNodes(nodes); + + logger.info( + "Register this service with msb discovery (" + DISCOVERY_HOST + ":" + DISCOVERY_PORT + "):\n" + + " - host: [" + thisNode.getIp() + "]\n" + + " - port: [" + thisNode.getPort() + "]\n" + + " - name: [" + msinfo.getServiceName() + "]\n" + + " - version: [" + msinfo.getVersion() + "]\n" + + " - url: [" + msinfo.getUrl() + "]\n" + + " - path: [" + msinfo.getPath() + "]\n" + + " - protocol: [" + msinfo.getProtocol() + "]\n" + + " - visualRange: [" + msinfo.getVisualRange() + "]\n" + + " - enableSSL: [" + SERVICE_ENABLE_SSL + "]\n" + ); + + MSBServiceClient msbClient = new MSBServiceClient(DISCOVERY_HOST, DISCOVERY_PORT); + MicroServiceFullInfo microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo); + + logger.debug("microServiceFullInfo = {}", microServiceFullInfo.toString()); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7c9975f..cc3a2b9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,7 +16,7 @@ # SERVER server.contextPath=/nbi/api/v1 -server.port = 8080 +server.port=8080 # LOGGING logging.level.=INFO @@ -49,6 +49,19 @@ so.owning.entity.id=6b5b6b70-4e9a-4f6f-8b7b-cbd7cf990c6e so.owning.entity.name=OE-generic so.project.name=Project-generic +# MSB +msb.discovery.host=msb_discovery +msb.discovery.port=10081 +msb.service.host= +msb.service.port=${server.port} +msb.service.name=nbi +msb.service.version=v1 +msb.service.url=${server.contextPath} +msb.service.custom_path= +msb.service.protocol=REST +msb.service.visual_range=1 +msb.service.enable_ssl=false + # MONGO spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 diff --git a/src/test/java/org/onap/nbi/apis/TestConfig.java b/src/test/java/org/onap/nbi/apis/TestConfig.java new file mode 100644 index 0000000..a3afa1c --- /dev/null +++ b/src/test/java/org/onap/nbi/apis/TestConfig.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2018 Orange + *

+ * 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. + */ + +package org.onap.nbi.apis; + +import org.onap.nbi.ServiceRegisterRunner; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; + +@SpringBootApplication +// Exclude MSB register runner when running tests +@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ServiceRegisterRunner.class)) +public class TestConfig { +} + diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 1361d42..a297fbf 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -49,6 +49,19 @@ so.owning.entity.id=6b5b6b70-4e9a-4f6f-8b7b-cbd7cf990c6e so.owning.entity.name=OE-generic so.project.name=Project-generic +# MSB +msb.discovery.host=msb_discovery +msb.discovery.port=10081 +msb.service.host= +msb.service.port=${server.port} +msb.service.name=nbi +msb.service.version=v1 +msb.service.url=${server.contextPath} +msb.service.custom_path= +msb.service.protocol=REST +msb.service.visual_range=1 +msb.service.enable_ssl=false + # H2 spring.datasource.url=jdbc:h2:mem:~/db;DB_CLOSE_ON_EXIT=false spring.datasource.username=sa -- 2.16.6