45609168eefa1e7b0db1416b4183fde9e02fe3f8
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / sdncrest / SDNCRequestCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.adapters.sdncrest;
21
22 import org.codehaus.jackson.annotate.JsonIgnore;
23 import org.codehaus.jackson.annotate.JsonProperty;
24 import org.codehaus.jackson.map.ObjectMapper;
25 import org.codehaus.jackson.map.SerializationConfig;
26 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
27
28 import javax.xml.bind.annotation.XmlElement;
29 import java.io.IOException;
30 import java.io.Serializable;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 /**
34  * Base class for all SDNC adapter requests.
35  */
36 public abstract class SDNCRequestCommon implements Serializable {
37         private static final long serialVersionUID = 1L;
38         
39         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
40
41         // Endpoint on which BPMN can receive notifications from the SDNC adapter.
42         private String bpNotificationUrl;
43
44         // BPMN flow timeout value in ISO 8601 format, e.g. PT5M.
45         // Not currently used by the SDNC adapter.
46         private String bpTimeout;
47
48         // Identifies the MSO transaction with SDNC.
49         // Maps to sdnc-request-header/requestId in the SDNC request.
50         private String sdncRequestId;
51
52         public SDNCRequestCommon(String sdncRequestId, String bpNotificationUrl,
53                         String bpTimeout) {
54                 this.sdncRequestId = sdncRequestId;
55                 this.bpNotificationUrl = bpNotificationUrl;
56                 this.bpTimeout = bpTimeout;
57         }
58
59         public SDNCRequestCommon() {
60         }
61
62         @JsonProperty("bpNotificationUrl")
63         @XmlElement(name = "bpNotificationUrl")
64         public String getBPNotificationUrl() {
65                 return bpNotificationUrl;
66         }
67
68         @JsonProperty("bpNotificationUrl")
69         public void setBPNotificationUrl(String bpNotificationUrl) {
70                 this.bpNotificationUrl = bpNotificationUrl;
71         }
72
73         @JsonProperty("bpTimeout")
74         @XmlElement(name = "bpTimeout")
75         public String getBPTimeout() {
76                 return bpTimeout;
77         }
78
79         @JsonProperty("bpTimeout")
80         public void setBPTimeout(String bpTimeout) {
81                 this.bpTimeout = bpTimeout;
82         }
83
84         @JsonProperty("sdncRequestId")
85         @XmlElement(name = "sdncRequestId")
86         public String getSDNCRequestId() {
87                 return sdncRequestId;
88         }
89
90         @JsonProperty("sdncRequestId")
91         public void setSDNCRequestId(String sdncRequestId) {
92                 this.sdncRequestId = sdncRequestId;
93         }
94
95         @JsonIgnore
96         public boolean isSynchronous() {
97                 return bpNotificationUrl == null || bpNotificationUrl.isEmpty();
98         }
99
100         public String toJson() {
101                 try {
102                         ObjectMapper mapper = new ObjectMapper();
103                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
104                         mapper.setSerializationInclusion(Inclusion.NON_NULL);
105                         return mapper.writeValueAsString(this);
106                 } catch (IOException e) {
107                     LOGGER.debug("Exception:", e);
108                         throw new UnsupportedOperationException("Cannot convert "
109                                 + getClass().getSimpleName() + " to JSON", e);
110                 }
111         }
112 }