<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>
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;
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;
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;
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 {
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);
}
}
+
+ 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();
String urlHead = systemInfo.getServiceUrl();
adaptorEnv.setCbamApiUriFront(urlHead);
+ adaptorEnv.setCbamUserName(systemInfo.getUserName());
+ adaptorEnv.setCbamPassword(systemInfo.getPassword());
+
return urlHead;
}
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;
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);
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;
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);
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);
+ }
}
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
//cbamApiFront is from aai query
private String cbamApiUriFront;
+ private String cbamUserName;
+ private String cbamPassword;
+
private String msbApiUriFront;
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;
+ }
+
}
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;
}
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;
+ }
+
}
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
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";
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";
//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";
}
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;
processor.addHdeader(key, headerMap.get(key));
}
- if(null != bodyString && bodyString.length() > 0)
+ if(null != bodyString && bodyString.length() > 0 && !bodyString.equalsIgnoreCase("null"))
{
processor.addPostEntity(bodyString);
}
}
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();
+ }
}
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;
}
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;
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));
}
}