2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;
18 import com.google.common.io.ByteStreams;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonParser;
21 import org.apache.http.HttpEntity;
22 import org.apache.http.HttpHeaders;
23 import org.apache.http.client.methods.CloseableHttpResponse;
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.IpMappingProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
29 import org.onap.vfccatalog.api.VnfpackageApi;
30 import org.onap.vfccatalog.model.VnfPkgDetailInfo;
31 import org.slf4j.Logger;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.Conditional;
34 import org.springframework.stereotype.Component;
36 import java.io.IOException;
37 import java.io.InputStream;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
42 import static org.slf4j.LoggerFactory.getLogger;
43 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
46 * Retrieves a package from VF-C
49 @Conditional(value = Conditions.UseForVfc.class)
50 public class VfcPackageProvider implements IPackageProvider {
51 private static Logger logger = getLogger(VfcPackageProvider.class);
52 private final VfcRestApiProvider restApiProvider;
53 private final IpMappingProvider ipMappingProvider;
56 VfcPackageProvider(VfcRestApiProvider restApiProvider, IpMappingProvider ipMappingProvider) {
57 this.restApiProvider = restApiProvider;
58 this.ipMappingProvider = ipMappingProvider;
62 public String getCbamVnfdId(String csarId) {
63 VnfPkgDetailInfo vnfPackageDetails;
65 VnfpackageApi onapCatalogApi = restApiProvider.getVfcCatalogApi();
66 vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId).execute().body();
67 } catch (Exception e) {
68 throw buildFatalFailure(logger, "Unable to query VNF package with " + csarId, e);
70 JsonElement vnfdModel = new JsonParser().parse(vnfPackageDetails.getPackageInfo().getVnfdModel());
71 return vnfdModel.getAsJsonObject().get("metadata").getAsJsonObject().get("resourceVendorModelNumber").getAsString();
75 public byte[] getPackage(String csarId) {
78 VnfpackageApi onapCatalogApi = restApiProvider.getVfcCatalogApi();
79 VnfPkgDetailInfo vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId).execute().body();
80 String urlFromVfc = vnfPackageDetails.getPackageInfo().getDownloadUrl();
81 String host = new URL(urlFromVfc).getHost();
82 downloadUrl = urlFromVfc.replaceFirst("://" + host, "://" + ipMappingProvider.mapPrivateIpToPublicIp(host));
83 } catch (Exception e) {
84 throw buildFatalFailure(logger, "Unable to query VNF package with " + csarId, e);
87 return downloadCbamVnfPackage(downloadUrl);
88 } catch (Exception e) {
89 throw buildFatalFailure(logger, "Unable to download package from " + downloadUrl, e);
93 private byte[] downloadCbamVnfPackage(String downloadUri) throws IOException {
94 CloseableHttpClient client = systemFunctions().getHttpClient();
95 HttpGet httpget = new HttpGet(downloadUri);
96 httpget.setHeader(HttpHeaders.ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
97 CloseableHttpResponse response = client.execute(httpget);
98 HttpEntity entity = response.getEntity();
99 InputStream is = entity.getContent();
100 byte[] bytes = ByteStreams.toByteArray(is);