e6e060c0cee42078ad6fea96e90c288dfd300f97
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / InstantiateVnfContinueRunnable.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.Executors;
24
25 import org.apache.http.client.ClientProtocolException;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.zeroturnaround.zip.ZipUtil;
46
47
48 public class InstantiateVnfContinueRunnable implements Runnable {
49         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
50         private CbamMgmrInf cbamMgmr;
51         private CatalogMgmrInf catalogMgmr;
52         private NslcmMgmrInf nslcmMgmr;
53         
54         private InstantiateVnfRequest driverRequest;
55         private String vnfInstanceId;
56         private String jobId;
57         
58         private VnfmJobExecutionRepository jobDbMgmr;
59         
60         private Driver2CbamRequestConverter requestConverter;
61         
62         public InstantiateVnfContinueRunnable(InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
63                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
64         {
65                 this.driverRequest = driverRequest;
66                 this.vnfInstanceId = vnfInstanceId;
67                 this.jobId = jobId;
68                 this.nslcmMgmr = nslcmMgmr; 
69                 this.catalogMgmr = catalogMgmr;
70                 this.cbamMgmr = cbamMgmr;
71                 this.requestConverter = requestConverter;
72                 this.jobDbMgmr = dbManager;
73         }
74         
75         public void run() {
76                 try {
77                         //step 1 handle vnf package
78                         handleVnfPackage();
79                         
80                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
81                         NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest);
82                         handleNslcmGrantResponse(grantResponse);
83                         
84                         
85                         //step 5: instantiate vnf
86                         CBAMInstantiateVnfRequest  instantiateReq = requestConverter.InstantiateReqConvert(driverRequest, grantResponse, null, null);
87                         CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
88                         handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
89                         
90                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
91                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
92                         
93                 } catch (ClientProtocolException e) {
94                         logger.error("InstantiateVnfContinueRunnable run error ClientProtocolException", e);
95                 } catch (IOException e) {
96                         logger.error("InstantiateVnfContinueRunnable run error IOException", e);
97                 }
98                 
99         }
100
101         private void handleVnfPackage() {
102                 Executors.newSingleThreadExecutor().execute(new Runnable() {
103                         @Override
104                         public void run() {
105                                 try {
106                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
107                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
108                                         String packageUrl = vnfPackageInfo.getDownloadUri();
109                                         String saveDir = "/service/vnfPackage";
110                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
111                                         Process process = Runtime.getRuntime().exec("mkdir " + saveDir);
112                                         process.waitFor();
113                                         
114                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
115                                                 //extract package
116                                                 ZipUtil.unpack(new File(saveDir + "/" + packageFileName), new File(saveDir));
117                                                 //upload package
118                                                 String cbamPackageDirName = saveDir + "/"
119                                                                 + packageFileName.substring(0, packageFileName.length() - 4) + "/Artifacts";
120                                                 String cbamPackageName = new File(cbamPackageDirName).list()[0];
121                                                 cbamMgmr.uploadVnfPackage(cbamPackageName);
122                                         } 
123                                 } catch (Exception e) {
124                                         logger.error("Error to handleVnfPackage", e);
125                                 }
126                         }
127                         
128                 });
129         }
130         
131         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
132                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
133                 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
134                 {
135                         request.setStatus(CommonEnum.status.start);
136                 }
137                 else
138                 {
139                         request.setStatus(CommonEnum.status.result);
140                         
141                         //TODO the following are for the result
142 //                      request.setAffectedVnfc(affectedVnfc);
143 //                      request.setAffectedVI(affectedVI);
144 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
145                 }
146                 
147                 request.setVnfInstanceId(vnfInstanceId);
148                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
149                 request.setJobId(jobId);
150                 return request;
151         }
152
153         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
154                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
155                 
156                 request.setVnfInstanceId(vnfInstanceId);
157                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
158                 request.setJobId(jobId);
159                 
160                 ResourceDefinition resource = getFreeVnfResource();
161                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
162                 resourceList.add(resource);
163                 request.setAddResource(resourceList);
164                 
165                 return request;
166         }
167
168         private ResourceDefinition getFreeVnfResource() {
169                 // TODO Auto-generated method stub
170                 return null;
171         }
172
173         private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
174                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
175                 
176                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
177                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
178                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
179                 }
180                 jobDbMgmr.save(jobInfo);
181         }
182
183         private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
184                 // TODO Auto-generated method stub
185                 
186         }
187
188 }