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