2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.s
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.JSONObject;
28 * This class is a wrapper for JSONObject, that represents VES event.
29 * It contains Strings that represents key, that can be found in VES event.
33 public class VesEvent {
35 public static final String VES_UNIQUE_ID = "VESuniqueId";
36 private static final String COMMON_EVENT_HEADER = "commonEventHeader";
37 private static final String DOMAIN = "domain";
38 private static final String STND_DEFINED_NAMESPACE = "stndDefinedNamespace";
39 private static final String STND_DEFINED_DOMAIN = "stndDefined";
40 private static final String STND_DEFINED_FIELDS = "stndDefinedFields";
41 private static final String SCHEMA_REFERENCE = "schemaReference";
42 private static final String EVENT = "event";
43 private static final String PARTITION_KEY = "sourceName";
45 private final JSONObject event;
47 public VesEvent(JSONObject event) {
51 public JsonNode asJsonNode() throws JsonProcessingException {
52 ObjectMapper objectMapper = new ObjectMapper();
53 return objectMapper.readTree(event.toString());
57 * Returns VES event in form of JSON object.
59 * @return event in form of json Object
61 public JSONObject asJsonObject() {
62 return new JSONObject(event.toString());
66 * Returns Domain name from VES event.
70 public String getDomain() {
71 return getEventHeader().getString(DOMAIN);
75 * Returns event primary key.
76 * @return a primary key
78 public String getPK() {
79 return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString();
83 * Returns schema reference.
84 * @return a schema reference.
86 public String getSchemaReference() {
87 return getStndDefinedFields().getString(SCHEMA_REFERENCE);
91 * Returns stream ID from VES event.
95 public String getStreamId() {
96 String retVal = getDomain();
98 if (isStdDefinedDomain(retVal)) {
99 retVal = resolveDomainForStndDefinedEvent();
106 * Returns unique ID of VES event.
110 public Object getUniqueId() {
111 return event.get(VES_UNIQUE_ID);
115 * Checks if type of event is same as given in paramaters.
117 * @param type name that will be compared with event type
118 * @return true or false depending if type given in parameter is same as VES event type
120 public boolean hasType(String type) {
121 return this.event.has(type);
125 * Remove Json element from event by key.
128 public void removeElement(String key) {
129 this.event.remove(key);
133 public String toString() {
134 return event.toString();
137 private JSONObject getStndDefinedFields() {
139 .getJSONObject(EVENT)
140 .getJSONObject(STND_DEFINED_FIELDS);
143 private String resolveDomainForStndDefinedEvent() {
144 final JSONObject eventHeader = getEventHeader();
145 if(eventHeader.has(STND_DEFINED_NAMESPACE)) {
146 final String domain = eventHeader
147 .getString(STND_DEFINED_NAMESPACE);
148 if(domain.isEmpty()) {
149 throw new StndDefinedNamespaceParameterHasEmptyValueException();
153 throw new StndDefinedNamespaceParameterNotDefinedException();
157 private JSONObject getEventHeader() {
159 .getJSONObject(EVENT)
160 .getJSONObject(COMMON_EVENT_HEADER);
163 private boolean isStdDefinedDomain(String domain) {
164 return domain.equals(STND_DEFINED_DOMAIN);