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.aai.spike.event.envelope;
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;
30 public class EventEnvelopeParser {
33 * Parses the event to extract the content of the body and generate a {@link GizmoGraphEvent}
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
40 public GizmoGraphEvent parseEvent(String event) throws SpikeException {
42 GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(extractEventBody(event));
44 GraphEventTransformer.populateUUID(graphEvent);
45 if (graphEvent.getTransactionId() == null || graphEvent.getTransactionId().isEmpty()) {
46 graphEvent.setTransactionId(UUID.randomUUID().toString());
48 if (graphEvent.getRelationship() != null) {
49 GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
50 } else if (graphEvent.getVertex() != null) {
51 GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
53 throw new SpikeException("Unable to parse event: " + event);
59 private String extractEventBody(String event) {
60 JsonParser jsonParser = new JsonParser();
61 JsonElement jsonElement = jsonParser.parse(event);
62 return jsonElement.getAsJsonObject().get("body").toString();