2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.event.envelope;
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;
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.
37 public class GraphEventEnvelope {
39 private GraphEventHeader header;
40 private GraphEvent body;
41 private JsonElement policyViolations;
43 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
46 * Construct the event.
48 * @param header the event header to describe the event
49 * @param body the body of the event with graph details
51 public GraphEventEnvelope(GraphEventHeader header, GraphEvent body) {
57 * Construct the envelope header from the provided GraphEvent.
59 * @param event the graph event for a vertex or edge operation
61 public GraphEventEnvelope(GraphEvent event) {
62 this.header = new GraphEventHeader.Builder().requestId(event.getTransactionId())
63 .validationEntityType(getType(event)).build();
67 public GraphEvent getBody() {
71 public GraphEventHeader getHeader() {
75 public JsonElement getPolicyViolations() {
76 return policyViolations != null ? policyViolations : new JsonArray();
80 * Serializes this object into a JSON string representation.
82 * @return a JSON format string representation of this object.
84 public String toJson() {
85 return gson.toJson(this);
89 * Deserializes a JSON string into a GraphEventEnvelope object.
91 * @param json the JSON string
92 * @return a GraphEventEnvelope object
93 * @throws CrudException
95 public static GraphEventEnvelope fromJson(String json) throws CrudException {
97 if (json == null || json.isEmpty()) {
98 throw new CrudException("Empty or null JSON string.", Response.Status.BAD_REQUEST);
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);
109 * @see java.lang.Object#toString()
112 public String toString() {
113 return this.toJson();
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();