AT&T 1712 and 1802 release code
[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 com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33 import org.openecomp.mso.logger.MsoLogger;
34
35 import com.fasterxml.jackson.annotation.JsonIgnore;
36 import com.fasterxml.jackson.annotation.JsonProperty;
37
38 /**
39  * Everything that is common between all Network Requests.
40  */
41 public abstract class NetworkRequestCommon {
42     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
43         private Boolean skipAAI = false;
44         private String messageId;
45         private String notificationUrl;
46         @JsonProperty
47         private boolean synchronous;
48         public Boolean getSkipAAI() {
49                 return skipAAI;
50         }
51
52         public void setSkipAAI(Boolean skipAAI) {
53                 this.skipAAI = skipAAI;
54         }
55
56         public String getMessageId() {
57                 return messageId;
58         }
59
60         public void setMessageId(String messageId) {
61                 this.messageId = messageId;
62         }
63
64         public String getNotificationUrl() {
65                 return notificationUrl;
66         }
67
68         public void setNotificationUrl(String notificationUrl) {
69                 this.notificationUrl = notificationUrl;
70         }
71
72         public boolean isSynchronous() {
73                 return notificationUrl == null || (notificationUrl.isEmpty());
74         }
75         
76         @JsonIgnore
77         public void setSynchronous(boolean synchronous) {
78                 this.synchronous = synchronous;
79         }
80
81         public String toJsonString() {
82                 String jsonString = null;
83                 try {
84                         ObjectMapper mapper = new ObjectMapper();
85                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
86                         jsonString = mapper.writeValueAsString(this);
87                 } catch (Exception e) {
88                     LOGGER.debug("Exception:", e);
89                 }
90                 return jsonString;
91         }
92
93         public String toXmlString() {
94                 try {
95                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
96                         JAXBContext context = JAXBContext.newInstance(this.getClass());
97                         Marshaller marshaller = context.createMarshaller();
98                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
99                         marshaller.marshal(this, bs);
100                         return bs.toString();
101                 } catch (Exception e) {
102                     LOGGER.debug("Exception:", e);
103                         return "";
104                 }
105         }
106 }