Sonar fix too many method param
[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  * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 public class HealVnfContinueRunnable implements Runnable {
41         private static final Logger logger = LoggerFactory.getLogger(HealVnfContinueRunnable.class);
42
43         @Autowired
44         private CbamMgmrInf cbamMgmr;
45         @Autowired
46         private NslcmMgmrInf nslcmMgmr;
47         
48         private HealVnfRequest driverRequest;
49         private String vnfInstanceId;
50         private String jobId;
51         private String vnfmId;
52         @Autowired
53         private VnfmJobExecutionMapper jobDbMgmr;
54         
55         private Driver2CbamRequestConverter requestConverter;
56
57         ///
58
59         public static class HealVnfContinueRunnableBuilder {
60                 private String inVnfmId;
61                 private HealVnfRequest driverRequest;
62                 private String vnfInstanceId;
63                 private String jobId;
64                 private NslcmMgmrInf nslcmMgmr;
65                 private CbamMgmrInf cbamMgmr;
66                 private Driver2CbamRequestConverter requestConverter;
67                 private VnfmJobExecutionMapper dbManager;
68
69                 public HealVnfContinueRunnableBuilder setInVnfmId(String inVnfmId) {
70                         this.inVnfmId = inVnfmId;
71                         return this;
72                 }
73
74                 public HealVnfContinueRunnableBuilder setDriverRequest(HealVnfRequest driverRequest) {
75                         this.driverRequest = driverRequest;
76                         return this;
77                 }
78
79                 public HealVnfContinueRunnableBuilder setVnfInstanceId(String vnfInstanceId) {
80                         this.vnfInstanceId = vnfInstanceId;
81                         return this;
82                 }
83
84                 public HealVnfContinueRunnableBuilder setJobId(String jobId) {
85                         this.jobId = jobId;
86                         return this;
87                 }
88
89                 public HealVnfContinueRunnableBuilder setNslcmMgmr(NslcmMgmrInf nslcmMgmr) {
90                         this.nslcmMgmr = nslcmMgmr;
91                         return this;
92                 }
93
94                 public HealVnfContinueRunnableBuilder setCbamMgmr(CbamMgmrInf cbamMgmr) {
95                         this.cbamMgmr = cbamMgmr;
96                         return this;
97                 }
98
99                 public HealVnfContinueRunnableBuilder setRequestConverter(Driver2CbamRequestConverter requestConverter) {
100                         this.requestConverter = requestConverter;
101                         return this;
102                 }
103
104                 public HealVnfContinueRunnableBuilder setDbManager(VnfmJobExecutionMapper dbManager) {
105                         this.dbManager = dbManager;
106                         return this;
107                 }
108
109                 public HealVnfContinueRunnable build() {
110                         return new HealVnfContinueRunnable(this);
111                 }
112         }
113
114         ///
115         
116         
117         public HealVnfContinueRunnable(HealVnfContinueRunnableBuilder builder) {
118             this.driverRequest = builder.driverRequest;
119         this.vnfInstanceId = builder.vnfInstanceId;
120         this.nslcmMgmr = builder.nslcmMgmr; 
121         this.cbamMgmr = builder.cbamMgmr;
122         this.requestConverter = builder.requestConverter;
123         this.jobId = builder.jobId;
124         this.jobDbMgmr = builder.dbManager;
125         this.vnfmId = builder.inVnfmId;
126         }
127
128     private void handleGrant(){
129                 try {
130                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
131                         nslcmMgmr.grantVnf(grantRequest);
132                 } catch (Exception e) {
133                         logger.error("HealVnfContinueRunnable --> handleGrant error.", e);
134                 }
135         }
136         
137         public void run() {
138                 handleGrant();
139                 handleHeal();
140         }
141         
142         
143
144         private CBAMHealVnfResponse handleHeal() {
145                 CBAMHealVnfResponse cbamResponse = null;
146                 try {
147                         CBAMHealVnfRequest  modifyReq = requestConverter.healReqConvert();
148                         cbamResponse = cbamMgmr.healVnf(modifyReq, vnfInstanceId);
149                         handleCbamHealResponse(cbamResponse, jobId);
150                 } catch (Exception e) {
151                         logger.error("HealVnfContinueRunnable --> handleHeal error.", e);
152                 }
153                 
154                 return cbamResponse;
155         }
156
157         private void handleCbamHealResponse(CBAMHealVnfResponse cbamResponse, String jobId) {
158                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
159                 
160                 jobInfo.setVnfmExecutionId(cbamResponse.getId());
161                 if(CommonEnum.OperationStatus.FAILED == cbamResponse.getStatus()) {
162                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
163                 }
164                 else {
165                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
166                 }
167                 jobDbMgmr.update(jobInfo);
168         }
169         
170         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
171                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
172                 
173                 request.setVnfInstanceId(vnfInstanceId);
174                 request.setLifecycleOperation(LifecycleOperation.Heal);
175                 request.setJobId(jobId);
176                 
177                 ResourceDefinition resource = getFreeVnfResource();
178                 List<ResourceDefinition> resourceList = new ArrayList<>();
179                 resourceList.add(resource);
180                 request.setRemoveResource(resourceList);
181                 
182                 return request;
183         }
184         
185         private ResourceDefinition getFreeVnfResource() {
186                 ResourceDefinition def = new ResourceDefinition();
187                 def.setVnfInstanceId(vnfInstanceId);
188                 def.setVimId("001");
189                 List<AddResource> resources = new ArrayList<>();
190                 AddResource res = new AddResource();
191                 res.setVdu("1");
192                 res.setType("vdu");
193                 res.setResourceDefinitionId(2);
194                 resources.add(res);
195                 def.setAddResource(resources);
196                 return def;
197         }
198
199         public void setDriverRequest(HealVnfRequest driverRequest) {
200                 this.driverRequest = driverRequest;
201         }
202
203         public void setVnfInstanceId(String vnfInstanceId) {
204                 this.vnfInstanceId = vnfInstanceId;
205         }
206
207         public void setJobId(String jobId) {
208                 this.jobId = jobId;
209         }
210
211         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
212                 this.requestConverter = requestConverter;
213         }
214
215         public void setVnfmId(String inVnfmId) {
216                 this.vnfmId = inVnfmId;
217         }
218 }