2  * ============LICENSE_START=======================================================
 
   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
 
  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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.client.lcm.impl.business;
 
  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;
 
  39 import java.lang.reflect.InvocationTargetException;
 
  40 import java.lang.reflect.Method;
 
  41 import java.util.Properties;
 
  42 import java.util.concurrent.TimeoutException;
 
  44 class LCMRequestProcessor {
 
  46     private final IInvocationManager invocationManager;
 
  47     private final ObjectMapper mapper;
 
  49     LCMRequestProcessor(ApplicationContext context, Properties properties) throws AppcClientBusinessException {
 
  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);
 
  60     <T> void processAsync(Object rpcInput, String rpcName, Class<T> rpcOutputType, ResponseHandler<T> handler) throws AppcClientInternalException {
 
  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);
 
  71     <T> T processSync(Object rpcInput, String rpcName, Class<T> rpcOutputType) throws AppcClientInternalException, AppcClientBusinessException {
 
  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();
 
  83                 throw new AppcClientInternalException(e);
 
  85         }catch (TimeoutException e){
 
  86             throw new AppcClientBusinessException(e);
 
  91     private CommonHeader getCommonHeader(Object rpcInput) throws AppcClientInternalException {
 
  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");
 
 101     private String createCorrelationID(Object rpcInput) throws AppcClientInternalException {
 
 102         CommonHeader commonHeader = getCommonHeader(rpcInput);
 
 103         return commonHeader.getRequestId() + "-" + commonHeader.getSubRequestId();
 
 106     private String marshallRPCInput(Object rpcInput) throws AppcClientInternalException {
 
 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);
 
 117     void shutdown(boolean isForceShutdown){
 
 118         invocationManager.shutdown(isForceShutdown);