Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / model / VesEvent.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.common.model;
21
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;
28
29 /**
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.
32  *
33  * @author Zebek
34  */
35 public class VesEvent {
36
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";
46
47     private final JSONObject event;
48
49     public VesEvent(JSONObject event) {
50         this.event = event;
51     }
52
53     public JsonNode asJsonNode() throws JsonProcessingException {
54         ObjectMapper objectMapper = new ObjectMapper();
55         return objectMapper.readTree(event.toString());
56     }
57
58     /**
59      * Returns VES event in form of JSON object.
60      *
61      * @return event in form of json Object
62      */
63     public JSONObject asJsonObject() {
64         return new JSONObject(event.toString());
65     }
66
67     /**
68      * Returns Domain name from VES event.
69      *
70      * @return domain
71      */
72     public String getDomain() {
73         return getEventHeader().getString(DOMAIN);
74     }
75
76     /**
77      * Returns event primary key.
78      * @return a primary key
79      */
80     public String getPK() {
81         return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString();
82     }
83
84     /**
85      * Returns schema reference.
86      * @return a schema reference.
87      */
88     public String getSchemaReference() {
89         return getStndDefinedFields().getString(SCHEMA_REFERENCE);
90     }
91
92     /**
93      * Returns stream ID from VES event.
94      *
95      * @return stream ID
96      */
97     public String getStreamId() {
98         String retVal = getDomain();
99
100         if (isStdDefinedDomain(retVal)) {
101             retVal = resolveDomainForStndDefinedEvent();
102         }
103
104         return retVal;
105     }
106
107     /**
108      * Returns unique ID of VES event.
109      *
110      * @return unique ID
111      */
112     public Object getUniqueId() {
113         return event.get(VES_UNIQUE_ID);
114     }
115
116     /**
117      * Returns optional stndDefinedNamespace name from VES event.
118      *
119      * @return Optional stndDefinedNamespace
120      */
121     public Optional<String> getStndDefinedNamespace() throws JSONException {
122         return isStdDefinedDomain(getDomain()) ? Optional.ofNullable(getEventHeader())
123                 .map(header -> header.getString(STND_DEFINED_NAMESPACE)) : Optional.empty();
124     }
125
126     /**
127      * Checks if type of event is same as given in paramaters.
128      *
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
131      */
132     public boolean hasType(String type) {
133         return this.event.has(type);
134     }
135
136     /**
137      * Remove Json element from event by key.
138      * @param key
139      */
140     public void removeElement(String key) {
141         this.event.remove(key);
142     }
143
144     @Override
145     public String toString() {
146         return event.toString();
147     }
148
149     private JSONObject getStndDefinedFields() {
150         return event
151                 .getJSONObject(EVENT)
152                 .getJSONObject(STND_DEFINED_FIELDS);
153     }
154
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();
162             }
163             return domain;
164         } else {
165             throw new StndDefinedNamespaceParameterNotDefinedException();
166         }
167     }
168
169     private JSONObject getEventHeader() {
170         return event
171                 .getJSONObject(EVENT)
172                 .getJSONObject(COMMON_EVENT_HEADER);
173     }
174
175     private boolean isStdDefinedDomain(String domain) {
176         return domain.equals(STND_DEFINED_DOMAIN);
177     }
178 }