Add unit test cases
[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         
52         @Autowired
53         private VnfmJobExecutionMapper jobDbMgmr;
54         
55         private Driver2CbamRequestConverter requestConverter;
56         
57         public ScaleVnfContinueRunnable(String vnfmId, ScaleVnfRequest 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         }
68         
69         private void handleGrant(){
70                 try {
71                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
72                         nslcmMgmr.grantVnf(grantRequest);
73                 } catch (Exception e) {
74                         logger.error("ScaleVnfContinueRunnable --> handleGrant error.", e);
75                 }
76         }
77         
78         public void run() {
79                 handleGrant();
80                 handleScale();
81         }
82         
83         
84
85         private CBAMScaleVnfResponse handleScale() {
86                 CBAMScaleVnfResponse cbamResponse = null;
87                 try {
88                         CBAMScaleVnfRequest scaleReq = requestConverter.scaleReqconvert(driverRequest);
89                         cbamResponse = cbamMgmr.scaleVnf(scaleReq, vnfInstanceId);
90                         handleCbamScaleResponse(cbamResponse, jobId);
91                 } catch (Exception e) {
92                         logger.error("ScaleVnfContinueRunnable --> handleScale error.", e);
93                 }
94                 
95                 return cbamResponse;
96         }
97
98         private void handleCbamScaleResponse(CBAMScaleVnfResponse cbamResponse, String jobId) {
99                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
100                 
101                 jobInfo.setVnfmExecutionId(cbamResponse.getId());
102                 if(CommonEnum.OperationStatus.FAILED == cbamResponse.getStatus()) {
103                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
104                 }
105                 else {
106                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
107                 }
108                 jobDbMgmr.update(jobInfo);
109         }
110         
111         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
112                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
113                 
114                 request.setVnfInstanceId(vnfInstanceId);
115                 if(ScaleType.SCALE_OUT == driverRequest.getType()) {
116                         request.setLifecycleOperation(LifecycleOperation.Scaleout);
117                 }else {
118                         request.setLifecycleOperation(LifecycleOperation.Scalein);
119                 }
120                 request.setJobId(jobId);
121                 
122                 ResourceDefinition resource = getFreeVnfResource();
123                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
124                 resourceList.add(resource);
125                 request.setRemoveResource(resourceList);
126                 
127                 return request;
128         }
129         
130         private ResourceDefinition getFreeVnfResource() {
131                 ResourceDefinition def = new ResourceDefinition();
132                 def.setVnfInstanceId(vnfInstanceId);
133                 def.setVimId("001");
134                 List<AddResource> resources = new ArrayList<>();
135                 AddResource res = new AddResource();
136                 res.setVdu("1");
137                 res.setType("vdu");
138                 res.setResourceDefinitionId(2);
139                 resources.add(res);
140                 def.setAddResource(resources);
141                 return def;
142         }
143
144         public void setDriverRequest(ScaleVnfRequest driverRequest) {
145                 this.driverRequest = driverRequest;
146         }
147
148         public void setVnfInstanceId(String vnfInstanceId) {
149                 this.vnfInstanceId = vnfInstanceId;
150         }
151
152         public void setJobId(String jobId) {
153                 this.jobId = jobId;
154         }
155
156         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
157                 this.requestConverter = requestConverter;
158         }
159
160 }