1f73000299ec29e1608f5733e3cbb21802eb252e
[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.onap.so.adapters.appc.orchestrator.client.StatusCategory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class ApplicationControllerCallback<T> implements ResponseHandler<T> {
33
34     private static final Logger logger = LoggerFactory.getLogger(ApplicationControllerCallback.class);
35
36     private final ExternalTask externalTask;
37     private final ExternalTaskService externalTaskService;
38     private final ApplicationControllerSupport appCSupport;
39
40     public ApplicationControllerCallback(ExternalTask externalTask, ExternalTaskService externalTaskService,
41             ApplicationControllerSupport appCSupport) {
42         this.externalTask = externalTask;
43         this.externalTaskService = externalTaskService;
44         this.appCSupport = appCSupport;
45     }
46
47     @Override
48     public void onResponse(T response) {
49         logger.info("ON RESPONSE IN CALLBACK");
50
51         Status status = appCSupport.getStatusFromGenericResponse(response);
52
53         logger.info("Status code is: " + status.getCode());
54         logger.info("Status message is: " + status.getMessage());
55
56         if (appCSupport.getFinalityOf(status)) {
57             logger.debug("Obtained final status, complete the task");
58             completeExternalTask(externalTask, externalTaskService, status);
59         } else {
60             logger.debug("Intermediate status, continue the task");
61         }
62     }
63
64     @Override
65     public void onException(AppcClientException exception) {
66
67         logger.info("ON EXCEPTION IN CALLBACK");
68         logger.info("Exception from APPC: " + exception.getMessage());
69         Status exceptionStatus = appCSupport.buildStatusFromAppcException(exception);
70         completeExternalTask(externalTask, externalTaskService, exceptionStatus);
71     }
72
73     private void completeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService,
74             Status status) {
75
76         if (appCSupport.getCategoryOf(status).equals(StatusCategory.NORMAL)) {
77             externalTaskService.complete(externalTask);
78             logger.debug("The External Task Id: {} Successful", externalTask.getId());
79         } else {
80             logger.debug("The External Task Id: {} Failed", externalTask.getId());
81             externalTaskService.handleBpmnError(externalTask, "MSOWorkflowException", status.getMessage());
82         }
83     }
84 }