8694bface1c3923a861ed522847b4c3d1561d0a0
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.client.impl.core;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 /** Abstract request response handler class, responsible for common functionality of
30  * @{@link AsyncRequestResponseHandler} and @{@link SyncRequestResponseHandler}
31  */
32 abstract class AbstractRequestResponseHandler implements RequestResponseHandler {
33
34     private final EELFLogger LOG = EELFManager.getInstance().getLogger(AbstractRequestResponseHandler.class);
35     ICoreResponseHandler businessCallback;
36     protected String corrID;
37     CoreManager coreManager;
38
39
40     AbstractRequestResponseHandler(String corrID,
41                                    ICoreResponseHandler businessCallback,
42                                    CoreManager coreManager)
43     {
44         this.businessCallback = businessCallback;
45         this.corrID = corrID;
46         this.coreManager = coreManager;
47     }
48
49     public synchronized void handleResponse(final MessageContext ctx, final String response) {
50         try {
51             coreManager.submitTask(ctx.getCorrelationID(), new Runnable() {
52                 @Override
53                 public void run() {
54                     LOG.info("handling response of corrID <" + corrID + ">" + "response " + response);
55                     if(coreManager.isExistHandler(corrID)) {
56                         runTask(response, ctx.getType());
57                     }
58
59                 }
60             });
61         } catch (InterruptedException e) {
62             LOG.error("could not handle response <" + response + "> of corrID <" + corrID + ">", e);
63         }
64     }
65
66     /**
67      *
68      * @param response - Response
69      * @param type - Type of Response
70      */
71     abstract void runTask(String response, String type);
72
73     @Override
74     public void sendRequest(String request, String corrId, String rpcName) throws CoreException {
75         if(!coreManager.isShutdownInProgress()) {
76             coreManager.registerHandler(corrId, this);
77             coreManager.sendRequest(request, corrId, rpcName);
78             coreManager.startTimer(corrId);
79         }else{
80             throw new CoreException("Shutdown is in progress. Request will not be handled");
81         }
82     }
83
84 }