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.fatalFailure;
 
  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) {
 
  64             VnfpackageApi onapCatalogApi = restApiProvider.getOnapCatalogApi();
 
  65             VnfPkgDetailInfo vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId);
 
  66             JsonElement vnfdModel = new JsonParser().parse(vnfPackageDetails.getPackageInfo().getVnfdModel());
 
  67             return vnfdModel.getAsJsonObject().get("metadata").getAsJsonObject().get("resourceVendorModelNumber").getAsString();
 
  68         } catch (Exception e) {
 
  69             throw fatalFailure(logger, "Unable to query VNF package with " + csarId, e);
 
  74     public byte[] getPackage(String csarId) {
 
  77             VnfpackageApi onapCatalogApi = restApiProvider.getOnapCatalogApi();
 
  78             VnfPkgDetailInfo vnfPackageDetails = onapCatalogApi.queryVnfPackage(csarId);
 
  79             downloadUrl = vnfPackageDetails.getPackageInfo().getDownloadUrl();
 
  80             String host = new URL(downloadUrl).getHost();
 
  81             if (!ipMappingProvider.mapPrivateIpToPublicIp(host).equals(host)) {
 
  82                 downloadUrl = downloadUrl.replaceFirst("://" + host, "://" + ipMappingProvider.mapPrivateIpToPublicIp(host));
 
  84         } catch (Exception e) {
 
  85             throw fatalFailure(logger, "Unable to query VNF package with " + csarId, e);
 
  88             return downloadCbamVnfPackage(downloadUrl);
 
  89         } catch (Exception e) {
 
  90             throw fatalFailure(logger, "Unable to download package from " + downloadUrl, e);
 
  94     private byte[] downloadCbamVnfPackage(String downloadUri) throws IOException {
 
  95         CloseableHttpClient client = systemFunctions().getHttpClient();
 
  96         HttpGet httpget = new HttpGet(downloadUri);
 
  97         httpget.setHeader(HttpHeaders.ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
 
  98         CloseableHttpResponse response = client.execute(httpget);
 
  99         HttpEntity entity = response.getEntity();
 
 100         InputStream is = entity.getContent();
 
 101         byte[] bytes = ByteStreams.toByteArray(is);