6b19dd55090685b609edd355a16044f60020b6d8
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / vfc / VfcRestApiProvider.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.vfc;
17
18 import com.google.common.annotations.VisibleForTesting;
19 import java.util.concurrent.TimeUnit;
20 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
21 import org.onap.vfccatalog.api.VnfpackageApi;
22 import org.onap.vnfmdriver.ApiClient;
23 import org.onap.vnfmdriver.api.NslcmApi;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Component;
26
27 /**
28  * Responsible for providing access to VF-C REST APIs
29  */
30 @Component
31 public class VfcRestApiProvider {
32     static final String NSLCM_API_SERVICE_NAME = "nslcm";
33     static final String NSLCM_API_VERION = "v1";
34     static final String NSCATALOG_SERVICE_NAME = "catalog";
35     static final String NSCATALOG_API_VERSION = "v1";
36     private final MsbApiProvider msbApiProvider;
37
38     @Autowired
39     VfcRestApiProvider(MsbApiProvider msbApiProvider) {
40         this.msbApiProvider = msbApiProvider;
41     }
42
43     /**
44      * @return API to access VF-C for granting & LCN API
45      */
46     public NslcmApi getNsLcmApi() {
47         return buildNslcmApiClient().createService(NslcmApi.class);
48     }
49
50     @VisibleForTesting
51     ApiClient buildNslcmApiClient() {
52         ApiClient apiClient = new ApiClient();
53         //LCN processing in VF-C is very slow
54         apiClient.getOkBuilder().connectTimeout(3, TimeUnit.MINUTES);
55         apiClient.getOkBuilder().readTimeout(3, TimeUnit.MINUTES);
56         apiClient.getOkBuilder().writeTimeout(3, TimeUnit.MINUTES);
57         String correctedUrl = fixIncorrectUrl();
58         if (!correctedUrl.endsWith("/")) {
59             correctedUrl = correctedUrl + "/";
60         }
61         apiClient.setAdapterBuilder(apiClient.getAdapterBuilder().baseUrl(correctedUrl));
62         return apiClient;
63     }
64
65     /**
66      * @return API to access VF-C catalog API
67      */
68     public VnfpackageApi getVfcCatalogApi() {
69         return buildCatalogApiClient().createService(VnfpackageApi.class);
70     }
71
72     @VisibleForTesting
73     org.onap.vfccatalog.ApiClient buildCatalogApiClient() {
74         org.onap.vfccatalog.ApiClient vfcApiClient = new org.onap.vfccatalog.ApiClient();
75         String microServiceUrl = msbApiProvider.getMicroServiceUrl(NSCATALOG_SERVICE_NAME, NSCATALOG_API_VERSION);
76         if (!microServiceUrl.endsWith("/")) {
77             microServiceUrl = microServiceUrl + "/";
78         }
79         vfcApiClient.setAdapterBuilder(vfcApiClient.getAdapterBuilder().baseUrl(microServiceUrl));
80         return vfcApiClient;
81     }
82
83     /**
84      * The swagger schema definition is not consistent with MSB info. The MSB reports
85      * the base path /restapi/nsclm/v1 (correct) and the paths defined in swagger is
86      * /nsclm/v1 making all API calls /restapi/nsclm/v1/nsclm/v1 (incorrect)
87      *
88      * @return
89      */
90     private String fixIncorrectUrl() {
91         String urlInMsb = msbApiProvider.getMicroServiceUrl(NSLCM_API_SERVICE_NAME, NSLCM_API_VERION);
92         //FIXME VF-C exposes multiple APIs in the single swagger definition, since the base path of different
93         //API is different the some API calls are incorrectly prefixed
94         //VF-C team refuses to fix this in Amsterdam https://jira.onap.org/browse/VFC-597?filter=-2
95         return urlInMsb.replaceFirst("/nslcm/v1", "");
96     }
97 }