d1907a237e043085aa2916b69a7e296f94725e4e
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / nwrest / NetworkResponseCommon.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.nwrest;
24
25
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.Serializable;
29
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.Marshaller;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.SerializationFeature;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Everything that is common between all Volume Group Responses, except for QueryVolumeGroupResponse.
40  */
41 public abstract class NetworkResponseCommon implements Serializable {
42
43         private static final long serialVersionUID = 1233520856935129726L;
44         private String messageId;
45         private static final Logger logger = LoggerFactory.getLogger(NetworkResponseCommon.class);
46
47         public NetworkResponseCommon() {
48                 messageId = null;
49         }
50
51         public NetworkResponseCommon(String messageId) {
52                 this.messageId = messageId;
53         }
54
55         public String getMessageId() {
56                 return messageId;
57         }
58
59         public void setMessageId(String messageId) {
60                 this.messageId = messageId;
61         }
62
63         public String toJsonString() {
64                 String jsonString = null;
65                 try {
66                         ObjectMapper mapper = new ObjectMapper();
67                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
68                         jsonString = mapper.writeValueAsString(this);
69                 } catch (Exception e) {
70                         logger.debug("Exception:", e);
71                 }
72                 return jsonString;
73         }
74
75         public String toXmlString() {
76                 try {
77                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
78                         JAXBContext context = JAXBContext.newInstance(this.getClass());
79                         Marshaller marshaller = context.createMarshaller();
80                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
81                         marshaller.marshal(this, bs);
82                         return bs.toString();
83                 } catch (Exception e) {
84                         logger.debug("Exception:", e);
85                         return "";
86                 }
87         }
88 }