aa0be2702b270ca5e105d2b119308d2143ea4732
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / sdncrest / SDNCResponseCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.adapters.sdncrest;
21
22 import org.codehaus.jackson.annotate.JsonProperty;
23 import org.codehaus.jackson.map.ObjectMapper;
24 import org.codehaus.jackson.map.SerializationConfig;
25 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
26
27 import javax.xml.bind.annotation.XmlElement;
28 import java.io.IOException;
29 import java.io.Serializable;
30
31 /**
32  * Base class for all SDNC adapter responses, including errors.
33  */
34 public abstract class SDNCResponseCommon implements Serializable {
35         private static final long serialVersionUID = 1L;
36
37         // Identifies the MSO transaction with SDNC.
38         private String sdncRequestId;
39
40         // Response code, either from SDNC, or generated by the SDNC adapter.
41         // 2XX responses are considered success responses.
42         private String responseCode;
43
44         // Response message, either from SDNC, or generated by the SDNC adapter.
45         private String responseMessage;
46
47         // Indicates if the response is final (Y or N).
48         private String ackFinalIndicator;
49
50         public SDNCResponseCommon(String sdncRequestId, String responseCode,
51                         String responseMessage, String ackFinalIndicator) {
52                 this.sdncRequestId = sdncRequestId;
53                 this.responseCode = responseCode;
54                 this.responseMessage = responseMessage;
55                 this.ackFinalIndicator = ackFinalIndicator;
56         }
57
58         public SDNCResponseCommon() {
59         }
60
61         @JsonProperty("sdncRequestId")
62         @XmlElement(name = "sdncRequestId")
63         public String getSDNCRequestId() {
64                 return sdncRequestId;
65         }
66
67         @JsonProperty("sdncRequestId")
68         public void setSDNCRequestId(String sdncRequestId) {
69                 this.sdncRequestId = sdncRequestId;
70         }
71
72         @JsonProperty("responseCode")
73         @XmlElement(name = "responseCode")
74         public String getResponseCode() {
75                 return responseCode;
76         }
77
78         @JsonProperty("responseCode")
79         public void setResponseCode(String responseCode) {
80                 this.responseCode = responseCode;
81         }
82
83         @JsonProperty("responseMessage")
84         @XmlElement(name = "responseMessage")
85         public String getResponseMessage() {
86                 return responseMessage;
87         }
88
89         @JsonProperty("responseMessage")
90         public void setResponseMessage(String responseMessage) {
91                 this.responseMessage = responseMessage;
92         }
93
94         @JsonProperty("ackFinalIndicator")
95         @XmlElement(name = "ackFinalIndicator")
96         public String getAckFinalIndicator() {
97                 return ackFinalIndicator;
98         }
99
100         @JsonProperty("ackFinalIndicator")
101         public void setAckFinalIndicator(String ackFinalIndicator) {
102                 this.ackFinalIndicator = ackFinalIndicator;
103         }
104
105         public String toJson() {
106                 try {
107                         ObjectMapper mapper = new ObjectMapper();
108                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
109                         mapper.setSerializationInclusion(Inclusion.NON_NULL);
110                         return mapper.writeValueAsString(this);
111                 } catch (IOException e) {
112                         e.printStackTrace();
113                         throw new UnsupportedOperationException("Cannot convert "
114                                 + getClass().getSimpleName() + " to JSON", e);
115                 }
116         }
117 }