Containerization feature of SO
[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  * ================================================================================
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
22 package org.onap.so.adapters.sdncrest;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.XmlRootElement;
31
32 import org.onap.so.logger.MsoLogger;
33
34 import com.fasterxml.jackson.annotation.JsonInclude;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.annotation.JsonProperty;
37 import com.fasterxml.jackson.annotation.JsonRootName;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.SerializationFeature;
40
41 // NOTE: the JAXB (XML) annotations are required with JBoss AS7 and RESTEasy,
42 //       even though we are using JSON exclusively.  The @NoJackson annotation
43 //       is also required in this environment.
44
45 /**
46  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.
47  When 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 MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, 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
79         @JsonProperty("eventType")
80         @XmlElement(name = "eventType")
81         public String getEventType() {
82                 return eventType;
83         }
84
85         @JsonProperty("eventType")
86         public void setEventType(String eventType) {
87                 this.eventType = eventType;
88         }
89
90         @JsonProperty("eventCorrelatorType")
91         @XmlElement(name = "eventCorrelatorType")
92         public String getEventCorrelatorType() {
93                 return eventCorrelatorType;
94         }
95
96         @JsonProperty("eventCorrelatorType")
97         public void setEventCorrelatorType(String eventCorrelatorType) {
98                 this.eventCorrelatorType = eventCorrelatorType;
99         }
100
101         @JsonProperty("eventCorrelator")
102         @XmlElement(name = "eventCorrelator")
103         public String getEventCorrelator() {
104                 return eventCorrelator;
105         }
106
107         @JsonProperty("eventCorrelator")
108         public void setEventCorrelator(String eventCorrelator) {
109                 this.eventCorrelator = eventCorrelator;
110         }
111
112         @JsonProperty("params")
113         @XmlElement(name = "params")
114         public Map<String, String> getParams() {
115                 return params;
116         }
117
118         @JsonProperty("params")
119         public void setParams(Map<String, String> params) {
120                 this.params = params;
121         }
122
123         public void addParam(String name, String value) {
124                 if (params == null) {
125                         params = new LinkedHashMap<>();
126                 }
127                 params.put(name, value);
128         }
129
130         public String toJson() {
131                 try {
132                         ObjectMapper mapper = new ObjectMapper();
133                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
134                         mapper.setSerializationInclusion(Include.NON_NULL);
135                         return mapper.writeValueAsString(this);
136                 } catch (IOException e) {
137                     LOGGER.debug("Exception:", e);
138                         throw new UnsupportedOperationException("Cannot convert "
139                                 + getClass().getSimpleName() + " to JSON", e);
140                 }
141         }
142 }