Merge "Workflow Recipe Lookup"
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / 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  * Modifications Copyright (c) 2019 Samsung
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.sdncrest;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27
28 import javax.xml.bind.annotation.XmlElement;
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 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Base class for all SDNC adapter responses, including errors.
40  */
41 public abstract class SDNCResponseCommon implements Serializable {
42         private static final long serialVersionUID = 1L;
43         
44         private static final Logger logger = LoggerFactory.getLogger(SDNCResponseCommon.class);
45
46         // Identifies the MSO transaction with SDNC.
47         private String sdncRequestId;
48
49         // Response code, either from SDNC, or generated by the SDNC adapter.
50         // 2XX responses are considered success responses.
51         private String responseCode;
52
53         // Response message, either from SDNC, or generated by the SDNC adapter.
54         private String responseMessage;
55
56         // Indicates if the response is final (Y or N).
57         private String ackFinalIndicator;
58
59         public SDNCResponseCommon(String sdncRequestId, String responseCode,
60                         String responseMessage, String ackFinalIndicator) {
61                 this.sdncRequestId = sdncRequestId;
62                 this.responseCode = responseCode;
63                 this.responseMessage = responseMessage;
64                 this.ackFinalIndicator = ackFinalIndicator;
65         }
66
67         public SDNCResponseCommon() {
68         }
69
70         @JsonProperty("sdncRequestId")
71         @XmlElement(name = "sdncRequestId")
72         public String getSdncRequestId() {
73                 return sdncRequestId;
74         }
75
76         @JsonProperty("sdncRequestId")
77         public void setSdncRequestId(String sdncRequestId) {
78                 this.sdncRequestId = sdncRequestId;
79         }
80
81         @JsonProperty("responseCode")
82         @XmlElement(name = "responseCode")
83         public String getResponseCode() {
84                 return responseCode;
85         }
86
87         @JsonProperty("responseCode")
88         public void setResponseCode(String responseCode) {
89                 this.responseCode = responseCode;
90         }
91
92         @JsonProperty("responseMessage")
93         @XmlElement(name = "responseMessage")
94         public String getResponseMessage() {
95                 return responseMessage;
96         }
97
98         @JsonProperty("responseMessage")
99         public void setResponseMessage(String responseMessage) {
100                 this.responseMessage = responseMessage;
101         }
102
103         @JsonProperty("ackFinalIndicator")
104         @XmlElement(name = "ackFinalIndicator")
105         public String getAckFinalIndicator() {
106                 return ackFinalIndicator;
107         }
108
109         @JsonProperty("ackFinalIndicator")
110         public void setAckFinalIndicator(String ackFinalIndicator) {
111                 this.ackFinalIndicator = ackFinalIndicator;
112         }
113
114         public String toJson() {
115                 try {
116                         ObjectMapper mapper = new ObjectMapper();
117                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
118                         mapper.setSerializationInclusion(Include.NON_NULL);
119                         return mapper.writeValueAsString(this);
120                 } catch (IOException e) {
121                         logger.debug("Exception:", e);
122                         throw new UnsupportedOperationException("Cannot convert "
123                                 + getClass().getSimpleName() + " to JSON", e);
124                 }
125         }
126 }