Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / event / envelope / EventEnvelope.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.aai.spike.event.envelope;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.onap.aai.spike.event.outgoing.SpikeGraphEvent;
26
27 public class EventEnvelope {
28
29     private EventHeader header;
30     private SpikeGraphEvent body;
31
32     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
33
34     public EventEnvelope(EventHeader eventHeader, SpikeGraphEvent body) {
35         this.header = eventHeader;
36         this.body = body;
37     }
38
39     /**
40      * Construct the envelope header from the provided event.
41      *
42      * @param event the Spike graph event for a vertex or edge operation
43      */
44     public EventEnvelope(SpikeGraphEvent event) {
45         this.header = new EventHeader.Builder().requestId(event.getTransactionId()).build();
46         this.body = event;
47     }
48
49     public EventHeader getEventHeader() {
50         return header;
51     }
52
53     public SpikeGraphEvent getBody() {
54         return body;
55     }
56
57     /**
58      * Serializes this object into a JSON string representation.
59      *
60      * @return a JSON format string representation of this object.
61      */
62     public String toJson() {
63         return gson.toJson(this);
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see java.lang.Object#toString()
70      */
71     @Override
72     public String toString() {
73         return this.toJson();
74     }
75 }