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