2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.event.envelope;
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;
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.
40 public class GraphEventEnvelope {
42 private GraphEventHeader header;
43 private GraphEvent body;
44 private JsonElement policyViolations;
46 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
49 * Construct the event.
51 * @param header the event header to describe the event
52 * @param body the body of the event with graph details
54 public GraphEventEnvelope(GraphEventHeader header, GraphEvent body) {
60 * Construct the envelope header from the provided GraphEvent.
62 * @param event the graph event for a vertex or edge operation
64 public GraphEventEnvelope(GraphEvent event) {
65 this.header = new GraphEventHeader.Builder().requestId(event.getTransactionId())
66 .validationEntityType(getType(event)).build();
70 public GraphEvent getBody() {
74 public GraphEventHeader getHeader() {
78 public JsonElement getPolicyViolations() {
79 return policyViolations != null ? policyViolations : new JsonArray();
83 * Serializes this object into a JSON string representation.
85 * @return a JSON format string representation of this object.
87 public String toJson() {
88 return gson.toJson(this);
92 * Deserializes a JSON string into a GraphEventEnvelope object.
94 * @param json the JSON string
95 * @return a GraphEventEnvelope object
96 * @throws CrudException
98 public static GraphEventEnvelope fromJson(String json) throws CrudException {
100 if (json == null || json.isEmpty()) {
101 throw new CrudException("Empty or null JSON string.", Response.Status.BAD_REQUEST);
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);
112 * @see java.lang.Object#toString()
115 public String toString() {
116 return this.toJson();
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();