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