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.ByteArrayOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
43 import static org.slf4j.LoggerFactory.getLogger;
44 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
47 * Retrieves a package from VF-C
50 @Conditional(value = Conditions.UseForVfc.class)
51 public class VfcPackageProvider implements IPackageProvider {
52 private static Logger logger = getLogger(VfcPackageProvider.class);
53 private final VfcRestApiProvider restApiProvider;
54 private final IpMappingProvider ipMappingProvider;
57 VfcPackageProvider(VfcRestApiProvider restApiProvider, IpMappingProvider ipMappingProvider) {
58 this.restApiProvider = restApiProvider;
59 this.ipMappingProvider = ipMappingProvider;
63 public String getCbamVnfdId(String csarId) {
65 VnfpackageApi onapCatalogApi = restApiProvider.getOnapCatalogApi();
66 VnfPkgDetailInfo vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId);
67 JsonElement vnfdModel = new JsonParser().parse(vnfPackageDetails.getPackageInfo().getVnfdModel());
68 return vnfdModel.getAsJsonObject().get("metadata").getAsJsonObject().get("resourceVendorModelNumber").getAsString();
69 } catch (Exception e) {
70 throw fatalFailure(logger, "Unable to query VNF package with " + csarId + " from VF-C", e);
75 public byte[] getPackage(String csarId) {
78 VnfpackageApi onapCatalogApi = restApiProvider.getOnapCatalogApi();
79 VnfPkgDetailInfo vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId);
80 downloadUrl = vnfPackageDetails.getPackageInfo().getDownloadUrl();
81 String host = new URL(downloadUrl).getHost();
82 if (!ipMappingProvider.mapPrivateIpToPublicIp(host).equals(host)) {
83 downloadUrl = downloadUrl.replaceFirst("://" + host, "://" + ipMappingProvider.mapPrivateIpToPublicIp(host));
85 } catch (Exception e) {
86 throw fatalFailure(logger, "Unable to query VNF package with " + csarId + " from VF-C", e);
89 return downloadCbamVnfPackage(downloadUrl);
90 } catch (Exception e) {
91 throw fatalFailure(logger, "Unable to download package from " + downloadUrl + " from VF-C", e);
95 private byte[] downloadCbamVnfPackage(String downloadUri) throws IOException {
96 CloseableHttpClient client = systemFunctions().getHttpClient();
97 HttpGet httpget = new HttpGet(downloadUri);
98 httpget.setHeader(HttpHeaders.ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
99 CloseableHttpResponse response = client.execute(httpget);
100 HttpEntity entity = response.getEntity();
101 InputStream is = entity.getContent();
102 ByteArrayOutputStream cbamInZip = new ByteArrayOutputStream();
103 byte[] bytes = ByteStreams.toByteArray(is);