add option to disable registration with msb
[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 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;
28
29 import java.net.InetAddress;
30 import java.util.HashSet;
31 import java.util.Set;
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.service.host}")
50     private String SERVICE_HOST;
51
52     @Value("${msb.service.port}")
53     private String SERVICE_PORT;
54
55     @Value("${msb.service.name}")
56     private String SERVICE_NAME;
57
58     @Value("${msb.service.version}")
59     private String SERVICE_VERSION;
60
61     @Value("${msb.service.url}")
62     private String SERVICE_URL;
63
64     @Value("${msb.service.custom_path}")
65     private String SERVICE_CUSTOM_PATH;
66
67     @Value("${msb.service.protocol}")
68     private String SERVICE_PROTOCOL;
69
70     @Value("${msb.service.visual_range}")
71     private String SERVICE_VISUAL_RANGE;
72
73     @Value("${msb.service.enable_ssl}")
74     private boolean SERVICE_ENABLE_SSL;
75
76     @Override
77     public void run(String... strings) throws Exception {
78         if (!IS_ENABLED) return;
79
80         MicroServiceInfo msinfo = new MicroServiceInfo();
81         msinfo.setServiceName(SERVICE_NAME);
82         msinfo.setVersion(SERVICE_VERSION);
83         msinfo.setUrl(SERVICE_URL);
84         msinfo.setProtocol(SERVICE_PROTOCOL);
85         msinfo.setVisualRange(SERVICE_VISUAL_RANGE);
86         msinfo.setEnable_ssl(SERVICE_ENABLE_SSL);
87
88         if (!Strings.isNullOrEmpty(SERVICE_CUSTOM_PATH)) {
89             msinfo.setPath(SERVICE_CUSTOM_PATH);
90         }
91
92         Set<Node> nodes = new HashSet<>();
93         Node thisNode = new Node();
94         thisNode.setIp(Strings.isNullOrEmpty(SERVICE_HOST) ? InetAddress.getLocalHost().getHostAddress() : SERVICE_HOST);
95         thisNode.setPort(SERVICE_PORT);
96         thisNode.setCheckType("HTTP");
97         thisNode.setCheckUrl(SERVICE_URL + "/status");
98         nodes.add(thisNode);
99         msinfo.setNodes(nodes);
100
101         logger.info(
102                 "Register this service with msb discovery (" + DISCOVERY_HOST + ":" + DISCOVERY_PORT + "):\n"
103                         + " - host: [" + thisNode.getIp() + "]\n"
104                         + " - port: [" + thisNode.getPort() + "]\n"
105                         + " - name: [" + msinfo.getServiceName() + "]\n"
106                         + " - version: [" + msinfo.getVersion() + "]\n"
107                         + " - url: [" + msinfo.getUrl() + "]\n"
108                         + " - path: [" + msinfo.getPath() + "]\n"
109                         + " - protocol: [" + msinfo.getProtocol() + "]\n"
110                         + " - visualRange: [" + msinfo.getVisualRange() + "]\n"
111                         + " - enableSSL: [" + SERVICE_ENABLE_SSL + "]\n"
112         );
113
114         MSBServiceClient msbClient = new MSBServiceClient(DISCOVERY_HOST, DISCOVERY_PORT);
115         MicroServiceFullInfo microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo);
116
117         logger.debug("microServiceFullInfo = {}", microServiceFullInfo.toString());
118     }
119 }