Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / MsbApiProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
17
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;
27
28 import static java.lang.Integer.valueOf;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
30 import static org.slf4j.LoggerFactory.getLogger;
31
32 /**
33  * Responsible for providing REST client to access MSB API
34  */
35 @Component
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;
42
43     @Autowired
44     MsbApiProvider(Environment environment) {
45         super(environment);
46     }
47
48     /**
49      * @return API to access ONAP MSB
50      */
51     public MSBServiceClient getMsbClient() {
52         return new MSBServiceClient(messageBusIp, valueOf(messageBusPort));
53     }
54
55     /**
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 )
59      */
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
64         //depends on https://jira.onap.org/browse/MSB-151
65         String protocol = (ipAnPort.endsWith(":8443") || ipAnPort.endsWith(":443")) ? "https://" : "http://";
66         //the field name in A&AI is misleading the URL is relative path postfixed to http(s)://ip:port
67         return protocol + ipAnPort + microServiceFullInfo.getUrl();
68     }
69
70     private MicroServiceFullInfo getMicroServiceInfo(String name, String version) {
71         try {
72             return getMsbClient().queryMicroServiceInfo(name, version);
73         } catch (RouteException e) {
74             throw buildFatalFailure(logger, "Unable to get micro service URL for " + name + " with version " + version, e);
75         }
76     }
77
78     private String getNodeIpAnPort(MicroServiceFullInfo microServiceFullInfo) {
79         for (NodeInfo nodeInfo : microServiceFullInfo.getNodes()) {
80             if (isADokcerInternalAddress(nodeInfo)) {
81                 return mapPrivateIpToPublicIp(nodeInfo.getIp()) + ":" + nodeInfo.getPort();
82             }
83         }
84         throw buildFatalFailure(logger, "The " + microServiceFullInfo.getServiceName() + " service with " + microServiceFullInfo.getVersion() + " does not have any valid nodes" + microServiceFullInfo.getNodes());
85     }
86
87     private boolean isADokcerInternalAddress(NodeInfo nodeInfo) {
88         return !nodeInfo.getIp().startsWith("172.");
89     }
90 }