2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 import com.google.common.base.Strings;
19 import java.net.InetAddress;
20 import java.util.HashSet;
22 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
24 import org.onap.msb.sdk.discovery.entity.Node;
25 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.CommandLineRunner;
30 import org.springframework.stereotype.Component;
33 * Register this NBI instance with MSB discovery when the app is fully started
36 public class ServiceRegisterRunner implements CommandLineRunner {
37 private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterRunner.class);
39 @Value("${msb.enabled}")
40 private boolean IS_ENABLED;
42 @Value("${msb.discovery.host}")
43 private String DISCOVERY_HOST;
45 @Value("${msb.discovery.port}")
46 private int DISCOVERY_PORT;
48 @Value("${msb.discovery.retry}")
51 @Value("${msb.discovery.retry_interval}")
52 private int RETRY_INTERVAL;
54 @Value("${msb.service.host}")
55 private String SERVICE_HOST;
57 @Value("${server.port}")
58 private String SERVICE_PORT;
60 @Value("${msb.service.name}")
61 private String SERVICE_NAME;
63 @Value("${nbi.version}")
64 private String SERVICE_VERSION;
66 @Value("${server.servlet.context-path}")
67 private String SERVICE_URL;
69 @Value("${msb.service.custom_path}")
70 private String SERVICE_CUSTOM_PATH;
72 @Value("${msb.service.protocol}")
73 private String SERVICE_PROTOCOL;
75 @Value("${msb.service.visual_range}")
76 private String SERVICE_VISUAL_RANGE;
78 @Value("${msb.service.enable_ssl}")
79 private boolean SERVICE_ENABLE_SSL;
82 public void run(String... strings) throws Exception {
84 logger.info("Registration with msb discovery is not enabled");
88 MicroServiceInfo msinfo = new MicroServiceInfo();
89 msinfo.setServiceName(SERVICE_NAME);
90 msinfo.setVersion(SERVICE_VERSION);
91 msinfo.setUrl(SERVICE_URL);
92 msinfo.setProtocol(SERVICE_PROTOCOL);
93 msinfo.setVisualRange(SERVICE_VISUAL_RANGE);
94 msinfo.setEnable_ssl(SERVICE_ENABLE_SSL);
96 if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) {
97 msinfo.setPath(SERVICE_CUSTOM_PATH);
100 Set<Node> nodes = new HashSet<>();
101 Node thisNode = new Node();
102 thisNode.setIp(Strings.isNullOrEmpty(SERVICE_HOST) ? InetAddress.getLocalHost().getHostAddress() : SERVICE_HOST);
103 thisNode.setPort(SERVICE_PORT);
104 thisNode.setCheckType("HTTP");
105 thisNode.setCheckUrl(SERVICE_URL + "/status");
107 msinfo.setNodes(nodes);
110 "Registering with msb discovery (" + DISCOVERY_HOST + ":" + DISCOVERY_PORT + "):\n"
111 + " - host: [" + thisNode.getIp() + "]\n"
112 + " - port: [" + thisNode.getPort() + "]\n"
113 + " - name: [" + msinfo.getServiceName() + "]\n"
114 + " - version: [" + msinfo.getVersion() + "]\n"
115 + " - url: [" + msinfo.getUrl() + "]\n"
116 + " - path: [" + msinfo.getPath() + "]\n"
117 + " - protocol: [" + msinfo.getProtocol() + "]g\n"
118 + " - visualRange: [" + msinfo.getVisualRange() + "]\n"
119 + " - enableSSL: [" + SERVICE_ENABLE_SSL + "]\n"
123 while (attempt<=RETRY) {
125 logger.info("Registration with msb discovery (attempt {}/{})", attempt, RETRY);
126 MSBServiceClient msbClient = new MSBServiceClient(DISCOVERY_HOST, DISCOVERY_PORT);
127 MicroServiceFullInfo microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo);
128 logger.debug("Registration with msb discovery done, microServiceFullInfo = {}", microServiceFullInfo.toString());
130 } catch (Exception ex) {
131 logger.warn("Registration with msb discovery (attempt {}/{}) FAILED.", attempt, RETRY);
134 logger.warn("Sleep {}ms", RETRY_INTERVAL);
135 Thread.sleep(RETRY_INTERVAL);