First part of onap rename
[appc.git] / appc-client / client-lib / src / main / java / org / openecomp / appc / client / impl / core / AbstractRequestResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.client.impl.core;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 /** Abstract request response handler class, responsible for common functionality of
31  * @{@link AsyncRequestResponseHandler} and @{@link SyncRequestResponseHandler}
32  */
33 abstract class AbstractRequestResponseHandler implements RequestResponseHandler {
34
35     private final EELFLogger LOG = EELFManager.getInstance().getLogger(AbstractRequestResponseHandler.class);
36     ICoreResponseHandler businessCallback;
37     protected String corrID;
38     CoreManager coreManager;
39
40
41     AbstractRequestResponseHandler(String corrID,
42                                    ICoreResponseHandler businessCallback,
43                                    CoreManager coreManager)
44     {
45         this.businessCallback = businessCallback;
46         this.corrID = corrID;
47         this.coreManager = coreManager;
48     }
49
50     public synchronized void handleResponse(final MessageContext ctx, final String response) {
51         try {
52             coreManager.submitTask(ctx.getCorrelationID(), new Runnable() {
53                 @Override
54                 public void run() {
55                     LOG.info("handling response of corrID <" + corrID + ">" + "response " + response);
56                     if(coreManager.isExistHandler(corrID)) {
57                         runTask(response, ctx.getType());
58                     }
59
60                 }
61             });
62         } catch (InterruptedException e) {
63             LOG.error("could not handle response <" + response + "> of corrID <" + corrID + ">", e);
64         }
65     }
66
67     /**
68      *
69      * @param response - Response
70      * @param type - Type of Response
71      */
72     abstract void runTask(String response, String type);
73
74     @Override
75     public void sendRequest(String request, String corrId, String rpcName) throws CoreException {
76         if(!coreManager.isShutdownInProgress()) {
77             coreManager.registerHandler(corrId, this);
78             coreManager.sendRequest(request, corrId, rpcName);
79             coreManager.startTimer(corrId);
80         }else{
81             throw new CoreException("Shutdown is in progress. Request will not be handled");
82         }
83     }
84
85 }