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