Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / event / envelope / EventEnvelopeParser.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 java.util.UUID;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import org.onap.aai.spike.event.incoming.GizmoGraphEvent;
27 import org.onap.aai.spike.exception.SpikeException;
28 import org.onap.aai.spike.schema.GraphEventTransformer;
29
30 public class EventEnvelopeParser {
31
32     /**
33      * Parses the event to extract the content of the body and generate a {@link GizmoGraphEvent}
34      * object.
35      *
36      * @param event event envelope with both header and body properties
37      * @return the body of the event represented by a {@link GizmoGraphEvent} object
38      * @throws SpikeException if the event cannot be parsed
39      */
40     public GizmoGraphEvent parseEvent(String event) throws SpikeException {
41
42         GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(extractEventBody(event));
43
44         GraphEventTransformer.populateUUID(graphEvent);
45         if (graphEvent.getTransactionId() == null || graphEvent.getTransactionId().isEmpty()) {
46             graphEvent.setTransactionId(UUID.randomUUID().toString());
47         }
48         if (graphEvent.getRelationship() != null) {
49             GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
50         } else if (graphEvent.getVertex() != null) {
51             GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
52         } else {
53             throw new SpikeException("Unable to parse event: " + event);
54         }
55
56         return graphEvent;
57     }
58
59     private String extractEventBody(String event) {
60         JsonParser jsonParser = new JsonParser();
61         JsonElement jsonElement = jsonParser.parse(event);
62         return jsonElement.getAsJsonObject().get("body").toString();
63     }
64 }