2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.client.lcm.impl.business;
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;
38 import java.lang.reflect.InvocationTargetException;
39 import java.lang.reflect.Method;
40 import java.util.Properties;
41 import java.util.concurrent.TimeoutException;
43 class LCMRequestProcessor {
45 private final IInvocationManager invocationManager;
46 private final ObjectMapper mapper;
48 LCMRequestProcessor(ApplicationContext context, Properties properties) throws AppcClientBusinessException {
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);
59 <T> void processAsync(Object rpcInput, String rpcName, Class<T> rpcOutputType, ResponseHandler<T> handler) throws AppcClientInternalException {
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);
70 <T> T processSync(Object rpcInput, String rpcName, Class<T> rpcOutputType) throws AppcClientInternalException, AppcClientBusinessException {
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();
82 throw new AppcClientInternalException(e);
84 }catch (TimeoutException e){
85 throw new AppcClientBusinessException(e);
90 private CommonHeader getCommonHeader(Object rpcInput) throws AppcClientInternalException {
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");
100 private String createCorrelationID(Object rpcInput) throws AppcClientInternalException {
101 CommonHeader commonHeader = getCommonHeader(rpcInput);
102 return commonHeader.getRequestId() + "-" + commonHeader.getSubRequestId();
105 private String marshallRPCInput(Object rpcInput) throws AppcClientInternalException {
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);
116 void shutdown(boolean isForceShutdown){
117 invocationManager.shutdown(isForceShutdown);