8e2db80e247c3388a9765ff1e70477feb9c11de7
[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 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
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.JSONObject;
26
27 /**
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.
30  *
31  * @author Zebek
32  */
33 public class VesEvent {
34
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";
44
45     private final JSONObject event;
46
47     public VesEvent(JSONObject event) {
48         this.event = event;
49     }
50
51     public JsonNode asJsonNode() throws JsonProcessingException {
52         ObjectMapper objectMapper = new ObjectMapper();
53         return objectMapper.readTree(event.toString());
54     }
55
56     /**
57      * Returns VES event in form of JSON object.
58      *
59      * @return event in form of json Object
60      */
61     public JSONObject asJsonObject() {
62         return new JSONObject(event.toString());
63     }
64
65     /**
66      * Returns Domain name from VES event.
67      *
68      * @return domain
69      */
70     public String getDomain() {
71         return getEventHeader().getString(DOMAIN);
72     }
73
74     /**
75      * Returns event primary key.
76      * @return a primary key
77      */
78     public String getPK() {
79         return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString();
80     }
81
82     /**
83      * Returns schema reference.
84      * @return a schema reference.
85      */
86     public String getSchemaReference() {
87         return getStndDefinedFields().getString(SCHEMA_REFERENCE);
88     }
89
90     /**
91      * Returns stream ID from VES event.
92      *
93      * @return stream ID
94      */
95     public String getStreamId() {
96         String retVal = getDomain();
97
98         if (isStdDefinedDomain(retVal)) {
99             retVal = resolveDomainForStndDefinedEvent();
100         }
101
102         return retVal;
103     }
104
105     /**
106      * Returns unique ID of VES event.
107      *
108      * @return unique ID
109      */
110     public Object getUniqueId() {
111         return event.get(VES_UNIQUE_ID);
112     }
113
114     /**
115      * Checks if type of event is same as given in paramaters.
116      *
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
119      */
120     public boolean hasType(String type) {
121         return this.event.has(type);
122     }
123
124     /**
125      * Remove Json element from event by key.
126      * @param key
127      */
128     public void removeElement(String key) {
129         this.event.remove(key);
130     }
131
132     @Override
133     public String toString() {
134         return event.toString();
135     }
136
137     private JSONObject getStndDefinedFields() {
138         return event
139                 .getJSONObject(EVENT)
140                 .getJSONObject(STND_DEFINED_FIELDS);
141     }
142
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();
150             }
151             return domain;
152         } else {
153             throw new StndDefinedNamespaceParameterNotDefinedException();
154         }
155     }
156
157     private JSONObject getEventHeader() {
158         return event
159                 .getJSONObject(EVENT)
160                 .getJSONObject(COMMON_EVENT_HEADER);
161     }
162
163     private boolean isStdDefinedDomain(String domain) {
164         return domain.equals(STND_DEFINED_DOMAIN);
165     }
166 }