update get job status part
[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.inf.CbamMgmrInf;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
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.NslcmNotifyLCMEventsRequest;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
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
46 import com.google.gson.Gson;
47
48
49 public class InstantiateVnfContinueRunnable implements Runnable {
50         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
51         private CbamMgmrInf cbamMgmr;
52         private CatalogMgmrInf catalogMgmr;
53         private NslcmMgmrInf nslcmMgmr;
54         
55         private InstantiateVnfRequest driverRequest;
56         private String vnfInstanceId;
57         private String jobId;
58         private String vnfmId;
59         
60         private VnfmJobExecutionRepository jobDbMgmr;
61         
62         private Driver2CbamRequestConverter requestConverter;
63         
64         private Gson gson = new Gson();
65         
66         public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
67                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
68         {
69                 this.driverRequest = driverRequest;
70                 this.vnfInstanceId = vnfInstanceId;
71                 this.jobId = jobId;
72                 this.nslcmMgmr = nslcmMgmr; 
73                 this.catalogMgmr = catalogMgmr;
74                 this.cbamMgmr = cbamMgmr;
75                 this.requestConverter = requestConverter;
76                 this.jobDbMgmr = dbManager;
77                 this.vnfmId = vnfmId;
78         }
79         
80         public void run() {
81                 //step 1 handle vnf package
82                 handleVnfPackage();
83                 
84                 handleGrant();
85                 
86                 handleModify();
87                 try {
88                         //step 5: instantiate vnf
89                         CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
90                         
91                         handleNotify(cbamInstantiateResponse);
92                 } catch (Exception e) {
93                         logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
94                 }
95         }
96
97         private void handleNotify(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
98                 try {
99                         logger.info("Start to notify LCM the instantiation result");
100                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
101                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
102                         logger.info("End to notify LCM the instantiation result");
103                 } catch (Exception e) {
104                         logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
105                 }
106         }
107
108         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
109                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
110                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
111                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
112                 return cbamInstantiateResponse;
113         }
114
115         private void handleModify() {
116                 try {
117                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
118                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
119                 } catch (Exception e) {
120                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
121                 }
122         }
123
124         private void handleGrant(){
125                 try {
126                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
127                         nslcmMgmr.grantVnf(grantRequest);
128                 } catch (Exception e) {
129                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
130                 }
131         }
132
133         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
134                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
135                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
136                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
137                 
138                 return req;
139         }
140
141         private void handleVnfPackage() {
142                 Executors.newSingleThreadExecutor().execute(new Runnable() {
143                         @Override
144                         public void run() {
145                                 try {
146                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
147                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
148                                         String packageUrl = vnfPackageInfo.getDownloadUri();
149                                         String saveDir = "/service/vnfPackage";
150                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
151                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
152                                         process.waitFor();
153                                         
154                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
155                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
156 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
157 //                                              //extract package
158 //                                              ZipUtil.explode(csarFile);
159 //                                              csarFile.delete();
160                                         }
161                                 } catch (Exception e) {
162                                         logger.error("Error to handleVnfPackage from SDC", e);
163                                 }
164                                 
165                         }
166                         
167                 });
168         }
169         
170         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
171                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
172                 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
173                 {
174                         request.setStatus(CommonEnum.status.start);
175                 }
176                 else
177                 {
178                         request.setStatus(CommonEnum.status.result);
179                         
180                         //TODO the following are for the result
181 //                      request.setAffectedVnfc(affectedVnfc);
182 //                      request.setAffectedVI(affectedVI);
183 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
184                 }
185                 
186                 request.setVnfInstanceId(vnfInstanceId);
187                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
188                 request.setJobId(jobId);
189                 return request;
190         }
191
192         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
193                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
194                 
195                 request.setVnfInstanceId(vnfInstanceId);
196                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
197                 request.setJobId(jobId);
198                 
199                 ResourceDefinition resource = getFreeVnfResource();
200                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
201                 resourceList.add(resource);
202                 request.setAddResource(resourceList);
203                 
204                 return request;
205         }
206
207         private ResourceDefinition getFreeVnfResource() {
208                 ResourceDefinition def = new ResourceDefinition();
209                 def.setVnfInstanceId(vnfInstanceId);
210                 def.setVimId("001");
211                 List<AddResource> resources = new ArrayList<>();
212                 AddResource res = new AddResource();
213                 res.setVdu("1");
214                 res.setType("vdu");
215                 res.setResourceDefinitionId(2);
216                 resources.add(res);
217                 def.setAddResource(resources);
218                 return def;
219         }
220
221         private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
222                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
223                 
224                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
225                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
226                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
227                 }
228                 jobDbMgmr.save(jobInfo);
229         }
230
231 }