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.openecomp.appc.simulator.client.impl;
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;
35 import java.io.FileWriter;
36 import java.util.concurrent.atomic.AtomicInteger;
38 public class JsonResponseHandler implements ResponseHandler<Object> {
40 public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
41 private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonResponseHandler.class);
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;
48 private AtomicInteger messageCount =new AtomicInteger(1);
50 public void setFile(String name) {
55 public void onResponse(Object response) {
59 output = OBJECT_MAPPER.writeValueAsString(response);
60 } catch (JsonProcessingException e) {
63 LOG.debug("Received response : " + output);
66 boolean isFinal = true;
68 JsonNode code = OBJECT_MAPPER.readTree(output).findValue("status").findValue("code");
71 LOG.error("Status code doesn't exist. Malformed response : " + output);
72 flushToErrorFile(output, isFinal);
75 errorCode = code.asInt();
76 errorCode = errorCode / 100;
79 isFinal = false; // for ACCEPT that it is not a final response
81 case INTERMEDIATE_MESSAGES:
82 isFinal = false; // for 5xx series messages are not a final response
88 } catch (Exception e) {
90 throw new RuntimeException(e);
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) {
100 throw new RuntimeException(e);
102 // no need to report ACCEPT into output file
106 flushToOutputFile(output, isFinal);
108 case INTERMEDIATE_MESSAGES:
109 flushToMessageFile(output, isFinal);
112 flushToErrorFile(output, isFinal);
117 public void onException(AppcClientException exception) {
118 flushToErrorFile("exception: " + exception, true);
121 private void flushToOutputFile(String output, boolean isFinal) {
122 this.flushToFile(output, ".output", isFinal);
124 private void flushToMessageFile(String output, boolean isFinal) {
125 this.flushToFile(output, ".message" + messageCount.getAndIncrement(), isFinal);
129 private void flushToErrorFile(String output, boolean isFinal) {
130 this.flushToFile(output, ".error", isFinal);
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);
137 fileWriter.write(output);
142 } catch (Exception e) {
145 System.out.println("== THR#" +Thread.currentThread().getId()+ " Output file : " + fileName + suffix);