6232af6f79377353b99d08fa98282e1ed060dad9
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / sdncrest / SDNCRequestCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
32 /**
33  * Base class for all SDNC adapter requests.
34  */
35 public abstract class SDNCRequestCommon implements Serializable {
36         private static final long serialVersionUID = 1L;
37
38         // Endpoint on which BPMN can receive notifications from the SDNC adapter.
39         private String bpNotificationUrl;
40
41         // BPMN flow timeout value in ISO 8601 format, e.g. PT5M.
42         // Not currently used by the SDNC adapter.
43         private String bpTimeout;
44
45         // Identifies the MSO transaction with SDNC.
46         // Maps to sdnc-request-header/requestId in the SDNC request.
47         private String sdncRequestId;
48
49         public SDNCRequestCommon(String sdncRequestId, String bpNotificationUrl,
50                         String bpTimeout) {
51                 this.sdncRequestId = sdncRequestId;
52                 this.bpNotificationUrl = bpNotificationUrl;
53                 this.bpTimeout = bpTimeout;
54         }
55
56         public SDNCRequestCommon() {
57         }
58
59         @JsonProperty("bpNotificationUrl")
60         @XmlElement(name = "bpNotificationUrl")
61         public String getBPNotificationUrl() {
62                 return bpNotificationUrl;
63         }
64
65         @JsonProperty("bpNotificationUrl")
66         public void setBPNotificationUrl(String bpNotificationUrl) {
67                 this.bpNotificationUrl = bpNotificationUrl;
68         }
69
70         @JsonProperty("bpTimeout")
71         @XmlElement(name = "bpTimeout")
72         public String getBPTimeout() {
73                 return bpTimeout;
74         }
75
76         @JsonProperty("bpTimeout")
77         public void setBPTimeout(String bpTimeout) {
78                 this.bpTimeout = bpTimeout;
79         }
80
81         @JsonProperty("sdncRequestId")
82         @XmlElement(name = "sdncRequestId")
83         public String getSDNCRequestId() {
84                 return sdncRequestId;
85         }
86
87         @JsonProperty("sdncRequestId")
88         public void setSDNCRequestId(String sdncRequestId) {
89                 this.sdncRequestId = sdncRequestId;
90         }
91
92         @JsonIgnore
93         public boolean isSynchronous() {
94                 return bpNotificationUrl == null || bpNotificationUrl.isEmpty();
95         }
96
97         public String toJson() {
98                 try {
99                         ObjectMapper mapper = new ObjectMapper();
100                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
101                         mapper.setSerializationInclusion(Inclusion.NON_NULL);
102                         return mapper.writeValueAsString(this);
103                 } catch (IOException e) {
104                         e.printStackTrace();
105                         throw new UnsupportedOperationException("Cannot convert "
106                                 + getClass().getSimpleName() + " to JSON", e);
107                 }
108         }
109 }