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