Format Java code with respect to ONAP Code Style
[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
17 package org.onap.nbi;
18
19 import com.google.common.base.Strings;
20 import java.net.InetAddress;
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
25 import org.onap.msb.sdk.discovery.entity.Node;
26 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.boot.CommandLineRunner;
31 import org.springframework.stereotype.Component;
32
33 /**
34  * Register this NBI instance with MSB discovery when the app is fully started
35  */
36 @Component
37 public class ServiceRegisterRunner implements CommandLineRunner {
38     private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterRunner.class);
39
40     @Value("${msb.enabled}")
41     private boolean IS_ENABLED;
42
43     @Value("${msb.discovery.host}")
44     private String DISCOVERY_HOST;
45
46     @Value("${msb.discovery.port}")
47     private int DISCOVERY_PORT;
48
49     @Value("${msb.discovery.retry}")
50     private int RETRY;
51
52     @Value("${msb.discovery.retry_interval}")
53     private int RETRY_INTERVAL;
54
55     @Value("${msb.service.host}")
56     private String SERVICE_HOST;
57
58     @Value("${server.port}")
59     private String SERVICE_PORT;
60
61     @Value("${msb.service.name}")
62     private String SERVICE_NAME;
63
64     @Value("${nbi.version}")
65     private String SERVICE_VERSION;
66
67     @Value("${server.servlet.context-path}")
68     private String SERVICE_URL;
69
70     @Value("${msb.service.custom_path}")
71     private String SERVICE_CUSTOM_PATH;
72
73     @Value("${msb.service.protocol}")
74     private String SERVICE_PROTOCOL;
75
76     @Value("${msb.service.visual_range}")
77     private String SERVICE_VISUAL_RANGE;
78
79     @Value("${msb.service.enable_ssl}")
80     private boolean SERVICE_ENABLE_SSL;
81
82     @Override
83     public void run(String... strings) throws Exception {
84         if (!IS_ENABLED) {
85             logger.info("Registration with msb discovery is not enabled");
86             return;
87         }
88
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);
96
97         if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) {
98             msinfo.setPath(SERVICE_CUSTOM_PATH);
99         }
100
101         Set<Node> nodes = new HashSet<>();
102         Node thisNode = new Node();
103         thisNode.setIp(
104                 Strings.isNullOrEmpty(SERVICE_HOST) ? InetAddress.getLocalHost().getHostAddress() : SERVICE_HOST);
105         thisNode.setPort(SERVICE_PORT);
106         thisNode.setCheckType("HTTP");
107         thisNode.setCheckUrl(SERVICE_URL + "/status");
108         nodes.add(thisNode);
109         msinfo.setNodes(nodes);
110
111         logger.info("Registering with msb discovery (" + DISCOVERY_HOST + ":" + DISCOVERY_PORT + "):\n" + " - host: ["
112                 + thisNode.getIp() + "]\n" + " - port: [" + thisNode.getPort() + "]\n" + " - name: ["
113                 + msinfo.getServiceName() + "]\n" + " - version: [" + msinfo.getVersion() + "]\n" + " - url: ["
114                 + msinfo.getUrl() + "]\n" + " - path: [" + msinfo.getPath() + "]\n" + " - protocol: ["
115                 + msinfo.getProtocol() + "]g\n" + " - visualRange: [" + msinfo.getVisualRange() + "]\n"
116                 + " - enableSSL: [" + SERVICE_ENABLE_SSL + "]\n");
117
118         int attempt = 1;
119         while (attempt <= RETRY) {
120             try {
121                 logger.info("Registration with msb discovery (attempt {}/{})", attempt, RETRY);
122                 MSBServiceClient msbClient = new MSBServiceClient(DISCOVERY_HOST, DISCOVERY_PORT);
123                 MicroServiceFullInfo microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo);
124                 logger.debug("Registration with msb discovery done, microServiceFullInfo = {}",
125                         microServiceFullInfo.toString());
126                 break;
127             } catch (Exception ex) {
128                 logger.warn("Registration with msb discovery (attempt {}/{}) FAILED.", attempt, RETRY);
129                 attempt += 1;
130                 if (attempt <= RETRY) {
131                     logger.warn("Sleep {}ms", RETRY_INTERVAL);
132                     Thread.sleep(RETRY_INTERVAL);
133                 }
134             }
135         }
136     }
137 }