Merge "Reorder modifiers"
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / 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  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.adapters.sdncrest;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27
28 import javax.xml.bind.annotation.XmlElement;
29 import javax.xml.bind.annotation.XmlRootElement;
30
31 import org.openecomp.mso.logger.MsoLogger;
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
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 <entry><key>${key}</key><value>${value}</value></entry> elements.
46  When marshalling to JSON they create a list of "${key}" : "${value}" pairs with no extra wrappers.
47  * </pre>
48  */
49 @JsonRootName("SDNCEvent")
50 @JsonInclude(Include.NON_NULL)
51 @XmlRootElement(name = "SDNCEvent")
52 public class SDNCEvent implements Serializable {
53         private static final long serialVersionUID = 1L;
54         
55         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
56
57         // Event type
58         private String eventType;
59
60         // Event correlator type
61         private String eventCorrelatorType;
62
63         // Event correlator value.
64         private String eventCorrelator;
65
66         // Map of response parameters (possibly none).
67         private Map<String, String> params = null;
68
69         public SDNCEvent(String eventType, String eventCorrelatorType, String eventCorrelator) {
70                 this.eventType = eventType;
71                 this.eventCorrelatorType =  eventCorrelatorType;
72                 this.eventCorrelator =  eventCorrelator;
73         }
74
75         public SDNCEvent() {
76         }
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 "
138                                 + getClass().getSimpleName() + " to JSON", e);
139                 }
140         }
141 }