AT&T 1712 and 1802 release code
[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 java.io.IOException;
23 import java.io.Serializable;
24
25 import javax.xml.bind.annotation.XmlElement;
26
27 import org.openecomp.mso.logger.MsoLogger;
28
29 import com.fasterxml.jackson.annotation.JsonIgnore;
30 import com.fasterxml.jackson.annotation.JsonInclude.Include;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.SerializationFeature;
34
35 /**
36  * Base class for all SDNC adapter requests.
37  */
38 public abstract class SDNCRequestCommon implements Serializable {
39         private static final long serialVersionUID = 1L;
40         
41         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
42
43         // Endpoint on which BPMN can receive notifications from the SDNC adapter.
44         private String bpNotificationUrl;
45
46         // BPMN flow timeout value in ISO 8601 format, e.g. PT5M.
47         // Not currently used by the SDNC adapter.
48         private String bpTimeout;
49
50         // Identifies the MSO transaction with SDNC.
51         // Maps to sdnc-request-header/requestId in the SDNC request.
52         private String sdncRequestId;
53
54         public SDNCRequestCommon(String sdncRequestId, String bpNotificationUrl,
55                         String bpTimeout) {
56                 this.sdncRequestId = sdncRequestId;
57                 this.bpNotificationUrl = bpNotificationUrl;
58                 this.bpTimeout = bpTimeout;
59         }
60
61         public SDNCRequestCommon() {
62         }
63
64         @JsonProperty("bpNotificationUrl")
65         @XmlElement(name = "bpNotificationUrl")
66         public String getBPNotificationUrl() {
67                 return bpNotificationUrl;
68         }
69
70         @JsonProperty("bpNotificationUrl")
71         public void setBPNotificationUrl(String bpNotificationUrl) {
72                 this.bpNotificationUrl = bpNotificationUrl;
73         }
74
75         @JsonProperty("bpTimeout")
76         @XmlElement(name = "bpTimeout")
77         public String getBPTimeout() {
78                 return bpTimeout;
79         }
80
81         @JsonProperty("bpTimeout")
82         public void setBPTimeout(String bpTimeout) {
83                 this.bpTimeout = bpTimeout;
84         }
85
86         @JsonProperty("sdncRequestId")
87         @XmlElement(name = "sdncRequestId")
88         public String getSDNCRequestId() {
89                 return sdncRequestId;
90         }
91
92         @JsonProperty("sdncRequestId")
93         public void setSDNCRequestId(String sdncRequestId) {
94                 this.sdncRequestId = sdncRequestId;
95         }
96
97         @JsonIgnore
98         public boolean isSynchronous() {
99                 return bpNotificationUrl == null || bpNotificationUrl.isEmpty();
100         }
101
102         public String toJson() {
103                 try {
104                         ObjectMapper mapper = new ObjectMapper();
105                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
106                         mapper.setSerializationInclusion(Include.NON_NULL);
107                         return mapper.writeValueAsString(this);
108                 } catch (IOException e) {
109                     LOGGER.debug("Exception:", e);
110                         throw new UnsupportedOperationException("Cannot convert "
111                                 + getClass().getSimpleName() + " to JSON", e);
112                 }
113         }
114 }