Merge "Method handling nokia-vnfmdriver"
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / HealVnfContinueRunnable.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.util.ArrayList;
20 import java.util.List;
21
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38
39 public class HealVnfContinueRunnable implements Runnable {
40         private static final Logger logger = LoggerFactory.getLogger(HealVnfContinueRunnable.class);
41
42         @Autowired
43         private CbamMgmrInf cbamMgmr;
44         @Autowired
45         private NslcmMgmrInf nslcmMgmr;
46         
47         private HealVnfRequest driverRequest;
48         private String vnfInstanceId;
49         private String jobId;
50         private String vnfmId;
51         @Autowired
52         private VnfmJobExecutionMapper jobDbMgmr;
53         
54         private Driver2CbamRequestConverter requestConverter;
55         
56         
57         public HealVnfContinueRunnable(String inVnfmId, HealVnfRequest driverRequest, String vnfInstanceId, String jobId,
58                         NslcmMgmrInf nslcmMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionMapper dbManager)
59         {
60                 this.driverRequest = driverRequest;
61                 this.vnfInstanceId = vnfInstanceId;
62                 this.nslcmMgmr = nslcmMgmr; 
63                 this.cbamMgmr = cbamMgmr;
64                 this.requestConverter = requestConverter;
65                 this.jobId = jobId;
66                 this.jobDbMgmr = dbManager;
67                 this.vnfmId = inVnfmId;
68         }
69         
70         private void handleGrant(){
71                 try {
72                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
73                         nslcmMgmr.grantVnf(grantRequest);
74                 } catch (Exception e) {
75                         logger.error("HealVnfContinueRunnable --> handleGrant error.", e);
76                 }
77         }
78         
79         public void run() {
80                 handleGrant();
81                 handleHeal();
82         }
83         
84         
85
86         private CBAMHealVnfResponse handleHeal() {
87                 CBAMHealVnfResponse cbamResponse = null;
88                 try {
89                         CBAMHealVnfRequest  modifyReq = requestConverter.healReqConvert();
90                         cbamResponse = cbamMgmr.healVnf(modifyReq, vnfInstanceId);
91                         handleCbamHealResponse(cbamResponse, jobId);
92                 } catch (Exception e) {
93                         logger.error("HealVnfContinueRunnable --> handleHeal error.", e);
94                 }
95                 
96                 return cbamResponse;
97         }
98
99         private void handleCbamHealResponse(CBAMHealVnfResponse cbamResponse, String jobId) {
100                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
101                 
102                 jobInfo.setVnfmExecutionId(cbamResponse.getId());
103                 if(CommonEnum.OperationStatus.FAILED == cbamResponse.getStatus()) {
104                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
105                 }
106                 else {
107                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
108                 }
109                 jobDbMgmr.update(jobInfo);
110         }
111         
112         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
113                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
114                 
115                 request.setVnfInstanceId(vnfInstanceId);
116                 request.setLifecycleOperation(LifecycleOperation.Heal);
117                 request.setJobId(jobId);
118                 
119                 ResourceDefinition resource = getFreeVnfResource();
120                 List<ResourceDefinition> resourceList = new ArrayList<>();
121                 resourceList.add(resource);
122                 request.setRemoveResource(resourceList);
123                 
124                 return request;
125         }
126         
127         private ResourceDefinition getFreeVnfResource() {
128                 ResourceDefinition def = new ResourceDefinition();
129                 def.setVnfInstanceId(vnfInstanceId);
130                 def.setVimId("001");
131                 List<AddResource> resources = new ArrayList<>();
132                 AddResource res = new AddResource();
133                 res.setVdu("1");
134                 res.setType("vdu");
135                 res.setResourceDefinitionId(2);
136                 resources.add(res);
137                 def.setAddResource(resources);
138                 return def;
139         }
140
141         public void setDriverRequest(HealVnfRequest driverRequest) {
142                 this.driverRequest = driverRequest;
143         }
144
145         public void setVnfInstanceId(String vnfInstanceId) {
146                 this.vnfInstanceId = vnfInstanceId;
147         }
148
149         public void setJobId(String jobId) {
150                 this.jobId = jobId;
151         }
152
153         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
154                 this.requestConverter = requestConverter;
155         }
156
157         public void setVnfmId(String inVnfmId) {
158                 this.vnfmId = inVnfmId;
159         }
160 }