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