Re-commit some changes which were lost
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / vnf / VnfAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * Modifications Copyright (c) 2019 Samsung
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.so.adapters.vnf;
26
27
28 import java.util.Map;
29 import javax.inject.Provider;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.core.GenericEntity;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.xml.ws.Holder;
43 import org.apache.http.HttpStatus;
44 import org.onap.so.adapters.vnf.exceptions.VnfException;
45 import org.onap.so.adapters.vnfrest.CreateVfModuleRequest;
46 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
47 import org.onap.so.adapters.vnfrest.DeleteVfModuleRequest;
48 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
49 import org.onap.so.adapters.vnfrest.QueryVfModuleResponse;
50 import org.onap.so.adapters.vnfrest.RollbackVfModuleRequest;
51 import org.onap.so.adapters.vnfrest.RollbackVfModuleResponse;
52 import org.onap.so.adapters.vnfrest.UpdateVfModuleRequest;
53 import org.onap.so.adapters.vnfrest.UpdateVfModuleResponse;
54 import org.onap.so.adapters.vnfrest.VfModuleExceptionResponse;
55 import org.onap.so.adapters.vnfrest.VfModuleRollback;
56 import org.onap.so.entity.MsoRequest;
57 import org.onap.so.logger.ErrorCode;
58 import org.onap.so.logger.MessageEnum;
59 import org.onap.so.openstack.beans.VnfRollback;
60 import org.onap.so.openstack.beans.VnfStatus;
61 import org.onap.so.openstack.exceptions.MsoExceptionCategory;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.stereotype.Component;
66 import org.springframework.transaction.annotation.Transactional;
67 import io.swagger.annotations.Api;
68 import io.swagger.annotations.ApiOperation;
69 import io.swagger.annotations.ApiParam;
70 import io.swagger.annotations.ApiResponse;
71 import io.swagger.annotations.ApiResponses;
72
73 /**
74  * This class services calls to the REST interface for VF Modules (http://host:port/vnfs/rest/v1/vnfs) Both XML and JSON
75  * can be produced/consumed. Set Accept: and Content-Type: headers appropriately. XML is the default. For testing, call
76  * with cloudSiteId = ___TESTING___ To test exceptions, also set tenantId = ___TESTING___
77  */
78 @Path("/v1/vnfs")
79 @Api(value = "/v1/vnfs", description = "root of vnf adapters restful web service")
80 @Transactional
81 @Component
82 public class VnfAdapterRest {
83     private static Logger logger = LoggerFactory.getLogger(VnfAdapterRest.class);
84     private static final String TESTING_KEYWORD = "___TESTING___";
85     private static final String RESP = ", resp=";
86
87     @Autowired
88     private MsoVnfAdapterImpl vnfAdapter;
89     // TODO Logging, SkipAAI, CREATED flags, Integrate with BPEL, Auth,
90
91     @Autowired
92     private Provider<BpelRestClient> bpelRestClientProvider;
93
94
95     /*
96      * URL:http://localhost:8080/vnfs/rest/v1/vnfs/<aaivnfid>/vf-modules/<aaimodid> REQUEST: {"deleteVfModuleRequest":
97      * {"cloudSiteId": "DAN", "tenantId": "214b428a1f554c02935e66330f6a5409", "vnfId": "somevnfid", "vfModuleId":
98      * "somemodid", "vfModuleStackId": "4e567676-e266-4594-a3a6-131c8a2baf73", "messageId": "ra.1", "notificationUrl":
99      * "http://localhost:8089/vnfmock", "skipAAI": true, "msoRequest": { "requestId": "ra1", "serviceInstanceId": "sa1"
100      * }} }
101      */
102     @DELETE
103     @Path("{aaiVnfId}/vf-modules/{aaiVfModuleId}")
104     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
105     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
106     @ApiOperation(value = "DeleteVfModule", response = Response.class,
107             notes = "Delete an existing vnfModule, DeleteVfModuleRequest JSON is required")
108     @ApiResponses({@ApiResponse(code = 200, message = "vnfModule has been successfully deleted"),
109             @ApiResponse(code = 202, message = "delete vnfModule request has been accepted (async only)"),
110             @ApiResponse(code = 500, message = "delete vnfModule failed, examine entity object for details")})
111     public Response deleteVfModule(
112             @ApiParam(value = "aaiVnfId", required = true) @PathParam("aaiVnfId") String aaiVnfId,
113             @ApiParam(value = "aaiVfModuleId", required = true) @PathParam("aaiVfModuleId") String aaiVfModuleId,
114             @ApiParam(value = "DeleteVfModuleRequest", required = true) final DeleteVfModuleRequest req) {
115         logger.debug("Delete VfModule enter: " + req.toJsonString());
116         if (aaiVnfId == null || !aaiVnfId.equals(req.getVnfId())) {
117             logger.debug("Req rejected - aaiVnfId not provided or doesn't match URL");
118             return Response.status(HttpStatus.SC_BAD_REQUEST).type(MediaType.TEXT_PLAIN)
119                     .entity("vnfid in URL does not match content").build();
120         }
121         if (aaiVfModuleId == null || !aaiVfModuleId.equals(req.getVfModuleId())) {
122             logger.debug("Req rejected - aaiVfModuleId not provided or doesn't match URL");
123             return Response.status(HttpStatus.SC_BAD_REQUEST).type(MediaType.TEXT_PLAIN)
124                     .entity("vfModuleId in URL does not match content").build();
125         }
126         DeleteVfModuleTask task = new DeleteVfModuleTask(req);
127         if (req.isSynchronous()) {
128             // This is a synchronous request
129             task.run();
130             return Response.status(task.getStatusCode()).entity(task.getGenericEntityResponse()).build();
131         } else {
132             // This is an asynchronous request
133             try {
134                 Thread t1 = new Thread(task);
135                 t1.start();
136             } catch (Exception e) {
137                 // problem handling delete, send generic failure as sync resp to caller
138                 logger.error("", MessageEnum.RA_DELETE_VNF_ERR.toString(), "deleteVfModule",
139                         ErrorCode.BusinessProcesssError.getValue(), "Exception in deleteVfModule", e);
140                 return Response.serverError().build();
141             }
142             // send sync response (ACK) to caller
143             logger.debug("deleteVNFVolumes exit");
144             return Response.status(HttpStatus.SC_ACCEPTED).build();
145         }
146     }
147
148     public class DeleteVfModuleTask implements Runnable {
149         private final DeleteVfModuleRequest req;
150         private DeleteVfModuleResponse response = null;
151         private VfModuleExceptionResponse eresp = null;
152         private boolean sendxml;
153
154         public DeleteVfModuleTask(DeleteVfModuleRequest req) {
155             this.req = req;
156             this.sendxml = true; // can be set with a field or header later
157         }
158
159         public int getStatusCode() {
160             return (response != null) ? HttpStatus.SC_OK : HttpStatus.SC_BAD_REQUEST;
161         }
162
163         public Object getGenericEntityResponse() {
164             return (response != null) ? new GenericEntity<DeleteVfModuleResponse>(response) {}
165                     : new GenericEntity<VfModuleExceptionResponse>(eresp) {};
166         }
167
168         private String getResponse() {
169             if (response != null) {
170                 return sendxml ? response.toXmlString() : response.toJsonString();
171             } else {
172                 return sendxml ? eresp.toXmlString() : eresp.toJsonString();
173             }
174         }
175
176         @Override
177         public void run() {
178             try {
179                 String cloudsite = req.getCloudSiteId();
180                 Holder<Map<String, String>> outputs = new Holder<>();
181                 if (cloudsite != null && !cloudsite.equals(TESTING_KEYWORD)) {
182                     // vnfAdapter.deleteVnf (req.getCloudSiteId(), req.getTenantId(), req.getVfModuleStackId(),
183                     // req.getMsoRequest());
184                     vnfAdapter.deleteVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(),
185                             req.getVfModuleStackId(), req.getMsoRequest(), outputs);
186                 }
187                 response = new DeleteVfModuleResponse(req.getVnfId(), req.getVfModuleId(), Boolean.TRUE,
188                         req.getMessageId(), outputs.value);
189             } catch (VnfException e) {
190                 logger.error("{} {} {}", MessageEnum.RA_DELETE_VNF_ERR.toString(),
191                         ErrorCode.BusinessProcesssError.getValue(), "VnfException - Delete VNF Module", e);
192                 eresp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE,
193                         req.getMessageId());
194             }
195             if (!req.isSynchronous()) {
196                 BpelRestClient bpelClient = bpelRestClientProvider.get();
197                 bpelClient.bpelPost(getResponse(), req.getNotificationUrl(), sendxml);
198             }
199             logger.debug("Delete vfModule exit: code=" + getStatusCode() + RESP + getResponse());
200         }
201     }
202
203     /*
204      * URL:http://localhost:8080/vnfs/rest/v1/vnfs/<aaiVnfId>/vf-modules/<aaiVfModuleId>?cloudSiteId=DAN&tenantId=
205      * vfModule?&skipAAI=TRUE&msoRequest.requestId=ra1&msoRequest.serviceInstanceId=si1&vfModuleName=T2N2S1 RESP:
206      * {"queryVfModuleResponse": { "vfModuleId": "AvfmodId", "vfModuleOutputs": {"entry": { "key":
207      * "server_private_ip_1", "value": "10.100.1.25" }}, "vfModuleStackId":
208      * "RaaVnf1/abfa8a6d-feb1-40af-aea3-109403b1cf6b", "vnfId": "AvnfID", "vnfStatus": "ACTIVE" }}
209      */
210     @GET
211     @Path("{aaiVnfId}/vf-modules/{aaiVfModuleId}")
212     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
213     @ApiOperation(value = "QueryVfModule", response = Response.class, notes = "Query an existing vnfModule")
214     @ApiResponses({@ApiResponse(code = 200, message = "vnfModule has been successfully queried"),
215             @ApiResponse(code = 500, message = "query vnfModule failed, examine entity object for details")})
216     public Response queryVfModule(@ApiParam(value = "aaiVnfId", required = true) @PathParam("aaiVnfId") String aaiVnfId,
217             @ApiParam(value = "aaiVfModuleId", required = true) @PathParam("aaiVfModuleId") String aaiVfModuleId,
218             @ApiParam(value = "cloudSiteId", required = true) @QueryParam("cloudSiteId") String cloudSiteId,
219             @ApiParam(value = "cloudOwner", required = true) @QueryParam("cloudOwner") String cloudOwner,
220             @ApiParam(value = "tenantId", required = true) @QueryParam("tenantId") String tenantId,
221             @ApiParam(value = "vfModuleName", required = true) @QueryParam("vfModuleName") String vfModuleName, // RAA?
222                                                                                                                 // Id in
223                                                                                                                 // doc
224             @ApiParam(value = "skipAAI", required = true) @QueryParam("skipAAI") Boolean skipAAI,
225             @ApiParam(value = "msoRequest.requestId",
226                     required = true) @QueryParam("msoRequest.requestId") String requestId,
227             @ApiParam(value = "msoRequest.serviceInstanceId",
228                     required = true) @QueryParam("msoRequest.serviceInstanceId") String serviceInstanceId) {
229         // This request responds synchronously only
230         logger.debug("Query vfModule enter:" + vfModuleName);
231         MsoRequest msoRequest = new MsoRequest(requestId, serviceInstanceId);
232
233         try {
234             int respStatus = HttpStatus.SC_OK;
235             QueryVfModuleResponse qryResp = new QueryVfModuleResponse(aaiVnfId, aaiVfModuleId, null, null, null);
236             Holder<Boolean> vnfExists = new Holder<>();
237             Holder<String> vfModuleId = new Holder<>();
238             Holder<VnfStatus> status = new Holder<>();
239             Holder<Map<String, String>> outputs = new Holder<>();
240             vnfAdapter.queryVnf(cloudSiteId, cloudOwner, tenantId, vfModuleName, msoRequest, vnfExists, vfModuleId,
241                     status, outputs);
242             if (!vnfExists.value) {
243                 logger.debug("vfModule not found");
244                 respStatus = HttpStatus.SC_NOT_FOUND;
245             } else {
246                 logger.debug("vfModule found" + vfModuleId.value + ", status=" + status.value);
247                 qryResp.setVfModuleId(vfModuleId.value);
248                 qryResp.setVnfStatus(status.value);
249                 qryResp.setVfModuleOutputs(outputs.value);
250             }
251             logger.debug("Query vfModule exit");
252             return Response.status(respStatus).entity(new GenericEntity<QueryVfModuleResponse>(qryResp) {}).build();
253         } catch (VnfException e) {
254             logger.error("{} {} {} {} {}", MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, "queryVfModule",
255                     ErrorCode.BusinessProcesssError.getValue(), "VnfException - queryVfModule", e);
256             VfModuleExceptionResponse excResp =
257                     new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.FALSE, null);
258             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
259                     .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {}).build();
260         }
261     }
262
263     /*
264      * URL: http://localhost:8080/vnfs/rest/v1/vnfs/<aaivnfid>/vf-modules REQUEST: {"createVfModuleRequest":
265      * {"cloudSiteId": "DAN", "tenantId": "214b428a1f554c02935e66330f6a5409", "vnfId": "somevnfid", "vfModuleId":
266      * "somemodid", "vfModuleName": "RaaVnf1", "vnfType": "ApacheVnf", "vfModuleParams": {"entry": [ {"key":
267      * "network_id", "value": "59ed7b41-2983-413f-ba93-e7d437433916"}, {"key": "subnet_id", "value":
268      * "086c9298-5c57-49b7-bb2b-6fd5730c5d92"}, {"key": "server_name_0", "value": "RaaVnf1"} ]}, "failIfExists": true,
269      * "messageId": "ra.1", "notificationUrl": "http://localhost:8089/vnfmock", "skipAAI": true, "msoRequest": {
270      * "requestId": "ra1", "serviceInstanceId": "sa1" }} }
271      */
272     @POST
273     @Path("{aaiVnfId}/vf-modules")
274     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
275     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
276     @ApiOperation(value = "CreateVfModule", response = Response.class, notes = "Create a vnfModule")
277     @ApiResponses({@ApiResponse(code = 200, message = "vnfModule has been successfully created"),
278             @ApiResponse(code = 202, message = "create vnfModule request has been successfully accepted (async only)"),
279             @ApiResponse(code = 500, message = "create vnfModule failed, examine entity object for details")})
280     public Response createVfModule(
281             @ApiParam(value = "aaiVnfId", required = true) @PathParam("aaiVnfId") String aaiVnfId,
282             @ApiParam(value = "CreateVfModuleRequest", required = true) final CreateVfModuleRequest req) {
283         logger.debug("Create VfModule enter inside VnfAdapterRest: " + req.toJsonString());
284         if (aaiVnfId == null || !aaiVnfId.equals(req.getVnfId())) {
285             logger.debug("Req rejected - aaiVnfId not provided or doesn't match URL");
286             return Response.status(HttpStatus.SC_BAD_REQUEST).type(MediaType.TEXT_PLAIN)
287                     .entity("vnfid in URL does not match content").build();
288         }
289         CreateVfModuleTask task = new CreateVfModuleTask(req);
290         if (req.isSynchronous()) {
291             // This is a synchronous request
292             task.run();
293             return Response.status(task.getStatusCode()).entity(task.getGenericEntityResponse()).build();
294         } else {
295             // This is an asynchronous request
296             try {
297                 Thread t1 = new Thread(task);
298                 t1.start();
299             } catch (Exception e) {
300                 // problem handling create, send generic failure as sync resp to caller
301                 logger.error("{} {} {} {}", MessageEnum.RA_CREATE_VNF_ERR, "createVfModule",
302                         ErrorCode.BusinessProcesssError, "Exception - createVfModule", e);
303                 return Response.serverError().build();
304             }
305             // send sync response (ACK) to caller
306             logger.debug("createVfModule exit");
307             return Response.status(HttpStatus.SC_ACCEPTED).build();
308         }
309     }
310
311     public class CreateVfModuleTask implements Runnable {
312         private final CreateVfModuleRequest req;
313         private CreateVfModuleResponse response = null;
314         private VfModuleExceptionResponse eresp = null;
315         private boolean sendxml;
316
317         public CreateVfModuleTask(CreateVfModuleRequest req) {
318             this.req = req;
319             this.sendxml = true; // can be set with a field or header later
320         }
321
322         public int getStatusCode() {
323             return (response != null) ? HttpStatus.SC_OK : HttpStatus.SC_BAD_REQUEST;
324         }
325
326         public Object getGenericEntityResponse() {
327             return (response != null) ? new GenericEntity<CreateVfModuleResponse>(response) {}
328                     : new GenericEntity<VfModuleExceptionResponse>(eresp) {};
329         }
330
331         private String getResponse() {
332             if (response != null) {
333                 return sendxml ? response.toXmlString() : response.toJsonString();
334             } else {
335                 return sendxml ? eresp.toXmlString() : eresp.toJsonString();
336             }
337         }
338
339         @Override
340         public void run() {
341             logger.debug("CreateVfModuleTask start");
342             try {
343                 // Synchronous Web Service Outputs
344                 Holder<String> vfModuleStackId = new Holder<>();
345                 Holder<Map<String, String>> outputs = new Holder<>();
346                 Holder<VnfRollback> vnfRollback = new Holder<>();
347                 String completeVnfVfModuleType = req.getVnfType() + "::" + req.getVfModuleType();
348                 logger.debug("completeVnfVfModuleType=" + completeVnfVfModuleType);
349                 String cloudsite = req.getCloudSiteId();
350                 String cloudOwner = req.getCloudOwner();
351                 if (cloudsite != null && cloudsite.equals(TESTING_KEYWORD)) {
352                     String tenant = req.getTenantId();
353                     if (tenant != null && tenant.equals(TESTING_KEYWORD)) {
354                         throw new VnfException("testing.");
355                     }
356                     vnfRollback.value = new VnfRollback(req.getVnfId(), tenant, cloudOwner, cloudsite, true, false,
357                             new MsoRequest("reqid", "svcid"), req.getVolumeGroupId(), req.getVolumeGroupId(),
358                             req.getRequestType(), req.getModelCustomizationUuid());
359                     vfModuleStackId.value = "479D3D8B-6360-47BC-AB75-21CC91981484";
360                     outputs.value = VolumeAdapterRest.testMap();
361                 } else {
362                     vnfAdapter.createVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(),
363                             completeVnfVfModuleType, req.getVnfVersion(), req.getVnfId(), req.getVfModuleName(),
364                             req.getVfModuleId(), req.getRequestType(), req.getVolumeGroupStackId(),
365                             req.getBaseVfModuleStackId(), req.getModelCustomizationUuid(), req.getVfModuleParams(),
366                             req.getFailIfExists(), req.getBackout(), req.getEnableBridge(), req.getMsoRequest(),
367                             vfModuleStackId, outputs, vnfRollback);
368                 }
369                 VfModuleRollback modRollback = new VfModuleRollback(vnfRollback.value, req.getVfModuleId(),
370                         vfModuleStackId.value, req.getMessageId());
371                 response = new CreateVfModuleResponse(req.getVnfId(), req.getVfModuleId(), vfModuleStackId.value,
372                         Boolean.TRUE, outputs.value, modRollback, req.getMessageId());
373             } catch (VnfException e) {
374                 logger.debug("Exception :", e);
375                 eresp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE,
376                         req.getMessageId());
377             }
378             if (!req.isSynchronous()) {
379                 BpelRestClient bpelClient = bpelRestClientProvider.get();
380                 bpelClient.bpelPost(getResponse(), req.getNotificationUrl(), sendxml);
381             }
382             logger.debug("CreateVfModuleTask exit: code=" + getStatusCode() + RESP + getResponse());
383         }
384     }
385
386     @PUT
387     @Path("{aaiVnfId}/vf-modules/{aaiVfModuleId}")
388     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
389     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
390     @ApiOperation(value = "UpdateVfModule", response = Response.class, notes = "Update an existing vnfModule")
391     @ApiResponses({@ApiResponse(code = 200, message = "vnfModule has been successfully updated"),
392             @ApiResponse(code = 202, message = "update vnfModule request has been successfully accepted (async only)"),
393             @ApiResponse(code = 500, message = "update vnfModule failed, examine entity object for details")})
394     public Response updateVfModule(
395             @ApiParam(value = "aaiVnfId", required = true) @PathParam("aaiVnfId") String aaiVnfId,
396             @ApiParam(value = "aaiVfModuleId", required = true) @PathParam("aaiVfModuleId") String aaiVfModuleId,
397             @ApiParam(value = "UpdateVfModuleRequest", required = true) final UpdateVfModuleRequest req) {
398         logger.debug("Update VfModule enter: " + req.toJsonString());
399         UpdateVfModulesTask task = new UpdateVfModulesTask(req);
400         if (req.isSynchronous()) {
401             // This is a synchronous request
402             task.run();
403             return Response.status(task.getStatusCode()).entity(task.getGenericEntityResponse()).build();
404         } else {
405             // This is an asynchronous request
406             try {
407                 Thread t1 = new Thread(task);
408                 t1.start();
409             } catch (Exception e) {
410                 // problem handling create, send generic failure as sync resp to caller
411                 logger.error("{} {} {} {}", MessageEnum.RA_UPDATE_VNF_ERR.toString(), "updateVfModule",
412                         ErrorCode.BusinessProcesssError.getValue(), "Exception - updateVfModule", e);
413                 return Response.serverError().build();
414             }
415             // send sync response (ACK) to caller
416             logger.debug("updateVfModules exit");
417             return Response.status(HttpStatus.SC_ACCEPTED).build();
418         }
419     }
420
421     public class UpdateVfModulesTask implements Runnable {
422         private final UpdateVfModuleRequest req;
423         private UpdateVfModuleResponse response = null;
424         private VfModuleExceptionResponse eresp = null;
425         private boolean sendxml;
426
427         public UpdateVfModulesTask(UpdateVfModuleRequest req) {
428             this.req = req;
429             this.sendxml = true; // can be set with a field or header later
430         }
431
432         public int getStatusCode() {
433             return (response != null) ? HttpStatus.SC_OK : HttpStatus.SC_BAD_REQUEST;
434         }
435
436         public Object getGenericEntityResponse() {
437             return (response != null) ? new GenericEntity<UpdateVfModuleResponse>(response) {}
438                     : new GenericEntity<VfModuleExceptionResponse>(eresp) {};
439         }
440
441         private String getResponse() {
442             if (response != null) {
443                 return sendxml ? response.toXmlString() : response.toJsonString();
444             } else {
445                 return sendxml ? eresp.toXmlString() : eresp.toJsonString();
446             }
447         }
448
449         @Override
450         public void run() {
451             try {
452                 // MsoVnfAdapter vnfAdapter = new MsoVnfAdapterImpl (msoPropertiesFactory, cloudConfigFactory);
453
454                 // Synchronous Web Service Outputs
455                 Holder<String> vfModuleStackId = new Holder<>();
456                 Holder<Map<String, String>> outputs = new Holder<>();
457                 Holder<VnfRollback> vnfRollback = new Holder<>();
458                 String completeVnfVfModuleType = req.getVnfType() + "::" + req.getVfModuleType();
459                 logger.debug("in updateVf - completeVnfVfModuleType=" + completeVnfVfModuleType);
460
461                 vnfAdapter.updateVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(),
462                         // req.getVnfType(),
463                         completeVnfVfModuleType, req.getVnfVersion(), req.getVfModuleName(), req.getRequestType(),
464                         req.getVolumeGroupStackId(), req.getBaseVfModuleStackId(), req.getVfModuleStackId(),
465                         req.getModelCustomizationUuid(), req.getVfModuleParams(), req.getMsoRequest(), outputs,
466                         vnfRollback);
467
468                 response = new UpdateVfModuleResponse(req.getVnfId(), req.getVfModuleId(), vfModuleStackId.value,
469                         outputs.value, req.getMessageId());
470             } catch (VnfException e) {
471                 logger.debug("Exception :", e);
472                 eresp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE,
473                         req.getMessageId());
474             }
475             if (!req.isSynchronous()) {
476                 // This is asynch, so POST response back to caller
477                 BpelRestClient bpelClient = bpelRestClientProvider.get();
478                 bpelClient.bpelPost(getResponse(), req.getNotificationUrl(), sendxml);
479             }
480             logger.debug("Update VfModule exit: code=" + getStatusCode() + RESP + getResponse());
481         }
482     }
483
484     /*
485      * URL:http://localhost:8080/vnfs/rest/v1/vnfs/<aaivnfid>/vf-modules/<aaimodid>/rollback REQUEST:
486      * {"deleteVfModuleRequest": {"cloudSiteId": "DAN", "tenantId": "214b428a1f554c02935e66330f6a5409", "vnfId":
487      * "somevnfid", "vfModuleId": "somemodid", "vfModuleStackId": "4e567676-e266-4594-a3a6-131c8a2baf73", "messageId":
488      * "ra.1", "notificationUrl": "http://localhost:8089/vnfmock", "skipAAI": true, "msoRequest": { "requestId": "ra1",
489      * "serviceInstanceId": "sa1" }} }
490      */
491     @DELETE
492     @Path("{aaiVnfId}/vf-modules/{aaiVfModuleId}/rollback")
493     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
494     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
495     @ApiOperation(value = "RollbackVfModule", response = Response.class, notes = "Rollback an existing vnfModule")
496     @ApiResponses({@ApiResponse(code = 200, message = "vnfModule has been successfully rolled back"),
497             @ApiResponse(code = 202,
498                     message = "rollback vnfModule request has been successfully accepted (async only)"),
499             @ApiResponse(code = 500, message = "rollback vnfModule failed, examine entity object for details")})
500     public Response rollbackVfModule(
501             @ApiParam(value = "aaiVnfId", required = true) @PathParam("aaiVnfId") String aaiVnfId,
502             @ApiParam(value = "aaiVfModuleId", required = true) @PathParam("aaiVfModuleId") String aaiVfModuleId,
503             @ApiParam(value = "RollbackVfModuleRequest", required = true)
504             // @QueryParam("rollback") String rollback,
505             final RollbackVfModuleRequest req) {
506         logger.debug("Rollback VfModule enter: " + req.toJsonString());
507         RollbackVfModulesTask task = new RollbackVfModulesTask(req);
508         if (req.isSynchronous()) {
509             // This is a synchronous request
510             task.run();
511             return Response.status(task.getStatusCode()).entity(task.getGenericEntityResponse()).build();
512         } else {
513             // This is an asynchronous request
514             try {
515                 Thread t1 = new Thread(task);
516                 t1.start();
517             } catch (Exception e) {
518                 // problem handling create, send generic failure as sync resp to caller
519                 logger.error("{} {} {} {}", MessageEnum.RA_ROLLBACK_VNF_ERR.toString(), "rollbackVfModule",
520                         ErrorCode.BusinessProcesssError.getValue(), "Exception - rollbackVfModule", e);
521                 return Response.serverError().build();
522             }
523             // send sync response (ACK) to caller
524             logger.debug("rollbackVfModule exit");
525             return Response.status(HttpStatus.SC_ACCEPTED).build();
526         }
527     }
528
529     public class RollbackVfModulesTask implements Runnable {
530         private final RollbackVfModuleRequest req;
531         private RollbackVfModuleResponse response = null;
532         private VfModuleExceptionResponse eresp = null;
533         private boolean sendxml;
534
535         public RollbackVfModulesTask(RollbackVfModuleRequest req) {
536             this.req = req;
537             this.sendxml = true; // can be set with a field or header later
538         }
539
540         public int getStatusCode() {
541             return (response != null) ? HttpStatus.SC_OK : HttpStatus.SC_BAD_REQUEST;
542         }
543
544         public Object getGenericEntityResponse() {
545             return (response != null) ? new GenericEntity<RollbackVfModuleResponse>(response) {}
546                     : new GenericEntity<VfModuleExceptionResponse>(eresp) {};
547         }
548
549         private String getResponse() {
550             if (response != null) {
551                 return sendxml ? response.toXmlString() : response.toJsonString();
552             } else {
553                 return sendxml ? eresp.toXmlString() : eresp.toJsonString();
554             }
555         }
556
557         @Override
558         public void run() {
559             try {
560                 VfModuleRollback vmr = req.getVfModuleRollback();
561                 VnfRollback vrb = new VnfRollback(vmr.getVfModuleStackId(), vmr.getTenantId(), vmr.getCloudOwner(),
562                         vmr.getCloudSiteId(), true, true, vmr.getMsoRequest(), null, null, null, null);
563                 vnfAdapter.rollbackVnf(vrb);
564                 response = new RollbackVfModuleResponse(Boolean.TRUE, req.getMessageId());
565             } catch (VnfException e) {
566                 logger.error("{} {} {}", MessageEnum.RA_ROLLBACK_VNF_ERR, ErrorCode.BusinessProcesssError,
567                         "Exception" + " - " + "rollbackVfModule", e);
568                 eresp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, false,
569                         req.getMessageId());
570             }
571             if (!req.isSynchronous()) {
572                 // This is asynch, so POST response back to caller
573                 BpelRestClient bpelClient = bpelRestClientProvider.get();
574                 bpelClient.bpelPost(getResponse(), req.getNotificationUrl(), sendxml);
575             }
576             logger.debug("RollbackVfModulesTask exit: code=" + getStatusCode() + RESP + getResponse());
577         }
578     }
579 }