summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/AAIRestApiProvider.java
blob: 08bf2c4104ca08091e730a16ee5c276af9e1241e (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * 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.direct;

import com.google.common.annotations.VisibleForTesting;
import okhttp3.Credentials;
import okhttp3.Request;
import org.onap.aai.ApiClient;
import org.onap.aai.api.CloudInfrastructureApi;
import org.onap.aai.api.ExternalSystemApi;
import org.onap.aai.api.NetworkApi;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;

/**
 * Responsible for providing access to AAI APIs.
 * Handles authentication and mandatory parameters.
 */
@Component
public class AAIRestApiProvider {
    private final MsbApiProvider msbApiProvider;
    private final AaiSecurityProvider aaiSecurityProvider;
    @Value("${aaiUsername}")
    private String aaiUsername;
    @Value("${aaiPassword}")
    private String aaiPassword;

    @Autowired
    AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
        this.msbApiProvider = msbApiProvider;
        this.aaiSecurityProvider = aaiSecurityProvider;
    }

    /**
     * @return API to access the cloud infrastructure
     */
    public CloudInfrastructureApi getCloudInfrastructureApi() {
        return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
    }

    /**
     * @return API to access the external systems
     */
    public ExternalSystemApi getExternalSystemApi() {
        return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
    }

    /**
     * @return API to access the networking
     */
    public NetworkApi getNetworkApi() {
        return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);

    }

    @VisibleForTesting
    ApiClient buildApiClient(AAIService service) {
        ApiClient apiClient = new ApiClient();
        apiClient.getOkBuilder().sslSocketFactory(aaiSecurityProvider.buildSSLSocketFactory(), aaiSecurityProvider.buildTrustManager());
        apiClient.getOkBuilder().hostnameVerifier(aaiSecurityProvider.buildHostnameVerifier());
        apiClient.getOkBuilder().addInterceptor(chain -> {
            Request request = chain.request().newBuilder().addHeader("X-FromAppId", SERVICE_NAME).build();
            return chain.proceed(request);
        });
        apiClient.getOkBuilder().authenticator((route, response) -> {
            String credential = Credentials.basic(aaiUsername, aaiPassword);
            return response.request().newBuilder().header("Authorization", credential).build();
        });
        String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11");
        if (!url.endsWith("/")) {
            url = url + "/";
        }
        apiClient.getAdapterBuilder().baseUrl(url);
        return apiClient;
    }

    enum AAIService {
        NETWORK {
            String getServiceName() {
                return "aai-network";
            }
        },
        ESR {
            String getServiceName() {
                return "aai-externalSystem";
            }
        },
        CLOUD {
            String getServiceName() {
                return "aai-cloudInfrastructure";
            }
        };

        abstract String getServiceName();
    }
}