Update published event to include header and body
[aai/gizmo.git] / src / main / java / org / onap / crud / event / envelope / GraphEventEnvelope.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.event.envelope;
25
26 import javax.ws.rs.core.Response;
27 import org.onap.crud.event.GraphEvent;
28 import org.onap.crud.event.GraphEventEdge;
29 import org.onap.crud.event.GraphEventVertex;
30 import org.onap.crud.exception.CrudException;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35
36 /**
37  * Defines the top-level structure of the Gizmo events. Primarily serves as a wrapper for the graph details and includes
38  * an event header to describe and identify the event.
39  */
40 public class GraphEventEnvelope {
41
42     private GraphEventHeader header;
43     private GraphEvent body;
44     private JsonElement policyViolations;
45
46     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
47
48     /**
49      * Construct the event.
50      *
51      * @param header the event header to describe the event
52      * @param body the body of the event with graph details
53      */
54     public GraphEventEnvelope(GraphEventHeader header, GraphEvent body) {
55         this.header = header;
56         this.body = body;
57     }
58
59     /**
60      * Construct the envelope header from the provided GraphEvent.
61      *
62      * @param event the graph event for a vertex or edge operation
63      */
64     public GraphEventEnvelope(GraphEvent event) {
65         this.header = new GraphEventHeader.Builder().requestId(event.getTransactionId())
66                 .validationEntityType(getType(event)).build();
67         this.body = event;
68     }
69
70     public GraphEvent getBody() {
71         return body;
72     }
73
74     public GraphEventHeader getHeader() {
75         return header;
76     }
77
78     public JsonElement getPolicyViolations() {
79         return policyViolations != null ? policyViolations : new JsonArray();
80     }
81
82     /**
83      * Serializes this object into a JSON string representation.
84      *
85      * @return a JSON format string representation of this object.
86      */
87     public String toJson() {
88         return gson.toJson(this);
89     }
90
91     /**
92      * Deserializes a JSON string into a GraphEventEnvelope object.
93      *
94      * @param json the JSON string
95      * @return a GraphEventEnvelope object
96      * @throws CrudException
97      */
98     public static GraphEventEnvelope fromJson(String json) throws CrudException {
99         try {
100             if (json == null || json.isEmpty()) {
101                 throw new CrudException("Empty or null JSON string.", Response.Status.BAD_REQUEST);
102             }
103             return gson.fromJson(json, GraphEventEnvelope.class);
104         } catch (Exception ex) {
105             throw new CrudException("Unable to parse JSON string: " + json, Response.Status.BAD_REQUEST);
106         }
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see java.lang.Object#toString()
113      */
114     @Override
115     public String toString() {
116         return this.toJson();
117     }
118
119     private String getType(GraphEvent event) {
120         GraphEventVertex vertex = event.getVertex();
121         GraphEventEdge edge = event.getEdge();
122         if (vertex != null) {
123             return vertex.getType();
124         } else if (edge != null) {
125             return edge.getType();
126         }
127         return null;
128     }
129
130 }