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