diff options
13 files changed, 222 insertions, 20 deletions
diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/pom.xml b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/pom.xml index 4f08fa0c..0a159f91 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/pom.xml +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/pom.xml @@ -132,6 +132,11 @@ <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> + <dependency> + <groupId>org.zeroturnaround</groupId> + <artifactId>zt-zip</artifactId> + <version>1.12</version> + </dependency> </dependencies> <build> diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/Driver2CbamRequestConverter.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/Driver2CbamRequestConverter.java index 28400ed0..f351925b 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/Driver2CbamRequestConverter.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/Driver2CbamRequestConverter.java @@ -49,7 +49,7 @@ public class Driver2CbamRequestConverter { public CBAMCreateVnfRequest createReqConvert(InstantiateVnfRequest driverRequest) { CBAMCreateVnfRequest request = new CBAMCreateVnfRequest(); - request.setVnfdId("vnfd_001"); + request.setVnfdId(driverRequest.getVnfDescriptorId()); request.setName(driverRequest.getVnfInstanceName()); request.setDescription(driverRequest.getVnfInstanceDescription()); return request; diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/InstantiateVnfContinueRunnable.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/InstantiateVnfContinueRunnable.java index e527b105..e6e060c0 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/InstantiateVnfContinueRunnable.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/InstantiateVnfContinueRunnable.java @@ -16,9 +16,11 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; import org.apache.http.client.ClientProtocolException; import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo; @@ -31,6 +33,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum; import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation; import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo; import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest; @@ -39,6 +42,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf; import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.zeroturnaround.zip.ZipUtil; public class InstantiateVnfContinueRunnable implements Runnable { @@ -70,12 +74,13 @@ public class InstantiateVnfContinueRunnable implements Runnable { public void run() { try { + //step 1 handle vnf package + handleVnfPackage(); + NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest(); NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest); handleNslcmGrantResponse(grantResponse); - //step 2: query vnfPackage uri - VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId()); //step 5: instantiate vnf CBAMInstantiateVnfRequest instantiateReq = requestConverter.InstantiateReqConvert(driverRequest, grantResponse, null, null); @@ -92,6 +97,36 @@ public class InstantiateVnfContinueRunnable implements Runnable { } } + + private void handleVnfPackage() { + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + try { + //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM + VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId()); + String packageUrl = vnfPackageInfo.getDownloadUri(); + String saveDir = "/service/vnfPackage"; + String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/")); + Process process = Runtime.getRuntime().exec("mkdir " + saveDir); + process.waitFor(); + + if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) { + //extract package + ZipUtil.unpack(new File(saveDir + "/" + packageFileName), new File(saveDir)); + //upload package + String cbamPackageDirName = saveDir + "/" + + packageFileName.substring(0, packageFileName.length() - 4) + "/Artifacts"; + String cbamPackageName = new File(cbamPackageDirName).list()[0]; + cbamMgmr.uploadVnfPackage(cbamPackageName); + } + } catch (Exception e) { + logger.error("Error to handleVnfPackage", e); + } + } + + }); + } private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) { NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest(); diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/VnfmDriverMgmrImpl.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/VnfmDriverMgmrImpl.java index 3e7457cb..79fb10f7 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/VnfmDriverMgmrImpl.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/adaptor/VnfmDriverMgmrImpl.java @@ -231,6 +231,9 @@ public class VnfmDriverMgmrImpl implements VnfmDriverMgmrInf{ String urlHead = systemInfo.getServiceUrl(); adaptorEnv.setCbamApiUriFront(urlHead); + adaptorEnv.setCbamUserName(systemInfo.getUserName()); + adaptorEnv.setCbamPassword(systemInfo.getPassword()); + return urlHead; } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/catalog/impl/CatalogMgmrImpl.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/catalog/impl/CatalogMgmrImpl.java index fd93ee41..4598fa59 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/catalog/impl/CatalogMgmrImpl.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/catalog/impl/CatalogMgmrImpl.java @@ -17,7 +17,6 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.impl; import java.io.IOException; -import java.util.HashMap; import org.apache.http.client.ClientProtocolException; import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.CatalogQueryVnfResponse; @@ -49,11 +48,8 @@ public class CatalogMgmrImpl implements CatalogMgmrInf{ public VnfPackageInfo queryVnfPackage(String vnfPackageId) throws ClientProtocolException, IOException { String url=adaptorEnv.getCatalogApiUriFront() + String.format(CommonConstants.RetrieveVnfPackagePath, vnfPackageId); - HashMap<String, String> map = new HashMap<>(); - String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret()); - - String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent(); + String responseStr = httpClientProcessor.process(url, RequestMethod.GET, null, null).getContent(); logger.info("CbamMgmrImpl -> queryVnfPackage, responseStr is " + responseStr); diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/impl/CbamMgmrImpl.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/impl/CbamMgmrImpl.java index bb226db9..6afa6c8d 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/impl/CbamMgmrImpl.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/impl/CbamMgmrImpl.java @@ -37,6 +37,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest; import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse; import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf; import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil; import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants; import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf; import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult; @@ -60,14 +61,14 @@ public class CbamMgmrImpl implements CbamMgmrInf { private String retrieveToken() throws ClientProtocolException, IOException, JSONException { String result = null; - String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath; + String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath; HashMap<String, String> map = new HashMap<>(); map.put(CommonConstants.ACCEPT, "*/*"); map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); - String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret()); + String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword()); - String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent(); + String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent(); logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr); @@ -252,5 +253,40 @@ public class CbamMgmrImpl implements CbamMgmrInf { public void setAdaptorEnv(AdaptorEnv adaptorEnv) { this.adaptorEnv = adaptorEnv; } + + @Override + public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException { + String httpPath = CommonConstants.CbamUploadVnfPackagePath; + RequestMethod method = RequestMethod.POST; + + HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method); + String responseStr = httpResult.getContent(); + + logger.info("CbamMgmrImpl -> uploadVnfPackage, responseStr is " + responseStr); + + int code = httpResult.getStatusCode(); + if(code == 200) { + logger.info("CbamMgmrImpl -> uploadVnfPackage, success" ); + logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful."); + }else { + logger.error("CbamMgmrImpl -> uploadVnfPackage, error" ); + } + } + + private HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException { + String token = null; + try { + token = retrieveToken(); + } catch (JSONException e) { + logger.error("retrieveTokenError ", e); + } + String url = adaptorEnv.getCbamApiUriFront() + httpPath; + + HashMap<String, String> map = new HashMap<>(); + map.put(CommonConstants.AUTHORIZATION, "bearer " + token); + map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=--fsgdsfgjgjdsgdfjgjgj"); + byte[] fileBytes = CommonUtil.getBytes(filePath); + return httpClientProcessor.processBytes(url, method, map, fileBytes); + } } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/inf/CbamMgmrInf.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/inf/CbamMgmrInf.java index 9c4bc6cc..d3bb970b 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/inf/CbamMgmrInf.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/inf/CbamMgmrInf.java @@ -53,4 +53,7 @@ public interface CbamMgmrInf { public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException; public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException; + + public void uploadVnfPackage(String cbamPackageName) throws ClientProtocolException, IOException; + }
\ No newline at end of file diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/bo/AdaptorEnv.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/bo/AdaptorEnv.java index e9a706c5..1a41765f 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/bo/AdaptorEnv.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/bo/AdaptorEnv.java @@ -56,6 +56,9 @@ public class AdaptorEnv { //cbamApiFront is from aai query private String cbamApiUriFront; + private String cbamUserName; + private String cbamPassword; + private String msbApiUriFront; @@ -221,5 +224,21 @@ public class AdaptorEnv { this.msbApiUriFront = msbApiUriFront; } + public String getCbamUserName() { + return cbamUserName; + } + + public void setCbamUserName(String cbamUserName) { + this.cbamUserName = cbamUserName; + } + + public String getCbamPassword() { + return cbamPassword; + } + + public void setCbamPassword(String cbamPassword) { + this.cbamPassword = cbamPassword; + } + } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java index 34530b51..c49d1f04 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java @@ -17,6 +17,7 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -78,4 +79,27 @@ public final class CommonUtil { } return path; } + + public static byte[] getBytes(String filePath){ + byte[] buffer = null; + try { + File file = new File(filePath); + FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); + byte[] b = new byte[1000]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + fis.close(); + bos.close(); + buffer = bos.toByteArray(); + } catch (FileNotFoundException e) { + logger.error("file " + filePath + " is not found.", e); + } catch (IOException e) { + logger.error("file " + filePath + " IOException.", e); + } + return buffer; + } + } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/constant/CommonConstants.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/constant/CommonConstants.java index 8115e767..b1059118 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/constant/CommonConstants.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/constant/CommonConstants.java @@ -30,7 +30,7 @@ public class CommonConstants { public static final String UTF_8 = "utf-8"; //AAI path get vnfm - // /external-system/esr-vnfm-list/esr-vnfm/{vnfm-id} + // /external-system/esr-vnfm-list/esr-vnfm/{vnfm-id}/esr-system-info-list public static final String RetrieveVnfmListPath = "/external-system/esr-vnfm-list/esr-vnfm/%s/esr-system-info-list"; //Nslcm path @@ -38,12 +38,12 @@ public class CommonConstants { public static final String NslcmNotifyPath = "/vnfs/%s/Notify"; //Catalog path - public static final String RetrieveVnfPackagePath = "/vnfpackages/%s"; + public static final String RetrieveVnfPackagePath = "/catalog/v1/vnfpackages/%s"; //CBAM -- Nokia VNFM path public static final String CBAM_TOKEN_KEY = "access_token"; - public static final String RetrieveCbamTokenPath="/auth/realms/cbam/protocol/openid-connect/token"; - public static final String RetrieveCbamTokenPostStr="grant_type=%s&client_id=%s&client_secret=%s&usernmae=%s&password=%s"; + public static final String CbamRetrieveTokenPath="/auth/realms/cbam/protocol/openid-connect/token"; + public static final String CbamRetrieveTokenPostStr="grant_type=password&client_id=%s&client_secret=%s&username=%s&password=%s"; public static final String CbamCreateVnfPath="/vnfs"; public static final String CbamInstantiateVnfPath="/vnfs/%s/instantiate"; public static final String CbamQueryVnfPath="/vnfs/%s"; @@ -53,6 +53,9 @@ public class CommonConstants { public static final String CbamScaleVnfPath = "/vnfs/%s/scale"; public static final String CbamHealVnfPath="/vnfs/%s/heal"; + public static final String CbamUploadVnfPackagePath="/api/catalog/vnfpackages"; + + public static final String NSLCM_OPERATION_INSTANTIATE = "Instantiate"; public static final String NSLCM_OPERATION_TERMINATE = "Terminal"; public static final String NSLCM_OPERATION_SCALE_OUT = "Scaleout"; @@ -66,7 +69,6 @@ public class CommonConstants { //MSB public static final String MSB_REGISTER_SERVICE_PATH = "/api/microservices/v1/services"; -// public static final String MSB_REGISTER_SERVICE_PATH = "/api/microservices/v1/services/{serviceName}/version/{version}/nodes/{ip}/{port}"; public static final String MSB_UNREGISTER_SERVICE_PATH = "/api/microservices/v1/services/%s/version/%s/nodes/%s/%s"; public static final String MSB_QUERY_SERVICE_PATH = "/api/microservices/v1/services/%s/version/%s"; } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorImpl.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorImpl.java index ca8e7e2e..114dea63 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorImpl.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorImpl.java @@ -16,17 +16,26 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.HashMap; import org.apache.http.client.ClientProtocolException; import org.apache.http.impl.client.HttpClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMethod; @Component public class HttpClientProcessorImpl implements HttpClientProcessorInf{ + private static final Logger logger = LoggerFactory.getLogger(HttpClientProcessorImpl.class); @Autowired private HttpClientBuilder httpClientBuilder; @@ -41,7 +50,7 @@ public class HttpClientProcessorImpl implements HttpClientProcessorInf{ processor.addHdeader(key, headerMap.get(key)); } - if(null != bodyString && bodyString.length() > 0) + if(null != bodyString && bodyString.length() > 0 && !bodyString.equalsIgnoreCase("null")) { processor.addPostEntity(bodyString); } @@ -49,8 +58,72 @@ public class HttpClientProcessorImpl implements HttpClientProcessorInf{ } return processor.process(url); } - + + public HttpResult processBytes(String url, RequestMethod methodType, HashMap<String, String> headerMap, byte[] byteArray) throws ClientProtocolException, IOException + { + HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType); + if(headerMap != null && !headerMap.isEmpty()) + { + for(String key : headerMap.keySet()) + { + processor.addHdeader(key, headerMap.get(key)); + } + + if(null != byteArray && byteArray.length > 0) + { + processor.addBytesPostEntity(byteArray); + } + + } + return processor.process(url); + } + public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) { this.httpClientBuilder = httpClientBuilder; } + + public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath) + { + try + { + URL url = new URL(urlStr); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + conn.setConnectTimeout(10*1000); + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + InputStream inputStream = conn.getInputStream(); + byte[] getData = readInputStream(inputStream); + File saveDir = new File(savePath); + if(!saveDir.exists()){ + saveDir.mkdir(); + } + File file = new File(saveDir+File.separator+fileName); + FileOutputStream fos = new FileOutputStream(file); + fos.write(getData); + if(fos!=null){ + fos.close(); + } + if(inputStream!=null){ + inputStream.close(); + } + } + catch ( IOException e ) { + logger.info("write file fail", e); + return false; + } + + logger.info("info: "+ urlStr + " download success"); + return true; + } + + + public static byte[] readInputStream(InputStream inputStream) throws IOException { + byte[] buffer = new byte[1024]; + int len = 0; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + bos.close(); + return bos.toByteArray(); + } } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorInf.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorInf.java index cbb820b8..ffcb68b5 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorInf.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpClientProcessorInf.java @@ -23,6 +23,8 @@ import org.apache.http.client.ClientProtocolException; import org.springframework.web.bind.annotation.RequestMethod; public interface HttpClientProcessorInf { - public HttpResult process(String url, RequestMethod methodType, HashMap<String, String> headerMap, String bodyString) throws ClientProtocolException, IOException; + public HttpResult process(String url, RequestMethod methodType, HashMap<String, String> headerMap, String bodyString) throws ClientProtocolException, IOException; + + public HttpResult processBytes(String url, RequestMethod method, HashMap<String, String> map, byte[] fileBytes) throws ClientProtocolException, IOException; } diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpRequestProcessor.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpRequestProcessor.java index 225a228a..363b5a92 100644 --- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpRequestProcessor.java +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/http/client/HttpRequestProcessor.java @@ -25,6 +25,7 @@ import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; @@ -76,6 +77,9 @@ public class HttpRequestProcessor { public void addPostEntity(String bodyStr) { ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8)); - + } + + public void addBytesPostEntity(byte[] byteArray) { + ((HttpPost)httpRequest).setEntity(new ByteArrayEntity(byteArray)); } } |