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 org.onap.msb.sdk.discovery.common.RouteException;
19 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
20 import org.onap.msb.sdk.discovery.entity.NodeInfo;
21 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
22 import org.slf4j.Logger;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.core.env.Environment;
26 import org.springframework.stereotype.Component;
28 import static java.lang.Integer.valueOf;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
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 MSBServiceClient getMsbClient() {
52 return new MSBServiceClient(messageBusIp, valueOf(messageBusPort));
56 * @param name the name of the micro service
57 * @param version the version of the micro service
58 * @return the base URL of the micro service (ex. https://1.2.3.4/path )
60 public String getMicroServiceUrl(String name, String version) {
61 MicroServiceFullInfo microServiceFullInfo = getMicroServiceInfo(name, version);
62 String ipAnPort = getNodeIpAnPort(microServiceFullInfo);
63 //FIXME the enable_ssl field should be used, but it is not available in SDK depends on MSB-151 jira issue
64 String protocol = (ipAnPort.endsWith(":8443") || ipAnPort.endsWith(":443")) ? "https://" : "http://";
65 //the field name in A&AI is misleading the URL is relative path postfixed to http(s)://ip:port
66 return protocol + ipAnPort + microServiceFullInfo.getUrl();
69 private MicroServiceFullInfo getMicroServiceInfo(String name, String version) {
71 return getMsbClient().queryMicroServiceInfo(name, version);
72 } catch (RouteException e) {
73 throw fatalFailure(logger, "Unable to get micro service URL for " + name + " with version " + version, e);
77 private String getNodeIpAnPort(MicroServiceFullInfo microServiceFullInfo) {
78 for (NodeInfo nodeInfo : microServiceFullInfo.getNodes()) {
79 if (isADokcerInternalAddress(nodeInfo)) {
80 return mapPrivateIpToPublicIp(nodeInfo.getIp()) + ":" + nodeInfo.getPort();
83 throw fatalFailure(logger, "The " + microServiceFullInfo.getServiceName() + " service with " + microServiceFullInfo.getVersion() + " does not have any valid nodes" + microServiceFullInfo.getNodes());
86 private boolean isADokcerInternalAddress(NodeInfo nodeInfo) {
87 return !nodeInfo.getIp().startsWith("172.");