aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/common/model/VesEvent.java
blob: 88419555c63d9ad69ec0b76854582193cf6c5496 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * ============LICENSE_START=======================================================
 * VES Collector
 * ================================================================================
 * Copyright (C) 2020-2021 Nokia. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.common.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Optional;

/**
 * This class is a wrapper for JSONObject, that represents VES event.
 * It contains Strings that represents key, that can be found in VES event.
 *
 * @author Zebek
 */
public class VesEvent {

    public static final String VES_UNIQUE_ID = "VESuniqueId";
    private static final String COMMON_EVENT_HEADER = "commonEventHeader";
    private static final String DOMAIN = "domain";
    private static final String STND_DEFINED_NAMESPACE = "stndDefinedNamespace";
    private static final String STND_DEFINED_DOMAIN = "stndDefined";
    private static final String STND_DEFINED_FIELDS = "stndDefinedFields";
    private static final String SCHEMA_REFERENCE = "schemaReference";
    private static final String EVENT = "event";
    private static final String PARTITION_KEY = "sourceName";

    private final JSONObject event;

    public VesEvent(JSONObject event) {
        this.event = event;
    }

    public JsonNode asJsonNode() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readTree(event.toString());
    }

    /**
     * Returns VES event in form of JSON object.
     *
     * @return event in form of json Object
     */
    public JSONObject asJsonObject() {
        return new JSONObject(event.toString());
    }

    /**
     * Returns Domain name from VES event.
     *
     * @return domain
     */
    public String getDomain() {
        return getEventHeader().getString(DOMAIN);
    }

    /**
     * Returns event primary key.
     * @return a primary key
     */
    public String getPK() {
        return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString();
    }

    /**
     * Returns schema reference.
     * @return a schema reference.
     */
    public String getSchemaReference() {
        return getStndDefinedFields().getString(SCHEMA_REFERENCE);
    }

    /**
     * Returns stream ID from VES event.
     *
     * @return stream ID
     */
    public String getStreamId() {
        String retVal = getDomain();

        if (isStdDefinedDomain(retVal)) {
            retVal = resolveDomainForStndDefinedEvent();
        }

        return retVal;
    }

    /**
     * Returns unique ID of VES event.
     *
     * @return unique ID
     */
    public Object getUniqueId() {
        return event.get(VES_UNIQUE_ID);
    }

    /**
     * Returns optional stndDefinedNamespace name from VES event.
     *
     * @return Optional stndDefinedNamespace
     */
    public Optional<String> getStndDefinedNamespace() throws JSONException {
        return isStdDefinedDomain(getDomain()) ? Optional.ofNullable(getEventHeader())
                .map(header -> header.getString(STND_DEFINED_NAMESPACE)) : Optional.empty();
    }

    /**
     * Checks if type of event is same as given in paramaters.
     *
     * @param type name that will be compared with event type
     * @return true or false depending if type given in parameter is same as VES event type
     */
    public boolean hasType(String type) {
        return this.event.has(type);
    }

    /**
     * Remove Json element from event by key.
     * @param key
     */
    public void removeElement(String key) {
        this.event.remove(key);
    }

    @Override
    public String toString() {
        return event.toString();
    }

    private JSONObject getStndDefinedFields() {
        return event
                .getJSONObject(EVENT)
                .getJSONObject(STND_DEFINED_FIELDS);
    }

    private String resolveDomainForStndDefinedEvent() {
        final JSONObject eventHeader = getEventHeader();
        if(eventHeader.has(STND_DEFINED_NAMESPACE)) {
            final String domain = eventHeader
                    .getString(STND_DEFINED_NAMESPACE);
            if(domain.isEmpty()) {
                throw new StndDefinedNamespaceParameterHasEmptyValueException();
            }
            return domain;
        } else {
            throw new StndDefinedNamespaceParameterNotDefinedException();
        }
    }

    private JSONObject getEventHeader() {
        return event
                .getJSONObject(EVENT)
                .getJSONObject(COMMON_EVENT_HEADER);
    }

    private boolean isStdDefinedDomain(String domain) {
        return domain.equals(STND_DEFINED_DOMAIN);
    }
}