More license header changes to appc-client files
[appc.git] / appc-client / client-kit / src / main / java / org / onap / appc / client / lcm / impl / business / CoreResponseHandler.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.lcm.exceptions.AppcClientInternalException;
27
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;
33
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37
38 class CoreResponseHandler<T> {
39     private final Class<T> rpcOutput;
40     private final ObjectMapper mapper;
41
42     private final EELFLogger LOG = EELFManager.getInstance().getLogger(CoreResponseHandler.class);
43
44     CoreResponseHandler(Class<T> rpcOutput, ObjectMapper mapper) {
45         this.rpcOutput = rpcOutput;
46         this.mapper = mapper;
47     }
48
49     T getResponse(String message, String type, Boolean[] isFinal) throws AppcClientInternalException {
50         if (type.equals("error")) {
51             try {
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");
56             }
57             throw new AppcClientInternalException(message);
58         }
59         T responseObject = jsonToResponseObject(message);
60         int code = getStatusCode(responseObject);
61
62         int family = code / 100;
63
64         switch (family) {
65             case 1:
66             case 5:
67                 // not final
68                 break;
69             case 2:
70             case 3:
71             case 4:
72                 isFinal[0] = true;
73                 // final
74                 break;
75             default: // Should never happen
76                 throw new AppcClientInternalException(new IllegalStateException("Unsupported status code " + code + ". message: " + message));
77         }
78         return responseObject;
79
80     }
81
82     private T jsonToResponseObject(String message) throws AppcClientInternalException {
83         try {
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);
88         }
89     }
90
91     private int getStatusCode(Object response) throws AppcClientInternalException {
92         try {
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);
98         }
99     }
100
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;
105     }
106
107 }