Removing jackson to mitigate cve-2017-4995
[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 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;
28
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 ServiceResourceApi getMsbApi() {
52         return buildApiClient().createService(ServiceResourceApi.class);
53     }
54
55     @VisibleForTesting
56     ApiClient buildApiClient() {
57         ApiClient apiClient = new ApiClient();
58         apiClient.setAdapterBuilder(apiClient.getAdapterBuilder().baseUrl("http://" + messageBusIp + ":" + messageBusPort + "/api/msdiscover/v1/"));
59         return apiClient;
60     }
61
62     /**
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 )
66      */
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();
73     }
74
75     private MicroServiceFullInfo getMicroServiceInfo(String name, String version) {
76         try {
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);
80         }
81     }
82
83     private String getNodeIpAnPort(MicroServiceFullInfo microServiceFullInfo) {
84         for (NodeInfo nodeInfo : microServiceFullInfo.getNodes()) {
85             if (isADokcerInternalAddress(nodeInfo)) {
86                 return mapPrivateIpToPublicIp(nodeInfo.getIp()) + ":" + nodeInfo.getPort();
87             }
88         }
89         throw buildFatalFailure(logger, "The " + microServiceFullInfo.getServiceName() + " service with " + microServiceFullInfo.getVersion() + " does not have any valid nodes" + microServiceFullInfo.getNodes());
90     }
91
92     private boolean isADokcerInternalAddress(NodeInfo nodeInfo) {
93         return !nodeInfo.getIp().startsWith("172.");
94     }
95 }