20c8bf503d995b8e864f942e84c2699f8aeb4550
[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                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
100                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
101                 } catch (Exception e) {
102                         logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
103                 }
104         }
105
106         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
107                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
108                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
109                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
110                 return cbamInstantiateResponse;
111         }
112
113         private void handleModify() {
114                 try {
115                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
116                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
117                 } catch (Exception e) {
118                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
119                 }
120         }
121
122         private void handleGrant(){
123                 try {
124                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
125                         nslcmMgmr.grantVnf(grantRequest);
126                 } catch (Exception e) {
127                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
128                 }
129         }
130
131         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
132                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
133                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
134                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
135                 
136                 return req;
137         }
138
139         private void handleVnfPackage() {
140                 Executors.newSingleThreadExecutor().execute(new Runnable() {
141                         @Override
142                         public void run() {
143                                 try {
144                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
145                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
146                                         String packageUrl = vnfPackageInfo.getDownloadUri();
147                                         String saveDir = "/service/vnfPackage";
148                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
149                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
150                                         process.waitFor();
151                                         
152                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
153                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
154 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
155 //                                              //extract package
156 //                                              ZipUtil.explode(csarFile);
157 //                                              csarFile.delete();
158                                         }
159                                 } catch (Exception e) {
160                                         logger.error("Error to handleVnfPackage from SDC", e);
161                                 }
162                                 
163                         }
164                         
165                 });
166         }
167         
168         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
169                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
170                 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
171                 {
172                         request.setStatus(CommonEnum.status.start);
173                 }
174                 else
175                 {
176                         request.setStatus(CommonEnum.status.result);
177                         
178                         //TODO the following are for the result
179 //                      request.setAffectedVnfc(affectedVnfc);
180 //                      request.setAffectedVI(affectedVI);
181 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
182                 }
183                 
184                 request.setVnfInstanceId(vnfInstanceId);
185                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
186                 request.setJobId(jobId);
187                 return request;
188         }
189
190         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
191                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
192                 
193                 request.setVnfInstanceId(vnfInstanceId);
194                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
195                 request.setJobId(jobId);
196                 
197                 ResourceDefinition resource = getFreeVnfResource();
198                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
199                 resourceList.add(resource);
200                 request.setAddResource(resourceList);
201                 
202                 return request;
203         }
204
205         private ResourceDefinition getFreeVnfResource() {
206                 ResourceDefinition def = new ResourceDefinition();
207                 def.setVnfInstanceId(vnfInstanceId);
208                 def.setVimId("001");
209                 List<AddResource> resources = new ArrayList<>();
210                 AddResource res = new AddResource();
211                 res.setVdu("1");
212                 res.setType("vdu");
213                 res.setResourceDefinitionId(2);
214                 resources.add(res);
215                 def.setAddResource(resources);
216                 return def;
217         }
218
219         private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
220                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
221                 
222                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
223                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
224                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
225                 }
226                 jobDbMgmr.save(jobInfo);
227         }
228
229 }