00b22dbaa76d38f2b165eb24a0642504e2c63b37
[appc.git] /
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.simulator.client.impl;
26
27 import org.onap.appc.client.lcm.api.ResponseHandler;
28 import org.onap.appc.client.lcm.exceptions.AppcClientException;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 import java.io.FileWriter;
36 import java.util.concurrent.atomic.AtomicInteger;
37
38 public class JsonResponseHandler implements ResponseHandler<Object> {
39
40     public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
41     private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonResponseHandler.class);
42
43     private String fileName = "default";
44     private static final int ACCEPT_FAMILY = 1;
45     private static final int SUCCESS_FAMILY = 4;
46     private static final int INTERMEDIATE_MESSAGES =5;
47
48     private AtomicInteger messageCount =new AtomicInteger(1);
49
50     public void setFile(String name) {
51         fileName = name;
52     }
53
54     @Override
55     public void onResponse(Object response) {
56
57         String output = null;
58         try {
59             output = OBJECT_MAPPER.writeValueAsString(response);
60         } catch (JsonProcessingException e) {
61             LOG.error(e.getMessage());
62         }
63         LOG.debug("Received response : " + output);
64
65         int errorCode = 0;
66         boolean isFinal = true;
67         try {
68             JsonNode code= OBJECT_MAPPER.readTree(output).findValue("status").findValue("code");
69             if (code == null)
70             {
71                 LOG.error("Status code doesn't exist. Malformed response : " + output);
72                 flushToErrorFile(output, isFinal);
73                 return;
74             }
75             errorCode = code.asInt();
76             errorCode = errorCode / 100;
77             switch (errorCode) {
78                 case ACCEPT_FAMILY:
79                     isFinal = false; // for ACCEPT that it is not a final response
80                     break;
81                 case INTERMEDIATE_MESSAGES:
82                     isFinal = false; // for 5xx series messages are not a final response
83                     break;
84                 default:
85                     break;
86             }
87
88         } catch (Exception e) {
89             throw new RuntimeException(e);
90         }
91
92         switch (errorCode) {
93             case ACCEPT_FAMILY: {
94                 try {
95                     System.out.println("== THR#" + Thread.currentThread().getId() + " Got ACCEPT on ReqID <" +
96                             OBJECT_MAPPER.readTree(output).findValue("common-header").findValue("request-id").asText() + ">");
97                 } catch (Exception e) {
98                     e.printStackTrace();
99                     throw new RuntimeException(e);
100                 }
101                 // no need to report ACCEPT into output file
102                 break;
103             }
104             case SUCCESS_FAMILY:
105                 flushToOutputFile(output, isFinal);
106                 break;
107             case INTERMEDIATE_MESSAGES:
108                 flushToMessageFile(output, isFinal);
109                 break;
110             default:
111                 flushToErrorFile(output, isFinal);
112         }
113     }
114
115     @Override
116     public void onException(AppcClientException exception) {
117         flushToErrorFile("exception: " + exception, true);
118     }
119
120     private void flushToOutputFile(String output, boolean isFinal) {
121         this.flushToFile(output, ".output", isFinal);
122     }
123     private void flushToMessageFile(String output, boolean isFinal) {
124         this.flushToFile(output, ".message" + messageCount.getAndIncrement(), isFinal);
125
126     }
127
128     private void flushToErrorFile(String output, boolean isFinal) {
129         this.flushToFile(output, ".error", isFinal);
130     }
131
132     private   void flushToFile(String output, String suffix, boolean isFinal) {
133         try (FileWriter fileWriter = new FileWriter(fileName + suffix);){
134             LOG.info("Output file : " + fileName + suffix);
135
136             fileWriter.write(output);
137             fileWriter.flush();
138             if (isFinal){
139                 fileWriter.close();
140             }
141         } catch (Exception e) {
142             LOG.error(e.getMessage());
143         }
144         System.out.println("== THR#" +Thread.currentThread().getId()+ " Output file : " + fileName + suffix);
145     }
146 }