summaryrefslogtreecommitdiffstats
path: root/datacollector/src/main/java/org/onap/rapp/datacollector/service/ParserAbstractClass.java
blob: 6f539b37507df78dc524a1f7dea2c76ac197d1fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (C) 2021 Samsung Electronics
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */


package org.onap.rapp.datacollector.service;

import static java.util.Objects.nonNull;

import java.util.Optional;

import org.onap.rapp.datacollector.entity.ves.CommonEventHeader;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public abstract class ParserAbstractClass {

    public static final String VES_EVENT_UNIQUE_ELEMENT = "measurementFields";
    public static final String FILE_READY_EVENT_UNIQUE_ELEMENT = "perf3gppFields";
    public static final String EVENT_JSON_ELEMENT_NAME = "event";
    public static final String COMMON_EVENT_HEADER = "commonEventHeader";

    /**
     * Scans the Json Object if contains VES_EVENT_UNIQUE_ELEMENT
     *
     * @param obj json object
     * @return true=it is VES event, false=not VES event
     */
    protected boolean isVesEvent(JsonObject obj) {
        return getEventJsonObject(obj).filter(jsonObject -> jsonObject.has(VES_EVENT_UNIQUE_ELEMENT)).isPresent();
    }

    /**
     * Scans the Json Object if contains FILE_READY_EVENT_UNIQUE_ELEMENT
     *
     * @param obj json object
     * @return true=it is FileReadyEvent event, false=not FileReadyEvent
     */
    protected boolean isFileReadyEvent(JsonObject obj) {
        return getEventJsonObject(obj).filter(jsonObject -> jsonObject.has(FILE_READY_EVENT_UNIQUE_ELEMENT)).isPresent();
    }

    /**
     * Gets Event json element from incoming json
     *
     * @param jsonElement top json elemnt
     * @return Event Json element
     */
    protected static Optional<JsonObject> getEventJsonObject(JsonElement jsonElement) {
        JsonObject obj = jsonElement.getAsJsonObject();
        return Optional.ofNullable(obj.getAsJsonObject(EVENT_JSON_ELEMENT_NAME));
    }

    /**
     * Gets CommonEventHeader from json
     *
     * @param obj Event json element
     * @param jsonDeserializationContext json context
     * @return CommonEventHeader object from json object
     */
    protected static CommonEventHeader getHeaderJsonObject(JsonObject obj, JsonDeserializationContext jsonDeserializationContext) {
        if (nonNull(obj) && obj.has(COMMON_EVENT_HEADER)) {
            JsonObject headerJson = obj.getAsJsonObject(COMMON_EVENT_HEADER);
            return jsonDeserializationContext.deserialize(headerJson, CommonEventHeader.class);
        } else {
            throw new JsonParseException("Common header not found");
        }
    }

}