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