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.lcm.exceptions.AppcClientInternalException;
28 import org.onap.appc.client.lcm.model.Status;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
38 class CoreResponseHandler<T> {
39 private final Class<T> rpcOutput;
40 private final ObjectMapper mapper;
42 private final EELFLogger LOG = EELFManager.getInstance().getLogger(CoreResponseHandler.class);
44 CoreResponseHandler(Class<T> rpcOutput, ObjectMapper mapper) {
45 this.rpcOutput = rpcOutput;
49 T getResponse(String message, String type, Boolean[] isFinal) throws AppcClientInternalException {
50 if (type.equals("error")) {
52 String correlationId = getCorrelationID(message);
53 LOG.error("Received response with error on correlation id: " + correlationId);
54 } catch (IOException e) {
55 LOG.error("Received response with error and couldn't extract correlationID");
57 throw new AppcClientInternalException(message);
59 T responseObject = jsonToResponseObject(message);
60 int code = getStatusCode(responseObject);
62 int family = code / 100;
75 default: // Should never happen
76 throw new AppcClientInternalException(new IllegalStateException("Unsupported status code " + code + ". message: " + message));
78 return responseObject;
82 private T jsonToResponseObject(String message) throws AppcClientInternalException {
84 JsonNode jsonOutput = mapper.readTree(message).get("output");
85 return rpcOutput.cast(mapper.treeToValue(jsonOutput, rpcOutput));
86 } catch (IOException e) {
87 throw new AppcClientInternalException("failed to read message: " + message, e);
91 private int getStatusCode(Object response) throws AppcClientInternalException {
93 Method method = response.getClass().getMethod("getStatus");
94 Status status = (Status) method.invoke(response);
95 return status.getCode();
96 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
97 throw new AppcClientInternalException("failed to get status code from response: " + response, e);
101 private String getCorrelationID(String message) throws IOException {
102 JsonNode common = mapper.readTree(message).get("output").get("common-header");
103 String correlationId = common.get("request-id").asText() + "-" + common.get("sub-request-id").asText();
104 return correlationId;