ea1baa78b5372f57518acea6d1dc8cd7a5ebc52c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.appc.orchestrator.client;
22
23 import org.camunda.bpm.client.task.ExternalTask;
24 import org.camunda.bpm.client.task.ExternalTaskService;
25 import org.onap.appc.client.lcm.api.ResponseHandler;
26 import org.onap.appc.client.lcm.exceptions.AppcClientException;
27 import org.onap.appc.client.lcm.model.Status;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class ApplicationControllerCallback<T> implements ResponseHandler<T> {
32
33     private static final Logger logger = LoggerFactory.getLogger(ApplicationControllerCallback.class);
34
35     private final ExternalTask externalTask;
36     private final ExternalTaskService externalTaskService;
37     private final ApplicationControllerSupport appCSupport;
38
39     public ApplicationControllerCallback(ExternalTask externalTask, ExternalTaskService externalTaskService,
40             ApplicationControllerSupport appCSupport) {
41         this.externalTask = externalTask;
42         this.externalTaskService = externalTaskService;
43         this.appCSupport = appCSupport;
44     }
45
46     @Override
47     public void onResponse(T response) {
48         logger.info("ON RESPONSE IN CALLBACK");
49
50         Status status = appCSupport.getStatusFromGenericResponse(response);
51
52         logger.info("Status code is: " + status.getCode());
53         logger.info("Status message is: " + status.getMessage());
54
55         if (appCSupport.getFinalityOf(status)) {
56             logger.debug("Obtained final status, complete the task");
57             completeExternalTask(externalTask, externalTaskService, status);
58         } else {
59             logger.debug("Intermediate status, continue the task");
60         }
61     }
62
63     @Override
64     public void onException(AppcClientException exception) {
65
66         logger.info("ON EXCEPTION IN CALLBACK");
67         logger.info("Exception from APPC: " + exception.getMessage());
68         Status exceptionStatus = appCSupport.buildStatusFromAppcException(exception);
69         completeExternalTask(externalTask, externalTaskService, exceptionStatus);
70     }
71
72     private void completeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService,
73             Status status) {
74
75         if (appCSupport.getCategoryOf(status).equals(StatusCategory.NORMAL)) {
76             externalTaskService.complete(externalTask);
77             logger.debug("The External Task Id: {} Successful", externalTask.getId());
78         } else {
79             logger.debug("The External Task Id: {} Failed", externalTask.getId());
80             externalTaskService.handleBpmnError(externalTask, "MSOWorkflowException", status.getMessage());
81         }
82     }
83 }