Merge "Enable vfc-ztevnfmdriver sonar scan"
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / CbamRestApiProvider.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.vnfm;
17
18 import com.google.common.io.BaseEncoding;
19 import com.nokia.cbam.catalog.v1.api.DefaultApi;
20 import com.nokia.cbam.lcm.v32.ApiClient;
21 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
22 import com.nokia.cbam.lcm.v32.api.VnfsApi;
23 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
25 import org.onap.vnfmdriver.model.VnfmInfo;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.stereotype.Component;
29
30 import java.io.ByteArrayInputStream;
31
32 /**
33  * Responsible for providing client to access CBAM REST API
34  */
35 @Component
36 public class CbamRestApiProvider {
37     public static final String NOKIA_LCN_API_VERSION = "3.2";
38     public static final String NOKIA_LCM_API_VERSION = "3.2";
39     private final DriverProperties driverProperties;
40     private final CbamTokenProvider tokenProvider;
41     private final VnfmInfoProvider vnfmInfoProvider;
42
43     @Value("${trustedCertificates}")
44     private String trustedCertificates;
45     @Value("${skipCertificateVerification}")
46     private boolean skipCertificateVerification;
47
48     @Autowired
49     public CbamRestApiProvider(DriverProperties driverProperties, CbamTokenProvider cbamTokenProvider, VnfmInfoProvider vnfmInfoProvider) {
50         this.driverProperties = driverProperties;
51         this.tokenProvider = cbamTokenProvider;
52         this.vnfmInfoProvider = vnfmInfoProvider;
53     }
54
55     /**
56      * @param vnfmId the identifier of the VNFM
57      * @return API to access CBAM LCM API
58      */
59     public VnfsApi getCbamLcmApi(String vnfmId) {
60         return new VnfsApi(getLcmApiClient(vnfmId));
61     }
62
63     /**
64      * @param vnfmId the identifier of the VNFM
65      * @return API to access the operation executions
66      */
67     public OperationExecutionsApi getCbamOperationExecutionApi(String vnfmId) {
68         return new OperationExecutionsApi(getLcmApiClient(vnfmId));
69     }
70
71     /**
72      * @param vnfmId the identifier of the VNFM
73      * @return API to access CBAM LCN subscription API
74      */
75     public SubscriptionsApi getCbamLcnApi(String vnfmId) {
76         com.nokia.cbam.lcn.v32.ApiClient apiClient = new com.nokia.cbam.lcn.v32.ApiClient();
77         if (!skipCertificateVerification) {
78             apiClient.setSslCaCert(new ByteArrayInputStream(BaseEncoding.base64().decode(trustedCertificates)));
79         } else {
80             apiClient.setVerifyingSsl(false);
81         }
82         apiClient.setBasePath(driverProperties.getCbamLcnUrl());
83         apiClient.setAccessToken(tokenProvider.getToken(vnfmId));
84         return new SubscriptionsApi(apiClient);
85     }
86
87     /**
88      * @param vnfmId the identifier of the VNFM
89      * @return API to access CBAM catalog API
90      */
91     public DefaultApi getCbamCatalogApi(String vnfmId) {
92         com.nokia.cbam.catalog.v1.ApiClient apiClient = new com.nokia.cbam.catalog.v1.ApiClient();
93         if (!skipCertificateVerification) {
94             apiClient.setSslCaCert(new ByteArrayInputStream(BaseEncoding.base64().decode(trustedCertificates)));
95         } else {
96             apiClient.setVerifyingSsl(false);
97         }
98         apiClient.setBasePath(driverProperties.getCbamCatalogUrl());
99         apiClient.setAccessToken(tokenProvider.getToken(vnfmId));
100         return new DefaultApi(apiClient);
101     }
102
103     private ApiClient getLcmApiClient(String vnfmId) {
104         VnfmInfo vnfmInfo = vnfmInfoProvider.getVnfmInfo(vnfmId);
105         ApiClient apiClient = new ApiClient();
106         if (!skipCertificateVerification) {
107             apiClient.setSslCaCert(new ByteArrayInputStream(BaseEncoding.base64().decode(trustedCertificates)));
108         } else {
109             apiClient.setVerifyingSsl(false);
110         }
111         apiClient.setAccessToken(tokenProvider.getToken(vnfmId));
112         apiClient.setBasePath(vnfmInfo.getUrl());
113         return apiClient;
114     }
115 }