f577fa9b59f46410c0750247c208423956d2da67
[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
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.XmlRootElement;
32
33 import com.fasterxml.jackson.annotation.JsonInclude;
34 import com.fasterxml.jackson.annotation.JsonInclude.Include;
35 import com.fasterxml.jackson.annotation.JsonProperty;
36 import com.fasterxml.jackson.annotation.JsonRootName;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.SerializationFeature;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 // NOTE: the JAXB (XML) annotations are required with JBoss AS7 and RESTEasy,
43 //       even though we are using JSON exclusively.  The @NoJackson annotation
44 //       is also required in this environment.
45
46 /**
47  Map<String, String> elements when marshalled to XML produce a list of <entry><key>${MsoUtils.xmlEscape(key)}</key><value>${MsoUtils.xmlEscape(value)}</value></entry> elements.
48  When marshalling to JSON they create a list of "${key}" : "${value}" pairs with no extra wrappers.
49  * </pre>
50  */
51 @JsonRootName("SDNCEvent")
52 @JsonInclude(Include.NON_NULL)
53 @XmlRootElement(name = "SDNCEvent")
54 public class SDNCEvent implements Serializable {
55         private static final long serialVersionUID = 1L;
56         
57         private static final Logger logger = LoggerFactory.getLogger(SDNCEvent.class);
58
59         // Event type
60         private String eventType;
61
62         // Event correlator type
63         private String eventCorrelatorType;
64
65         // Event correlator value.
66         private String eventCorrelator;
67
68         // Map of response parameters (possibly none).
69         private Map<String, String> params = null;
70
71         public SDNCEvent(String eventType, String eventCorrelatorType, String eventCorrelator) {
72                 this.eventType = eventType;
73                 this.eventCorrelatorType =  eventCorrelatorType;
74                 this.eventCorrelator =  eventCorrelator;
75         }
76
77         public SDNCEvent() {
78         }
79
80         @JsonProperty("eventType")
81         @XmlElement(name = "eventType")
82         public String getEventType() {
83                 return eventType;
84         }
85
86         @JsonProperty("eventType")
87         public void setEventType(String eventType) {
88                 this.eventType = eventType;
89         }
90
91         @JsonProperty("eventCorrelatorType")
92         @XmlElement(name = "eventCorrelatorType")
93         public String getEventCorrelatorType() {
94                 return eventCorrelatorType;
95         }
96
97         @JsonProperty("eventCorrelatorType")
98         public void setEventCorrelatorType(String eventCorrelatorType) {
99                 this.eventCorrelatorType = eventCorrelatorType;
100         }
101
102         @JsonProperty("eventCorrelator")
103         @XmlElement(name = "eventCorrelator")
104         public String getEventCorrelator() {
105                 return eventCorrelator;
106         }
107
108         @JsonProperty("eventCorrelator")
109         public void setEventCorrelator(String eventCorrelator) {
110                 this.eventCorrelator = eventCorrelator;
111         }
112
113         @JsonProperty("params")
114         @XmlElement(name = "params")
115         public Map<String, String> getParams() {
116                 return params;
117         }
118
119         @JsonProperty("params")
120         public void setParams(Map<String, String> params) {
121                 this.params = params;
122         }
123
124         public void addParam(String name, String value) {
125                 if (params == null) {
126                         params = new LinkedHashMap<>();
127                 }
128                 params.put(name, value);
129         }
130
131         public String toJson() {
132                 try {
133                         ObjectMapper mapper = new ObjectMapper();
134                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
135                         mapper.setSerializationInclusion(Include.NON_NULL);
136                         return mapper.writeValueAsString(this);
137                 } catch (IOException e) {
138                         logger.debug("Exception:", e);
139                         throw new UnsupportedOperationException("Cannot convert "
140                                 + getClass().getSimpleName() + " to JSON", e);
141                 }
142         }
143 }