2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.common.model;
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import java.util.Optional;
30 * This class is a wrapper for JSONObject, that represents VES event.
31 * It contains Strings that represents key, that can be found in VES event.
35 public class VesEvent {
37 public static final String VES_UNIQUE_ID = "VESuniqueId";
38 private static final String COMMON_EVENT_HEADER = "commonEventHeader";
39 private static final String DOMAIN = "domain";
40 private static final String STND_DEFINED_NAMESPACE = "stndDefinedNamespace";
41 private static final String STND_DEFINED_DOMAIN = "stndDefined";
42 private static final String STND_DEFINED_FIELDS = "stndDefinedFields";
43 private static final String SCHEMA_REFERENCE = "schemaReference";
44 private static final String EVENT = "event";
45 private static final String PARTITION_KEY = "sourceName";
47 private final JSONObject event;
49 public VesEvent(JSONObject event) {
53 public JsonNode asJsonNode() throws JsonProcessingException {
54 ObjectMapper objectMapper = new ObjectMapper();
55 return objectMapper.readTree(event.toString());
59 * Returns VES event in form of JSON object.
61 * @return event in form of json Object
63 public JSONObject asJsonObject() {
64 return new JSONObject(event.toString());
68 * Returns Domain name from VES event.
72 public String getDomain() {
73 return getEventHeader().getString(DOMAIN);
77 * Returns event primary key.
78 * @return a primary key
80 public String getPK() {
81 return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString();
85 * Returns schema reference.
86 * @return a schema reference.
88 public String getSchemaReference() {
89 return getStndDefinedFields().getString(SCHEMA_REFERENCE);
93 * Returns stream ID from VES event.
97 public String getStreamId() {
98 String retVal = getDomain();
100 if (isStdDefinedDomain(retVal)) {
101 retVal = resolveDomainForStndDefinedEvent();
108 * Returns unique ID of VES event.
112 public Object getUniqueId() {
113 return event.get(VES_UNIQUE_ID);
117 * Returns optional stndDefinedNamespace name from VES event.
119 * @return Optional stndDefinedNamespace
121 public Optional<String> getStndDefinedNamespace() throws JSONException {
122 return isStdDefinedDomain(getDomain()) ? Optional.ofNullable(getEventHeader())
123 .map(header -> header.getString(STND_DEFINED_NAMESPACE)) : Optional.empty();
127 * Checks if type of event is same as given in paramaters.
129 * @param type name that will be compared with event type
130 * @return true or false depending if type given in parameter is same as VES event type
132 public boolean hasType(String type) {
133 return this.event.has(type);
137 * Remove Json element from event by key.
140 public void removeElement(String key) {
141 this.event.remove(key);
145 public String toString() {
146 return event.toString();
149 private JSONObject getStndDefinedFields() {
151 .getJSONObject(EVENT)
152 .getJSONObject(STND_DEFINED_FIELDS);
155 private String resolveDomainForStndDefinedEvent() {
156 final JSONObject eventHeader = getEventHeader();
157 if(eventHeader.has(STND_DEFINED_NAMESPACE)) {
158 final String domain = eventHeader
159 .getString(STND_DEFINED_NAMESPACE);
160 if(domain.isEmpty()) {
161 throw new StndDefinedNamespaceParameterHasEmptyValueException();
165 throw new StndDefinedNamespaceParameterNotDefinedException();
169 private JSONObject getEventHeader() {
171 .getJSONObject(EVENT)
172 .getJSONObject(COMMON_EVENT_HEADER);
175 private boolean isStdDefinedDomain(String domain) {
176 return domain.equals(STND_DEFINED_DOMAIN);