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