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