summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/VfcRestApiProvider.java
blob: 5c9dc5436978567ba4e4358e51929099a4f3fcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * 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.vfc;

import com.google.common.annotations.VisibleForTesting;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
import org.onap.vfccatalog.api.VnfpackageApi;
import org.onap.vnfmdriver.ApiClient;
import org.onap.vnfmdriver.api.NslcmApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Responsible for providing access to VF-C REST APIs
 */
@Component
public class VfcRestApiProvider {
    static final String NSLCM_API_SERVICE_NAME = "nslcm";
    static final String NSLCM_API_VERION = "v1";
    static final String NSCATALOG_SERVICE_NAME = "catalog";
    static final String NSCATALOG_API_VERSION = "v1";
    private final MsbApiProvider msbApiProvider;

    @Autowired
    VfcRestApiProvider(MsbApiProvider msbApiProvider) {
        this.msbApiProvider = msbApiProvider;
    }

    /**
     * @return API to access VF-C for granting & LCN API
     */
    public NslcmApi getNsLcmApi() {
        return buildNslcmApiClient().createService(NslcmApi.class);
    }

    @VisibleForTesting
    ApiClient buildNslcmApiClient() {
        ApiClient apiClient = new ApiClient();
        String correctedUrl = fixIncorrectUrl();
        if (!correctedUrl.endsWith("/")) {
            correctedUrl = correctedUrl + "/";
        }
        apiClient.setAdapterBuilder(apiClient.getAdapterBuilder().baseUrl(correctedUrl));
        return apiClient;
    }

    /**
     * @return API to access VF-C catalog API
     */
    public VnfpackageApi getVfcCatalogApi() {
        return buildCatalogApiClient().createService(VnfpackageApi.class);
    }

    @VisibleForTesting
    org.onap.vfccatalog.ApiClient buildCatalogApiClient() {
        org.onap.vfccatalog.ApiClient vfcApiClient = new org.onap.vfccatalog.ApiClient();
        String microServiceUrl = msbApiProvider.getMicroServiceUrl(NSCATALOG_SERVICE_NAME, NSCATALOG_API_VERSION);
        if (!microServiceUrl.endsWith("/")) {
            microServiceUrl = microServiceUrl + "/";
        }
        vfcApiClient.setAdapterBuilder(vfcApiClient.getAdapterBuilder().baseUrl(microServiceUrl));
        return vfcApiClient;
    }

    /**
     * The swagger schema definition is not consistent with MSB info. The MSB reports
     * the base path /restapi/nsclm/v1 (correct) and the paths defined in swagger is
     * /nsclm/v1 making all API calls /restapi/nsclm/v1/nsclm/v1 (incorrect)
     *
     * @return
     */
    private String fixIncorrectUrl() {
        String urlInMsb = msbApiProvider.getMicroServiceUrl(NSLCM_API_SERVICE_NAME, NSLCM_API_VERION);
        //FIXME VF-C exposes multiple APIs in the single swagger definition, since the base path of different
        //API is different the some API calls are incorrectly prefixed
        //VF-C team refuses to fix this in Amsterdam https://jira.onap.org/browse/VFC-597?filter=-2
        return urlInMsb.replaceFirst("/nslcm/v1", "");
    }
}