05072c131f22062f7968e0bf90719b7f15533ff9
[externalapi/nbi.git] / src / main / java / org / onap / nbi / ServiceRegisterRunner.java
1 /**
2  * Copyright (c) 2018 Orange
3  * <p>
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
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.
15  */
16 package org.onap.nbi;
17
18 import com.google.common.base.Strings;
19 import java.net.InetAddress;
20 import java.util.HashSet;
21 import java.util.Set;
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;
31
32 /**
33  * Register this NBI instance with MSB discovery when the app is fully started
34  */
35 @Component
36 public class ServiceRegisterRunner implements CommandLineRunner {
37     private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterRunner.class);
38
39     @Value("${msb.enabled}")
40     private boolean IS_ENABLED;
41
42     @Value("${msb.discovery.host}")
43     private String DISCOVERY_HOST;
44
45     @Value("${msb.discovery.port}")
46     private int DISCOVERY_PORT;
47
48     @Value("${msb.discovery.retry}")
49     private int RETRY;
50
51     @Value("${msb.discovery.retry_interval}")
52     private int RETRY_INTERVAL;
53
54     @Value("${msb.service.host}")
55     private String SERVICE_HOST;
56
57     @Value("${server.port}")
58     private String SERVICE_PORT;
59
60     @Value("${msb.service.name}")
61     private String SERVICE_NAME;
62
63     @Value("${nbi.version}")
64     private String SERVICE_VERSION;
65
66     @Value("${server.servlet.context-path}")
67     private String SERVICE_URL;
68
69     @Value("${msb.service.custom_path}")
70     private String SERVICE_CUSTOM_PATH;
71
72     @Value("${msb.service.protocol}")
73     private String SERVICE_PROTOCOL;
74
75     @Value("${msb.service.visual_range}")
76     private String SERVICE_VISUAL_RANGE;
77
78     @Value("${msb.service.enable_ssl}")
79     private boolean SERVICE_ENABLE_SSL;
80
81     @Override
82     public void run(String... strings) throws Exception {
83         if (!IS_ENABLED) {
84             logger.info("Registration with msb discovery is not enabled");
85             return;
86         }
87
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);
95
96         if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) {
97             msinfo.setPath(SERVICE_CUSTOM_PATH);
98         }
99
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");
106         nodes.add(thisNode);
107         msinfo.setNodes(nodes);
108
109         logger.info(
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"
120         );
121
122         int attempt = 1;
123         while (attempt<=RETRY) {
124             try {
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());
129                 break;
130             } catch (Exception ex) {
131                 logger.warn("Registration with msb discovery (attempt {}/{}) FAILED.", attempt, RETRY);
132                 attempt += 1;
133                 if(attempt<=RETRY) {
134                     logger.warn("Sleep {}ms", RETRY_INTERVAL);
135                     Thread.sleep(RETRY_INTERVAL);
136                 }
137             }
138         }
139     }
140 }