fa274feed61bdd8dd284d35320077b8ea1e575ac
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / VnfmDriverMgmrIml.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
23 import org.apache.http.HttpStatus;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMOperExecutVnfRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.VnfmInfo;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
48 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
49 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
50 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
51 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
52 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
53 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.entity.ResponseDescriptor;
54 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.entity.ResponseHistoryList;
55 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Component;
58
59
60 @Component
61 public class VnfmDriverMgmrIml implements VnfmDriverMgmrInf{
62         private static final Logger logger = LogManager.getLogger("VnfmDriverMgmrIml");
63         
64         @Autowired
65         Driver2CbamRequestConverter requestConverter;
66         
67         @Autowired
68         Cbam2DriverResponseConverter responseConverter;
69         
70         @Autowired
71         private CbamMgmrInf cbamMgmr;
72         
73         @Autowired
74         private CatalogMgmrInf catalogMgmr;
75         
76         @Autowired
77         private NslcmMgmrInf nslcmMgmr;
78         
79         @Autowired
80         private VnfmJobExecutionRepository jobDbManager;
81         
82         public InstantiateVnfResponse instantiateVnf(InstantiateVnfRequest driverRequest, String vnfmId) throws VnfmDriverException {
83                 InstantiateVnfResponse driverResponse;
84                 try {
85                         //step 1: query vnfm info
86                         VnfmInfo vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
87                         
88                         if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
89                         {
90                                 throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
91                         }
92                         
93                         //step 3: create vnf
94                         CBAMCreateVnfRequest cbamRequest = requestConverter.createReqConvert(driverRequest);
95                         CBAMCreateVnfResponse cbamResponse = cbamMgmr.createVnf(cbamRequest);
96                         driverResponse = responseConverter.createRspConvert(cbamResponse);
97                         
98                         String vnfInstanceId = driverResponse.getVnfInstanceId();
99                         String jobId = driverResponse.getJobId();
100                         continueInstantiateVnf(driverRequest, vnfInstanceId, jobId);
101                         
102                         
103                 } catch (Exception e) {
104                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
105                 }
106                 
107         return driverResponse;
108         }
109
110         public void continueInstantiateVnf(InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId) {
111                 InstantiateVnfContinueRunnable runnable = new InstantiateVnfContinueRunnable(driverRequest, vnfInstanceId, jobId,
112                                 nslcmMgmr, catalogMgmr, cbamMgmr, requestConverter, jobDbManager);
113                 
114                 Thread thread = new Thread(runnable);
115                 
116                 thread.run();
117         }
118
119         public TerminateVnfResponse terminateVnf(TerminateVnfRequest driverRequest, String vnfmId, String vnfInstanceId) {
120                 TerminateVnfResponse driverResponse;
121                 try {
122                         VnfmInfo vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
123                         
124                         if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
125                         {
126                                 throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
127                         }
128                         driverResponse = generateTerminateVnfResponse(vnfInstanceId);
129                         String jobId = driverResponse.getJobId();
130                         continueTerminateVnf(driverRequest, vnfInstanceId, jobId);
131                         
132                 } catch (Exception e) {
133                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
134                 }
135                 
136         return driverResponse;
137         }
138
139         private TerminateVnfResponse generateTerminateVnfResponse(String vnfInstanceId) {
140                 VnfmJobExecutionInfo jobInfo = new VnfmJobExecutionInfo();
141                 jobInfo.setVnfInstanceId(vnfInstanceId);
142                 jobInfo.setVnfmInterfceName(CommonConstants.NSLCM_OPERATION_TERMINATE);
143                 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
144                 
145                 VnfmJobExecutionInfo jobInfo1=  jobDbManager.save(jobInfo);
146                 Long jobId = jobInfo1.getJobId();
147                 
148                 TerminateVnfResponse response = new TerminateVnfResponse();
149                 response.setJobId("" + jobId);
150                 return response;
151         }
152
153         public void continueTerminateVnf(TerminateVnfRequest driverRequest, String vnfInstanceId, String jobId) {
154                 TerminateVnfContinueRunnable runnable = new TerminateVnfContinueRunnable(driverRequest, vnfInstanceId, jobId,
155                                 nslcmMgmr, cbamMgmr, requestConverter, jobDbManager);
156                 
157                 Thread thread = new Thread(runnable);
158                 
159                 thread.run();
160         }
161
162
163         public QueryVnfResponse queryVnf(String vnfmId, String vnfInstanceId) {
164                 QueryVnfResponse driverResponse;
165                 try {
166                         nslcmMgmr.queryVnfm(vnfmId);
167                         VnfmInfo vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
168                         
169                         if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
170                         {
171                                 throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
172                         }
173                         CBAMQueryVnfResponse cbamResponse = cbamMgmr.queryVnf(vnfInstanceId);
174                         driverResponse = responseConverter.queryRspConvert(cbamResponse);
175                 } catch (Exception e) {
176                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
177                 }
178                 
179         return driverResponse;
180         }
181
182         public OperStatusVnfResponse getOperStatus(String vnfmId, String jobId)  throws VnfmDriverException {
183                 VnfmInfo vnfmInfo;
184                 try {
185                         vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
186                 }  catch (Exception e) {
187                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
188                 }
189                 
190                 if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
191                 {
192                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
193                 }
194                 
195                 VnfmJobExecutionInfo jobInfo = jobDbManager.findOne(Long.getLong(jobId));
196                 String execId = jobInfo.getVnfmExecutionId();
197                 
198                 CBAMQueryOperExecutionResponse cbamResponse;
199                 
200                 try {
201                         cbamResponse = cbamMgmr.queryOperExecution(execId);
202                 } catch (Exception e) {
203                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
204                 }
205                 
206                 OperStatusVnfResponse response = responseConverter.operRspConvert(cbamResponse);
207                 
208                 return response;
209         }
210
211         public ScaleVnfResponse scaleVnf(ScaleVnfRequest driverRequest, String vnfmId, String vnfInstanceId) throws VnfmDriverException {
212                 ScaleVnfResponse driverResponse;
213                 try {
214                         VnfmInfo vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
215                         
216                         if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
217                         {
218                                 throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
219                         }
220                         CBAMScaleVnfRequest cbamRequest = requestConverter.scaleReqconvert(driverRequest);
221                         CBAMScaleVnfResponse cbamResponse = cbamMgmr.scaleVnf(cbamRequest, vnfInstanceId);
222                         driverResponse = responseConverter.scaleRspConvert(cbamResponse);
223                 } catch (Exception e) {
224                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
225                 }
226                 
227         return driverResponse;
228         }
229
230         public HealVnfResponse healVnf(HealVnfRequest driverRequest, String vnfmId, String vnfInstanceId) throws VnfmDriverException {
231                 HealVnfResponse driverResponse;
232                 try {
233                         VnfmInfo vnfmInfo = nslcmMgmr.queryVnfm(vnfmId);
234                         
235                         if(vnfmInfo == null || vnfmId.equalsIgnoreCase(vnfmInfo.getVnfmId()))
236                         {
237                                 throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
238                         }
239                         CBAMHealVnfRequest cbamRequest = requestConverter.healReqConvert(driverRequest);
240                         CBAMHealVnfResponse cbamResponse = cbamMgmr.healVnf(cbamRequest, vnfInstanceId);
241                         driverResponse = responseConverter.healRspConvert(cbamResponse);
242                 } catch (Exception e) {
243                         throw new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
244                 }
245                 
246         return driverResponse;
247         }
248
249 }