b3c8bfe144c200e23b85a3fa9861b604714d5a71
[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.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.Executors;
23
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.OperationExecution;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.gson.Gson;
51
52
53 public class InstantiateVnfContinueRunnable implements Runnable {
54         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
55         private CbamMgmrInf cbamMgmr;
56         private CatalogMgmrInf catalogMgmr;
57         private NslcmMgmrInf nslcmMgmr;
58         
59         private InstantiateVnfRequest driverRequest;
60         private String vnfInstanceId;
61         private String jobId;
62         private String vnfmId;
63         
64         private VnfmJobExecutionRepository jobDbMgmr;
65         
66         private Driver2CbamRequestConverter requestConverter;
67         
68         private Gson gson = new Gson();
69         
70         public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
71                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
72         {
73                 this.driverRequest = driverRequest;
74                 this.vnfInstanceId = vnfInstanceId;
75                 this.jobId = jobId;
76                 this.nslcmMgmr = nslcmMgmr; 
77                 this.catalogMgmr = catalogMgmr;
78                 this.cbamMgmr = cbamMgmr;
79                 this.requestConverter = requestConverter;
80                 this.jobDbMgmr = dbManager;
81                 this.vnfmId = vnfmId;
82         }
83         
84         public void run() {
85                 //step 1 handle vnf package
86                 handleVnfPackage();
87                 
88                 handleGrant();
89                 
90                 handleModify();
91                 try {
92                         //step 5: instantiate vnf
93                         CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
94                         
95                         handleNotify(cbamInstantiateResponse.getId());
96                 } catch (Exception e) {
97                         logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
98                 }
99         }
100
101         private void handleNotify(String execId) {
102                 boolean instantiateFinished = false;
103                 
104                 do {
105                         try {
106                                 logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
107                                 CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
108                                 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
109                                 {
110                                         instantiateFinished = true;
111                                         handleCbamInstantiateResponse(exeResponse, jobId);
112                                         OperateTaskProgress.stopInstantiateTimerTask();
113                                         if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
114                                         {
115                                                 
116                                                 logger.info("Start to get vnfc resource");
117                                                 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
118                                                                 
119                                                 try {
120                                                         vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
121                                                 } catch (Exception e) {
122                                                         logger.error("Error to queryVnfcResource.", e);
123                                                 }
124                                                 
125                                                 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
126                                                 logger.info("End to get vnfc resource");
127                                                 
128                                                 if(vnfcResources == null)
129                                                 {
130                                                         vnfcResources = new ArrayList<>();
131                                                 }
132                                                 logger.info("Start to notify LCM the instantiation result");
133                                                 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
134                                                 
135                                                 OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
136                                                 
137                                                 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
138                                                 logger.info("End to notify LCM the instantiation result");
139                                         }
140                                 }
141                                 else {
142                                         Thread.sleep(60000);
143                                 }
144                                 
145                         } catch (Exception e) {
146                                 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
147                         }
148                 } while(!instantiateFinished);
149                 
150         }
151
152         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
153                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
154                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
155                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
156                 return cbamInstantiateResponse;
157         }
158
159         private void handleModify() {
160                 try {
161                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
162                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
163                 } catch (Exception e) {
164                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
165                 }
166         }
167
168         private void handleGrant(){
169                 try {
170                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
171                         nslcmMgmr.grantVnf(grantRequest);
172                 } catch (Exception e) {
173                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
174                 }
175         }
176
177         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
178                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
179                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
180                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
181                 
182                 return req;
183         }
184
185         private void handleVnfPackage() {
186                 Executors.newSingleThreadExecutor().execute(new Runnable() {
187                         @Override
188                         public void run() {
189                                 try {
190                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
191                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
192                                         String packageUrl = vnfPackageInfo.getDownloadUri();
193                                         String saveDir = "/service/vnfPackage";
194                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
195                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
196                                         process.waitFor();
197                                         
198                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
199                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
200 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
201 //                                              //extract package
202 //                                              ZipUtil.explode(csarFile);
203 //                                              csarFile.delete();
204                                         }
205                                 } catch (Exception e) {
206                                         logger.error("Error to handleVnfPackage from SDC", e);
207                                 }
208                                 
209                         }
210                         
211                 });
212         }
213         
214         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
215                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
216             request.setStatus(CommonEnum.status.result);
217                 request.setVnfInstanceId(vnfInstanceId);
218                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
219                 request.setJobId(jobId);
220                 
221                 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
222                 request.setAffectedVnfc(affectedVnfcs);
223                 return request;
224         }
225
226         private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
227                 List<AffectedVnfc> vnfcs = new ArrayList<>();
228                 for(VnfcResourceInfo resource : vnfcResources)
229                 {
230                         if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
231                         {
232                                 AffectedVnfc vnfc = new AffectedVnfc();
233                                 vnfc.setVnfcInstanceId(resource.getId());
234                                 vnfc.setVduId(resource.getVduId());
235                                 vnfc.setVimid(resource.getComputeResource().getVimId());
236                                 vnfc.setVmid(resource.getComputeResource().getResourceId());
237                                 
238                                 vnfcs.add(vnfc);
239                         }
240                 }
241                 return vnfcs;
242         }
243
244         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
245                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
246                 
247                 request.setVnfInstanceId(vnfInstanceId);
248                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
249                 request.setJobId(jobId);
250                 
251                 ResourceDefinition resource = getFreeVnfResource();
252                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
253                 resourceList.add(resource);
254                 request.setAddResource(resourceList);
255                 
256                 return request;
257         }
258
259         private ResourceDefinition getFreeVnfResource() {
260                 ResourceDefinition def = new ResourceDefinition();
261                 def.setVnfInstanceId(vnfInstanceId);
262                 def.setVimId("001");
263                 List<AddResource> resources = new ArrayList<>();
264                 AddResource res = new AddResource();
265                 res.setVdu("1");
266                 res.setType("vdu");
267                 res.setResourceDefinitionId(2);
268                 resources.add(res);
269                 def.setAddResource(resources);
270                 return def;
271         }
272
273         private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
274                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
275                 
276                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
277                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
278                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
279 //                      jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
280                 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
281                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
282                 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
283                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
284                 }
285                 else{
286                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
287                 }
288                         
289                 jobDbMgmr.save(jobInfo);
290         }
291
292 }