b718c1ac9adcf3c8d599c5941a78972cced85565
[appc.git] / appc-client / client-kit / src / main / java / org / openecomp / appc / client / lcm / impl / business / LCMRequestProcessor.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.lcm.impl.business;
26
27 import org.onap.appc.client.impl.core.*;
28 import org.onap.appc.client.lcm.api.ApplicationContext;
29 import org.onap.appc.client.lcm.api.ResponseHandler;
30 import org.onap.appc.client.lcm.exceptions.AppcClientBusinessException;
31 import org.onap.appc.client.lcm.exceptions.AppcClientInternalException;
32 import org.onap.appc.client.lcm.model.CommonHeader;
33 import com.fasterxml.jackson.annotation.JsonInclude;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37
38
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.Properties;
42 import java.util.concurrent.TimeoutException;
43
44 class LCMRequestProcessor {
45
46     private final IInvocationManager invocationManager;
47     private final ObjectMapper mapper;
48
49     LCMRequestProcessor(ApplicationContext context, Properties properties) throws AppcClientBusinessException {
50         try{
51             invocationManager = InvocationManagerFactory.getInstance();
52             invocationManager.init(properties);
53             mapper = new ObjectMapper();
54             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
55         } catch (CoreException e) {
56             throw new AppcClientBusinessException(e);
57         }
58     }
59
60     <T> void processAsync(Object rpcInput, String rpcName, Class<T> rpcOutputType, ResponseHandler<T> handler) throws AppcClientInternalException {
61         try {
62             String correlationID = createCorrelationID(rpcInput);
63             String rpcStr = marshallRPCInput(rpcInput);
64             ICoreAsyncResponseHandler asyncResponseHandler = new CoreAsyncResponseHandlerImpl<T>(handler, rpcOutputType, mapper);
65             invocationManager.asyncRequest(rpcStr, asyncResponseHandler, correlationID, rpcName);
66         } catch (CoreException e) {
67             throw new AppcClientInternalException(e);
68         }
69     }
70
71     <T> T processSync(Object rpcInput, String rpcName, Class<T> rpcOutputType) throws AppcClientInternalException, AppcClientBusinessException {
72         T response = null;
73         try {
74             String correlationID = createCorrelationID(rpcInput);
75             String rpcStr = marshallRPCInput(rpcInput);
76             ICoreSyncResponseHandler syncResponseHandler = new CoreSyncResponseHandlerImpl<T>(rpcOutputType, mapper);
77             response = invocationManager.syncRequest(rpcStr, syncResponseHandler, correlationID, rpcName);
78         }catch (CoreException e){
79             if (e.getCause() instanceof AppcClientInternalException) {
80                 throw (AppcClientInternalException) e.getCause();
81             }
82             else {
83                 throw new AppcClientInternalException(e);
84             }
85         }catch (TimeoutException e){
86             throw new AppcClientBusinessException(e);
87         }
88         return response;
89     }
90
91     private CommonHeader getCommonHeader(Object rpcInput) throws AppcClientInternalException {
92         try {
93             Class<?> clazz = rpcInput.getClass();
94             Method method = clazz.getMethod("getCommonHeader");
95             return CommonHeader.class.cast(method.invoke(rpcInput));
96         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
97             throw new AppcClientInternalException("can't get commonHeader");
98         }
99     }
100
101     private String createCorrelationID(Object rpcInput) throws AppcClientInternalException {
102         CommonHeader commonHeader = getCommonHeader(rpcInput);
103         return commonHeader.getRequestId() + "-" + commonHeader.getSubRequestId();
104     }
105
106     private String marshallRPCInput(Object rpcInput) throws AppcClientInternalException {
107         try {
108             JsonNode body =  mapper.valueToTree(rpcInput);
109             ObjectNode message = mapper.createObjectNode();
110             message.set("input", body);
111             return message.toString();
112         } catch (RuntimeException e) {
113             throw new AppcClientInternalException("can't marshall input", e);
114         }
115     }
116
117     void shutdown(boolean isForceShutdown){
118         invocationManager.shutdown(isForceShutdown);
119     }
120 }