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