5f14bb84993b6584287df577028feed938ac18e3
[appc.git] / appc-client / client-simulator / src / main / java / org / openecomp / appc / simulator / client / impl / JsonResponseHandler.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.openecomp.appc.simulator.client.impl;
26
27 import org.openecomp.appc.client.lcm.api.ResponseHandler;
28 import org.openecomp.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             e.printStackTrace();
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             e.printStackTrace();
90             throw new RuntimeException(e);
91         }
92
93         switch (errorCode) {
94             case ACCEPT_FAMILY: {
95                 try {
96                     System.out.println("== THR#" + Thread.currentThread().getId() + " Got ACCEPT on ReqID <" +
97                             OBJECT_MAPPER.readTree(output).findValue("common-header").findValue("request-id").asText() + ">");
98                 } catch (Exception e) {
99                     e.printStackTrace();
100                     throw new RuntimeException(e);
101                 }
102                 // no need to report ACCEPT into output file
103                 break;
104             }
105             case SUCCESS_FAMILY:
106                 flushToOutputFile(output, isFinal);
107                 break;
108             case INTERMEDIATE_MESSAGES:
109                 flushToMessageFile(output, isFinal);
110                 break;
111             default:
112                 flushToErrorFile(output, isFinal);
113         }
114     }
115
116     @Override
117     public void onException(AppcClientException exception) {
118         flushToErrorFile("exception: " + exception, true);
119     }
120
121     private void flushToOutputFile(String output, boolean isFinal) {
122         this.flushToFile(output, ".output", isFinal);
123     }
124     private void flushToMessageFile(String output, boolean isFinal) {
125         this.flushToFile(output, ".message" + messageCount.getAndIncrement(), isFinal);
126
127     }
128
129     private void flushToErrorFile(String output, boolean isFinal) {
130         this.flushToFile(output, ".error", isFinal);
131     }
132
133     private   void flushToFile(String output, String suffix, boolean isFinal) {
134         try (FileWriter fileWriter = new FileWriter(fileName + suffix);){
135             LOG.info("Output file : " + fileName + suffix);
136
137             fileWriter.write(output);
138             fileWriter.flush();
139             if (isFinal){
140                 fileWriter.close();
141             }
142         } catch (Exception e) {
143             e.printStackTrace();
144         }
145         System.out.println("== THR#" +Thread.currentThread().getId()+ " Output file : " + fileName + suffix);
146     }
147 }