Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / org / onap / vid / model / mso / MsoResponseWrapper2.java
1 package org.onap.vid.model.mso;
2
3 import com.fasterxml.jackson.annotation.JsonIgnore;
4 import com.fasterxml.jackson.annotation.JsonProperty;
5 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6 import com.fasterxml.jackson.core.JsonProcessingException;
7 import com.fasterxml.jackson.databind.ObjectMapper;
8
9 @JsonPropertyOrder({
10             "status",
11             "entity"
12 })
13
14 /*
15 This is a brother of MsoResponseWrapper. I (Ittay) think it's better.
16 It is generic, immutable, and has some knowledge about RestObject.
17 The serialized "entity" field may be either String or nested object.
18  */
19 public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
20
21     final static ObjectMapper objectMapper = new ObjectMapper();
22
23         private final int status;
24         private final T entity;
25     private final String raw;
26
27     public MsoResponseWrapper2(RestObject<T> msoResponse) {
28         this.status = msoResponse.getStatusCode();
29         this.entity = msoResponse.get();
30         this.raw = msoResponse.getRaw();
31     }
32
33     public MsoResponseWrapper2(
34             @JsonProperty(value = "status", required = true) int status,
35             @JsonProperty(value = "entity", required = true) T entity) {
36         this.status = status;
37         this.entity = entity;
38         this.raw = null;
39     }
40
41     public int getStatus() {
42                 return status;
43         }
44
45     @Override
46     @JsonIgnore
47     public String getResponse() {
48         try {
49             return objectMapper.writeValueAsString(this);
50         } catch (JsonProcessingException e) {
51             return getEntity() != null ? getEntity().toString() : null;
52         }
53     }
54
55     @JsonProperty
56         public Object getEntity() {
57                 return entity != null ? entity : raw;
58         }
59
60 }