/* * Copyright 2016-2017, Nokia Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core; import org.onap.msb.sdk.discovery.common.RouteException; import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; import org.onap.msb.sdk.discovery.entity.NodeInfo; import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import static java.lang.Integer.valueOf; import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure; import static org.slf4j.LoggerFactory.getLogger; /** * Responsible for providing REST client to access MSB API */ @Component public class MsbApiProvider extends IpMappingProvider { private static Logger logger = getLogger(MsbApiProvider.class); @Value("${messageBusIp}") private String messageBusIp; @Value("${messageBusPort}") private String messageBusPort; @Autowired MsbApiProvider(Environment environment) { super(environment); } /** * @return API to access ONAP MSB */ public MSBServiceClient getMsbClient() { return new MSBServiceClient(messageBusIp, valueOf(messageBusPort)); } /** * @param name the name of the micro service * @param version the version of the micro service * @return the base URL of the micro service (ex. https://1.2.3.4/path ) */ public String getMicroServiceUrl(String name, String version) { MicroServiceFullInfo microServiceFullInfo = getMicroServiceInfo(name, version); String protocol = "http://"; //FIXME the enable_ssl field should be used, but it is not available in SDK String ipAnPort = getNodeIpAnPort(microServiceFullInfo); //the field name in A&AI is misleading the URL is relative path postfixed to http(s)://ip:port String fullUrl = protocol + ipAnPort + microServiceFullInfo.getUrl(); return fullUrl; } private MicroServiceFullInfo getMicroServiceInfo(String name, String version) throws RuntimeException { try { return getMsbClient().queryMicroServiceInfo(name, version); } catch (RouteException e) { throw fatalFailure(logger, "Unable to get micro service URL for " + name + " with version " + version, e); } } private String getNodeIpAnPort(MicroServiceFullInfo microServiceFullInfo) { for (NodeInfo nodeInfo : microServiceFullInfo.getNodes()) { if (!nodeInfo.getIp().startsWith("172.")) { // FIXME how to know which of the multiple addresses to use? return mapPrivateIpToPublicIp(nodeInfo.getIp()) + ":" + nodeInfo.getPort(); } } throw fatalFailure(logger, "The " + microServiceFullInfo.getServiceName() + " service with " + microServiceFullInfo.getVersion() + " does not have any valid nodes" + microServiceFullInfo.getNodes()); } }