Merge "Added support for userdata and metadata"
[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 org.codehaus.jackson.annotate.JsonProperty;
24 import org.codehaus.jackson.map.ObjectMapper;
25 import org.codehaus.jackson.map.SerializationConfig;
26 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
27
28 import javax.xml.bind.annotation.XmlElement;
29 import java.io.IOException;
30 import java.io.Serializable;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 /**
34  * Base class for all SDNC adapter responses, including errors.
35  */
36 public abstract class SDNCResponseCommon implements Serializable {
37         private static final long serialVersionUID = 1L;
38         
39         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
40
41         // Identifies the MSO transaction with SDNC.
42         private String sdncRequestId;
43
44         // Response code, either from SDNC, or generated by the SDNC adapter.
45         // 2XX responses are considered success responses.
46         private String responseCode;
47
48         // Response message, either from SDNC, or generated by the SDNC adapter.
49         private String responseMessage;
50
51         // Indicates if the response is final (Y or N).
52         private String ackFinalIndicator;
53
54         public SDNCResponseCommon(String sdncRequestId, String responseCode,
55                         String responseMessage, String ackFinalIndicator) {
56                 this.sdncRequestId = sdncRequestId;
57                 this.responseCode = responseCode;
58                 this.responseMessage = responseMessage;
59                 this.ackFinalIndicator = ackFinalIndicator;
60         }
61
62         public SDNCResponseCommon() {
63         }
64
65         @JsonProperty("sdncRequestId")
66         @XmlElement(name = "sdncRequestId")
67         public String getSDNCRequestId() {
68                 return sdncRequestId;
69         }
70
71         @JsonProperty("sdncRequestId")
72         public void setSDNCRequestId(String sdncRequestId) {
73                 this.sdncRequestId = sdncRequestId;
74         }
75
76         @JsonProperty("responseCode")
77         @XmlElement(name = "responseCode")
78         public String getResponseCode() {
79                 return responseCode;
80         }
81
82         @JsonProperty("responseCode")
83         public void setResponseCode(String responseCode) {
84                 this.responseCode = responseCode;
85         }
86
87         @JsonProperty("responseMessage")
88         @XmlElement(name = "responseMessage")
89         public String getResponseMessage() {
90                 return responseMessage;
91         }
92
93         @JsonProperty("responseMessage")
94         public void setResponseMessage(String responseMessage) {
95                 this.responseMessage = responseMessage;
96         }
97
98         @JsonProperty("ackFinalIndicator")
99         @XmlElement(name = "ackFinalIndicator")
100         public String getAckFinalIndicator() {
101                 return ackFinalIndicator;
102         }
103
104         @JsonProperty("ackFinalIndicator")
105         public void setAckFinalIndicator(String ackFinalIndicator) {
106                 this.ackFinalIndicator = ackFinalIndicator;
107         }
108
109         public String toJson() {
110                 try {
111                         ObjectMapper mapper = new ObjectMapper();
112                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
113                         mapper.setSerializationInclusion(Inclusion.NON_NULL);
114                         return mapper.writeValueAsString(this);
115                 } catch (IOException e) {
116                     LOGGER.debug("Exception:", e);
117                         throw new UnsupportedOperationException("Cannot convert "
118                                 + getClass().getSimpleName() + " to JSON", e);
119                 }
120         }
121 }