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 org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
21 import org.onap.msb.sdk.discovery.entity.Node;
22 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.boot.CommandLineRunner;
27 import org.springframework.stereotype.Component;
29 import java.net.InetAddress;
30 import java.util.HashSet;
34 * Register this NBI instance with MSB discovery when the app is fully started
37 public class ServiceRegisterRunner implements CommandLineRunner {
38 private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterRunner.class);
40 @Value("${msb.enabled}")
41 private boolean IS_ENABLED;
43 @Value("${msb.discovery.host}")
44 private String DISCOVERY_HOST;
46 @Value("${msb.discovery.port}")
47 private int DISCOVERY_PORT;
49 @Value("${msb.discovery.retry}")
52 @Value("${msb.discovery.retry_interval}")
53 private int RETRY_INTERVAL;
55 @Value("${msb.service.host}")
56 private String SERVICE_HOST;
58 @Value("${server.port}")
59 private String SERVICE_PORT;
61 @Value("${msb.service.name}")
62 private String SERVICE_NAME;
64 @Value("${nbi.version}")
65 private String SERVICE_VERSION;
67 @Value("${server.contextPath}")
68 private String SERVICE_URL;
70 @Value("${msb.service.custom_path}")
71 private String SERVICE_CUSTOM_PATH;
73 @Value("${msb.service.protocol}")
74 private String SERVICE_PROTOCOL;
76 @Value("${msb.service.visual_range}")
77 private String SERVICE_VISUAL_RANGE;
79 @Value("${msb.service.enable_ssl}")
80 private boolean SERVICE_ENABLE_SSL;
83 public void run(String... strings) throws Exception {
85 logger.info("Registration with msb discovery is not enabled");
89 MicroServiceInfo msinfo = new MicroServiceInfo();
90 msinfo.setServiceName(SERVICE_NAME);
91 msinfo.setVersion(SERVICE_VERSION);
92 msinfo.setUrl(SERVICE_URL);
93 msinfo.setProtocol(SERVICE_PROTOCOL);
94 msinfo.setVisualRange(SERVICE_VISUAL_RANGE);
95 msinfo.setEnable_ssl(SERVICE_ENABLE_SSL);
97 if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) {
98 msinfo.setPath(SERVICE_CUSTOM_PATH);
101 Set<Node> nodes = new HashSet<>();
102 Node thisNode = new Node();
103 thisNode.setIp(Strings.isNullOrEmpty(SERVICE_HOST) ? InetAddress.getLocalHost().getHostAddress() : SERVICE_HOST);
104 thisNode.setPort(SERVICE_PORT);
105 thisNode.setCheckType("HTTP");
106 thisNode.setCheckUrl(SERVICE_URL + "/status");
108 msinfo.setNodes(nodes);
111 "Registering with msb discovery (" + DISCOVERY_HOST + ":" + DISCOVERY_PORT + "):\n"
112 + " - host: [" + thisNode.getIp() + "]\n"
113 + " - port: [" + thisNode.getPort() + "]\n"
114 + " - name: [" + msinfo.getServiceName() + "]\n"
115 + " - version: [" + msinfo.getVersion() + "]\n"
116 + " - url: [" + msinfo.getUrl() + "]\n"
117 + " - path: [" + msinfo.getPath() + "]\n"
118 + " - protocol: [" + msinfo.getProtocol() + "]g\n"
119 + " - visualRange: [" + msinfo.getVisualRange() + "]\n"
120 + " - enableSSL: [" + SERVICE_ENABLE_SSL + "]\n"
124 while (attempt<RETRY) {
127 logger.info("Registration with msb discovery (attempt {}/{})", attempt, RETRY);
128 MSBServiceClient msbClient = new MSBServiceClient(DISCOVERY_HOST, DISCOVERY_PORT);
129 MicroServiceFullInfo microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo);
130 logger.debug("Registration with msb discovery done, microServiceFullInfo = {}", microServiceFullInfo.toString());
132 } catch (Exception ex) {
133 logger.info("Registration with msb discovery (attempt {}/{}) FAILED. Sleep {}ms", attempt, RETRY, RETRY_INTERVAL);
135 Thread.sleep(RETRY_INTERVAL);