Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / sdncrest / SDNCEvent.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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.sdncrest;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.XmlRootElement;
31 import com.fasterxml.jackson.annotation.JsonInclude;
32 import com.fasterxml.jackson.annotation.JsonInclude.Include;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34 import com.fasterxml.jackson.annotation.JsonRootName;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 // NOTE: the JAXB (XML) annotations are required with JBoss AS7 and RESTEasy,
41 // even though we are using JSON exclusively. The @NoJackson annotation
42 // is also required in this environment.
43
44 /**
45  * Map<String, String> elements when marshalled to XML produce a list of
46  * <entry><key>${MsoUtils.xmlEscape(key)}</key><value>${MsoUtils.xmlEscape(value)}</value></entry> elements. When
47  * marshalling to JSON they create a list of "${key}" : "${value}" pairs with no extra wrappers.
48  * </pre>
49  */
50 @JsonRootName("SDNCEvent")
51 @JsonInclude(Include.NON_NULL)
52 @XmlRootElement(name = "SDNCEvent")
53 public class SDNCEvent implements Serializable {
54     private static final long serialVersionUID = 1L;
55
56     private static final Logger logger = LoggerFactory.getLogger(SDNCEvent.class);
57
58     // Event type
59     private String eventType;
60
61     // Event correlator type
62     private String eventCorrelatorType;
63
64     // Event correlator value.
65     private String eventCorrelator;
66
67     // Map of response parameters (possibly none).
68     private Map<String, String> params = null;
69
70     public SDNCEvent(String eventType, String eventCorrelatorType, String eventCorrelator) {
71         this.eventType = eventType;
72         this.eventCorrelatorType = eventCorrelatorType;
73         this.eventCorrelator = eventCorrelator;
74     }
75
76     public SDNCEvent() {}
77
78     @JsonProperty("eventType")
79     @XmlElement(name = "eventType")
80     public String getEventType() {
81         return eventType;
82     }
83
84     @JsonProperty("eventType")
85     public void setEventType(String eventType) {
86         this.eventType = eventType;
87     }
88
89     @JsonProperty("eventCorrelatorType")
90     @XmlElement(name = "eventCorrelatorType")
91     public String getEventCorrelatorType() {
92         return eventCorrelatorType;
93     }
94
95     @JsonProperty("eventCorrelatorType")
96     public void setEventCorrelatorType(String eventCorrelatorType) {
97         this.eventCorrelatorType = eventCorrelatorType;
98     }
99
100     @JsonProperty("eventCorrelator")
101     @XmlElement(name = "eventCorrelator")
102     public String getEventCorrelator() {
103         return eventCorrelator;
104     }
105
106     @JsonProperty("eventCorrelator")
107     public void setEventCorrelator(String eventCorrelator) {
108         this.eventCorrelator = eventCorrelator;
109     }
110
111     @JsonProperty("params")
112     @XmlElement(name = "params")
113     public Map<String, String> getParams() {
114         return params;
115     }
116
117     @JsonProperty("params")
118     public void setParams(Map<String, String> params) {
119         this.params = params;
120     }
121
122     public void addParam(String name, String value) {
123         if (params == null) {
124             params = new LinkedHashMap<>();
125         }
126         params.put(name, value);
127     }
128
129     public String toJson() {
130         try {
131             ObjectMapper mapper = new ObjectMapper();
132             mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
133             mapper.setSerializationInclusion(Include.NON_NULL);
134             return mapper.writeValueAsString(this);
135         } catch (IOException e) {
136             logger.debug("Exception:", e);
137             throw new UnsupportedOperationException("Cannot convert " + getClass().getSimpleName() + " to JSON", e);
138         }
139     }
140 }