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