d9b09fa1b06664b7e73333a15b2e5fb6d085d038
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / nwrest / NetworkRequestCommon.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.nwrest;
23
24
25
26 import java.io.ByteArrayOutputStream;
27
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.codehaus.jackson.map.SerializationConfig;
33 import org.openecomp.mso.logger.MsoLogger;
34
35 /**
36  * Everything that is common between all Network Requests.
37  */
38 public abstract class NetworkRequestCommon {
39     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
40         private Boolean skipAAI = false;
41         private String messageId;
42         private String notificationUrl;
43
44         public Boolean getSkipAAI() {
45                 return skipAAI;
46         }
47
48         public void setSkipAAI(Boolean skipAAI) {
49                 this.skipAAI = skipAAI;
50         }
51
52         public String getMessageId() {
53                 return messageId;
54         }
55
56         public void setMessageId(String messageId) {
57                 this.messageId = messageId;
58         }
59
60         public String getNotificationUrl() {
61                 return notificationUrl;
62         }
63
64         public void setNotificationUrl(String notificationUrl) {
65                 this.notificationUrl = notificationUrl;
66         }
67
68         public boolean isSynchronous() {
69                 return notificationUrl == null || (notificationUrl.isEmpty());
70         }
71
72         public String toJsonString() {
73                 String jsonString = null;
74                 try {
75                         ObjectMapper mapper = new ObjectMapper();
76                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
77                         jsonString = mapper.writeValueAsString(this);
78                 } catch (Exception e) {
79                     LOGGER.debug("Exception:", e);
80                 }
81                 return jsonString;
82         }
83
84         public String toXmlString() {
85                 try {
86                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
87                         JAXBContext context = JAXBContext.newInstance(this.getClass());
88                         Marshaller marshaller = context.createMarshaller();
89                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
90                         marshaller.marshal(this, bs);
91                         return bs.toString();
92                 } catch (Exception e) {
93                     LOGGER.debug("Exception:", e);
94                         return "";
95                 }
96         }
97 }