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