Second part of onap rename
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.client.lcm.impl.business;
26
27 import org.onap.appc.client.lcm.exceptions.AppcClientInternalException;
28
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;
34
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38
39 class CoreResponseHandler<T> {
40     private final Class<T> rpcOutput;
41     private final ObjectMapper mapper;
42
43     private final EELFLogger LOG = EELFManager.getInstance().getLogger(CoreResponseHandler.class);
44
45     CoreResponseHandler(Class<T> rpcOutput, ObjectMapper mapper) {
46         this.rpcOutput = rpcOutput;
47         this.mapper = mapper;
48     }
49
50     T getResponse(String message, String type, Boolean[] isFinal) throws AppcClientInternalException {
51         if (type.equals("error")) {
52             try {
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");
57             }
58             throw new AppcClientInternalException(message);
59         }
60         T responseObject = jsonToResponseObject(message);
61         int code = getStatusCode(responseObject);
62
63         int family = code / 100;
64
65         switch (family) {
66             case 1:
67             case 5:
68                 // not final
69                 break;
70             case 2:
71             case 3:
72             case 4:
73                 isFinal[0] = true;
74                 // final
75                 break;
76             default: // Should never happen
77                 throw new AppcClientInternalException(new IllegalStateException("Unsupported status code " + code + ". message: " + message));
78         }
79         return responseObject;
80
81     }
82
83     private T jsonToResponseObject(String message) throws AppcClientInternalException {
84         try {
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);
89         }
90     }
91
92     private int getStatusCode(Object response) throws AppcClientInternalException {
93         try {
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);
99         }
100     }
101
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;
106     }
107
108 }