[MSO-8] Update the maven dependency
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / sdncrest / SDNCEvent.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.openecomp.mso.adapters.json.MapDeserializer;
23 import org.openecomp.mso.adapters.json.MapSerializer;
24 import org.codehaus.jackson.annotate.JsonProperty;
25 import org.codehaus.jackson.map.ObjectMapper;
26 import org.codehaus.jackson.map.SerializationConfig;
27 import org.codehaus.jackson.map.annotate.JsonDeserialize;
28 import org.codehaus.jackson.map.annotate.JsonRootName;
29 import org.codehaus.jackson.map.annotate.JsonSerialize;
30 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
31 import org.jboss.resteasy.annotations.providers.NoJackson;
32
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import java.io.IOException;
36 import java.io.Serializable;
37 import java.util.LinkedHashMap;
38 import java.util.Map;
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  * SDNC adapter success response for "agnostic" API services. Note that the
46  * map of response parameters is represented this way in JSON:
47  * <pre>
48  * "params": {
49  *   "entry": [
50  *     {"key": "P1", "value": "V1"},
51  *     {"key": "P2", "value": "V2"},
52  *     ...
53  *     {"key": "PN", "value": "VN"}
54  *   ]
55  * }
56  * </pre>
57  */
58 @JsonRootName("SDNCEvent")
59 @JsonSerialize(include= Inclusion.NON_NULL)
60 @XmlRootElement(name = "SDNCEvent")
61 @NoJackson
62 public class SDNCEvent implements Serializable {
63         private static final long serialVersionUID = 1L;
64
65         // Event type
66         private String eventType;
67
68         // Event correlator type
69         private String eventCorrelatorType;
70
71         // Event correlator value.
72         private String eventCorrelator;
73
74         // Map of response parameters (possibly none).
75         private Map<String, String> params = null;
76
77         public SDNCEvent(String eventType, String eventCorrelatorType, String eventCorrelator) {
78                 this.eventType = eventType;
79                 this.eventCorrelatorType =  eventCorrelatorType;
80                 this.eventCorrelator =  eventCorrelator;
81         }
82
83         public SDNCEvent() {
84         }
85
86         @JsonProperty("eventType")
87         @XmlElement(name = "eventType")
88         public String getEventType() {
89                 return eventType;
90         }
91
92         @JsonProperty("eventType")
93         public void setEventType(String eventType) {
94                 this.eventType = eventType;
95         }
96
97         @JsonProperty("eventCorrelatorType")
98         @XmlElement(name = "eventCorrelatorType")
99         public String getEventCorrelatorType() {
100                 return eventCorrelatorType;
101         }
102
103         @JsonProperty("eventCorrelatorType")
104         public void setEventCorrelatorType(String eventCorrelatorType) {
105                 this.eventCorrelatorType = eventCorrelatorType;
106         }
107
108         @JsonProperty("eventCorrelator")
109         @XmlElement(name = "eventCorrelator")
110         public String getEventCorrelator() {
111                 return eventCorrelator;
112         }
113
114         @JsonProperty("eventCorrelator")
115         public void setEventCorrelator(String eventCorrelator) {
116                 this.eventCorrelator = eventCorrelator;
117         }
118
119         @JsonProperty("params")
120         @JsonDeserialize(using = MapDeserializer.class)
121         @XmlElement(name = "params")
122         public Map<String, String> getParams() {
123                 return params;
124         }
125
126         @JsonProperty("params")
127         @JsonSerialize(using = MapSerializer.class, include= Inclusion.NON_NULL)
128         public void setParams(Map<String, String> params) {
129                 this.params = params;
130         }
131
132         public void addParam(String name, String value) {
133                 if (params == null) {
134                         params = new LinkedHashMap<String, String>();
135                 }
136                 params.put(name, value);
137         }
138
139         public String toJson() {
140                 try {
141                         ObjectMapper mapper = new ObjectMapper();
142                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
143                         mapper.setSerializationInclusion(Inclusion.NON_NULL);
144                         return mapper.writeValueAsString(this);
145                 } catch (IOException e) {
146                         e.printStackTrace();
147                         throw new UnsupportedOperationException("Cannot convert "
148                                 + getClass().getSimpleName() + " to JSON", e);
149                 }
150         }
151 }