2 * Copyright 2016-2017, Nokia Corporation
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
18 import com.google.common.annotations.VisibleForTesting;
19 import org.onap.msb.ApiClient;
20 import org.onap.msb.api.ServiceResourceApi;
21 import org.onap.msb.model.MicroServiceFullInfo;
22 import org.onap.msb.model.NodeInfo;
23 import org.slf4j.Logger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.core.env.Environment;
27 import org.springframework.stereotype.Component;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
30 import static org.slf4j.LoggerFactory.getLogger;
33 * Responsible for providing REST client to access MSB API
36 public class MsbApiProvider extends IpMappingProvider {
37 private static Logger logger = getLogger(MsbApiProvider.class);
38 @Value("${messageBusIp}")
39 private String messageBusIp;
40 @Value("${messageBusPort}")
41 private String messageBusPort;
44 MsbApiProvider(Environment environment) {
49 * @return API to access ONAP MSB
51 public ServiceResourceApi getMsbApi() {
52 return buildApiClient().createService(ServiceResourceApi.class);
56 ApiClient buildApiClient() {
57 ApiClient apiClient = new ApiClient();
58 apiClient.setAdapterBuilder(apiClient.getAdapterBuilder().baseUrl("http://" + messageBusIp + ":" + messageBusPort + "/api/msdiscover/v1/"));
63 * @param name the name of the micro service
64 * @param version the version of the micro service
65 * @return the base URL of the micro service (ex. https://1.2.3.4/path )
67 public String getMicroServiceUrl(String name, String version) {
68 MicroServiceFullInfo microServiceFullInfo = getMicroServiceInfo(name, version);
69 String ipAnPort = getNodeIpAnPort(microServiceFullInfo);
70 String protocol = microServiceFullInfo.isEnableSsl() ? "https://" : "http://";
71 //the field name in A&AI is misleading the URL is relative path postfixed to http(s)://ip:port
72 return protocol + ipAnPort + microServiceFullInfo.getUrl();
75 private MicroServiceFullInfo getMicroServiceInfo(String name, String version) {
77 return getMsbApi().getMicroService_0(name, version, null, null, null, null, null).blockingFirst();
78 } catch (Exception e) {
79 throw buildFatalFailure(logger, "Unable to get micro service URL for " + name + " with version " + version, e);
83 private String getNodeIpAnPort(MicroServiceFullInfo microServiceFullInfo) {
84 for (NodeInfo nodeInfo : microServiceFullInfo.getNodes()) {
85 if (isADokcerInternalAddress(nodeInfo)) {
86 return mapPrivateIpToPublicIp(nodeInfo.getIp()) + ":" + nodeInfo.getPort();
89 throw buildFatalFailure(logger, "The " + microServiceFullInfo.getServiceName() + " service with " + microServiceFullInfo.getVersion() + " does not have any valid nodes" + microServiceFullInfo.getNodes());
92 private boolean isADokcerInternalAddress(NodeInfo nodeInfo) {
93 return !nodeInfo.getIp().startsWith("172.");