2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
28 import org.onap.policy.apex.model.basicmodel.service.ModelService;
29 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
32 import org.onap.policy.apex.service.engine.event.ApexEvent;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
40 import com.google.gson.Gson;
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.JsonElement;
43 import com.google.gson.JsonObject;
46 * The Class Apex2JSONEventConverter converts {@link ApexEvent} instances to and from JSON string representations of
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class Apex2JSONEventConverter implements ApexEventProtocolConverter {
52 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JSONEventConverter.class);
54 // The parameters for the JSON event protocol
55 private JSONEventProtocolParameters jsonPars;
60 * @see org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter#init(org.onap.policy.
61 * apex.service.parameters.eventprotocol.EventProtocolParameters)
64 public void init(final EventProtocolParameters parameters) {
65 // Check and get the JSON parameters
66 if (!(parameters instanceof JSONEventProtocolParameters)) {
67 final String errorMessage = "specified consumer properties are not applicable to the JSON event protocol";
68 LOGGER.warn(errorMessage);
69 throw new ApexEventRuntimeException(errorMessage);
72 jsonPars = (JSONEventProtocolParameters) parameters;
78 * @see org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String, java.lang.Object)
81 public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
82 // Check the event eventObject
83 if (eventObject == null) {
84 LOGGER.warn("event processing failed, event is null");
85 throw new ApexEventException("event processing failed, event is null");
88 // Cast the event to a string, if our conversion is correctly configured, this cast should
90 String jsonEventString = null;
92 jsonEventString = (String) eventObject;
93 } catch (final Exception e) {
94 final String errorMessage = "error converting event \"" + eventObject + "\" to a string";
95 LOGGER.debug(errorMessage, e);
96 throw new ApexEventRuntimeException(errorMessage, e);
99 // The list of events we will return
100 final List<ApexEvent> eventList = new ArrayList<>();
103 // We may have a single JSON object with a single event or an array of JSON objects
104 final Object decodedJsonObject = new GsonBuilder().serializeNulls().create().fromJson(jsonEventString,
107 // Check if we have a list of objects
108 if (decodedJsonObject instanceof List) {
109 // Check if it's a list of JSON objects or a list of strings
110 @SuppressWarnings("unchecked")
111 final List<Object> decodedJsonList = (List<Object>) decodedJsonObject;
113 // Decode each of the list elements in sequence
114 for (final Object jsonListObject : decodedJsonList) {
115 if (jsonListObject instanceof String) {
116 eventList.add(jsonStringApexEvent(eventName, (String) jsonListObject));
117 } else if (jsonListObject instanceof JsonObject) {
118 eventList.add(jsonObject2ApexEvent(eventName, (JsonObject) jsonListObject));
120 throw new ApexEventException("incoming event (" + jsonEventString
121 + ") is a JSON object array containing an invalid object " + jsonListObject);
125 eventList.add(jsonStringApexEvent(eventName, jsonEventString));
127 } catch (final Exception e) {
128 final String errorString = "Failed to unmarshal JSON event: " + e.getMessage() + ", event="
130 LOGGER.warn(errorString, e);
131 throw new ApexEventException(errorString, e);
134 // Return the list of events we have unmarshalled
141 * @see org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.
142 * apex.service.engine.event.ApexEvent)
145 public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
146 // Check the Apex event
147 if (apexEvent == null) {
148 LOGGER.warn("event processing failed, Apex event is null");
149 throw new ApexEventException("event processing failed, Apex event is null");
152 // Get the event definition for the event from the model service
153 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName(),
154 apexEvent.getVersion());
156 // Use a GSON Json object to marshal the Apex event to JSON
157 final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
158 final JsonObject jsonObject = new JsonObject();
160 jsonObject.addProperty(ApexEvent.NAME_HEADER_FIELD, apexEvent.getName());
161 jsonObject.addProperty(ApexEvent.VERSION_HEADER_FIELD, apexEvent.getVersion());
162 jsonObject.addProperty(ApexEvent.NAMESPACE_HEADER_FIELD, apexEvent.getNameSpace());
163 jsonObject.addProperty(ApexEvent.SOURCE_HEADER_FIELD, apexEvent.getSource());
164 jsonObject.addProperty(ApexEvent.TARGET_HEADER_FIELD, apexEvent.getTarget());
166 if (apexEvent.getExceptionMessage() != null) {
167 jsonObject.addProperty(ApexEvent.EXCEPTION_MESSAGE_HEADER_FIELD, apexEvent.getExceptionMessage());
170 for (final AxField eventField : eventDefinition.getFields()) {
171 final String fieldName = eventField.getKey().getLocalName();
173 if (!apexEvent.containsKey(fieldName)) {
174 if (!eventField.getOptional()) {
175 final String errorMessage = "error parsing " + eventDefinition.getID() + " event to Json. "
176 + "Field \"" + fieldName + "\" is missing, but is mandatory. Fields: " + apexEvent;
177 LOGGER.debug(errorMessage);
178 throw new ApexEventRuntimeException(errorMessage);
183 final Object fieldValue = apexEvent.get(fieldName);
185 // Get the schema helper
186 final SchemaHelper fieldSchemaHelper = new SchemaHelperFactory().createSchemaHelper(eventField.getKey(),
187 eventField.getSchema());
188 jsonObject.add(fieldName, (JsonElement)fieldSchemaHelper.marshal2Object(fieldValue));
191 // Output JSON string in a pretty format
192 return gson.toJson(jsonObject);
196 * This method converts a JSON object into an Apex event.
199 * the name of the event
200 * @param jsonEventString
201 * the JSON string that holds the event
202 * @return the apex event that we have converted the JSON object into
203 * @throws ApexEventException
204 * thrown on unmarshaling exceptions
206 private ApexEvent jsonStringApexEvent(final String eventName, final String jsonEventString)
207 throws ApexEventException {
208 // Use GSON to read the event string
209 final JsonObject jsonObject = new GsonBuilder().serializeNulls().create().fromJson(jsonEventString,
212 if (jsonObject == null || !jsonObject.isJsonObject()) {
213 throw new ApexEventException(
214 "incoming event (" + jsonEventString + ") is not a JSON object or an JSON object array");
217 return jsonObject2ApexEvent(eventName, jsonObject);
221 * This method converts a JSON object into an Apex event.
224 * the name of the event
226 * the JSON object that holds the event
227 * @return the apex event that we have converted the JSON object into
228 * @throws ApexEventException
229 * thrown on unmarshaling exceptions
231 private ApexEvent jsonObject2ApexEvent(final String eventName, final JsonObject jsonObject)
232 throws ApexEventException {
233 // Process the mandatory Apex header
234 final ApexEvent apexEvent = processApexEventHeader(eventName, jsonObject);
236 // Get the event definition for the event from the model service
237 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName(),
238 apexEvent.getVersion());
240 // Iterate over the input fields in the event
241 for (final AxField eventField : eventDefinition.getFields()) {
242 final String fieldName = eventField.getKey().getLocalName();
243 if (!hasJSONField(jsonObject, fieldName)) {
244 if (!eventField.getOptional()) {
245 final String errorMessage = "error parsing " + eventDefinition.getID() + " event from Json. "
246 + "Field \"" + fieldName + "\" is missing, but is mandatory.";
247 LOGGER.debug(errorMessage);
248 throw new ApexEventException(errorMessage);
253 final JsonElement fieldValue = getJSONField(jsonObject, fieldName, null, !eventField.getOptional());
255 if (fieldValue != null && !fieldValue.isJsonNull()) {
256 // Get the schema helper
257 final SchemaHelper fieldSchemaHelper = new SchemaHelperFactory().createSchemaHelper(eventField.getKey(),
258 eventField.getSchema());
259 apexEvent.put(fieldName, fieldSchemaHelper.createNewInstance(fieldValue));
261 apexEvent.put(fieldName, null);
269 * This method processes the event header of an Apex event.
272 * the name of the event
274 * the JSON object containing the JSON representation of the incoming event
275 * @return an apex event constructed using the header fields of the event
276 * @throws ApexEventRuntimeException
277 * the apex event runtime exception
278 * @throws ApexEventException
279 * on invalid events with missing header fields
281 private ApexEvent processApexEventHeader(final String eventName, final JsonObject jsonObject)
282 throws ApexEventException {
283 // Get the event header fields
285 String name = getJSONStringField(jsonObject, ApexEvent.NAME_HEADER_FIELD, jsonPars.getNameAlias(), ApexEvent.NAME_REGEXP, false);
286 String version = getJSONStringField(jsonObject, ApexEvent.VERSION_HEADER_FIELD, jsonPars.getVersionAlias(), ApexEvent.VERSION_REGEXP, false);
287 String namespace = getJSONStringField(jsonObject, ApexEvent.NAMESPACE_HEADER_FIELD, jsonPars.getNameSpaceAlias(), ApexEvent.NAMESPACE_REGEXP, false);
288 String source = getJSONStringField(jsonObject, ApexEvent.SOURCE_HEADER_FIELD, jsonPars.getSourceAlias(), ApexEvent.SOURCE_REGEXP, false);
289 String target = getJSONStringField(jsonObject, ApexEvent.TARGET_HEADER_FIELD, jsonPars.getTargetAlias(), ApexEvent.TARGET_REGEXP, false);
292 // Check that an event name has been specified
293 if (name == null && eventName == null) {
294 throw new ApexEventRuntimeException(
295 "event received without mandatory parameter \"name\" on configuration or on event");
298 // Check if an event name was specified on the event parameters
299 if (eventName != null) {
300 if (name != null && !eventName.equals(name)) {
301 LOGGER.warn("The incoming event name \"{}\" does not match the configured event name \"{}\", using configured event name",
307 // Now, find the event definition in the model service. If version is null, the newest event
308 // definition in the model service is used
309 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(name, version);
310 if (eventDefinition == null) {
311 throw new ApexEventRuntimeException("an event definition for an event named \"" + name
312 + "\" with version \"" + version + "\" not found in Apex model");
315 // Use the defined event version if no version is specified on the incoming fields
316 if (version == null) {
317 version = eventDefinition.getKey().getVersion();
320 // Check the name space is OK if it is defined, if not, use the name space from the model
321 if (namespace != null) {
322 if (!namespace.equals(eventDefinition.getNameSpace())) {
323 throw new ApexEventRuntimeException("namespace \"" + namespace + "\" on event \"" + name
324 + "\" does not match namespace \"" + eventDefinition.getNameSpace()
325 + "\" for that event in the Apex model");
328 namespace = eventDefinition.getNameSpace();
331 // For source, use the defined source only if the source is not found on the incoming event
332 if (source == null) {
333 source = eventDefinition.getSource();
336 // For target, use the defined source only if the source is not found on the incoming event
337 if (target == null) {
338 target = eventDefinition.getTarget();
341 return new ApexEvent(name, version, namespace, source, target);
345 * This method gets an event string field from a JSON object.
348 * the JSON object containing the JSON representation of the incoming event
350 * the field name to find in the event
352 * the alias for the field to find in the event, overrides the field name if it is not null
354 * the regular expression to check the field against for validity
356 * true if the field is mandatory
357 * @return the value of the field in the JSON object or null if the field is optional
358 * @throws ApexEventRuntimeException
359 * the apex event runtime exception
361 private String getJSONStringField(final JsonObject jsonObject, final String fieldName, final String fieldAlias,
362 final String fieldRE, final boolean mandatory) {
363 // Get the JSON field for the string field
364 final JsonElement jsonField = getJSONField(jsonObject, fieldName, fieldAlias, mandatory);
366 // Null strings are allowed
367 if (jsonField == null || jsonField.isJsonNull()) {
371 // Check if this is a string field
372 String fieldValueString = null;
374 fieldValueString = jsonField.getAsString();
375 } catch (final Exception e) {
376 // The element is not a string so throw an error
377 throw new ApexEventRuntimeException("field \"" + fieldName + "\" with type \""
378 + jsonField.getClass().getCanonicalName() + "\" is not a string value");
381 // Is regular expression checking required
382 if (fieldRE == null) {
383 return fieldValueString;
386 // Check the event field against its regular expression
387 if (!fieldValueString.matches(fieldRE)) {
388 throw new ApexEventRuntimeException(
389 "field \"" + fieldName + "\" with value \"" + fieldValueString + "\" is invalid");
392 return fieldValueString;
396 * This method gets an event field from a JSON object.
399 * the JSON object containing the JSON representation of the incoming event
401 * the field name to find in the event
403 * the alias for the field to find in the event, overrides the field name if it is not null
405 * true if the field is mandatory
406 * @return the value of the field in the JSON object or null if the field is optional
407 * @throws ApexEventRuntimeException
408 * the apex event runtime exception
410 private JsonElement getJSONField(final JsonObject jsonObject, final String fieldName, final String fieldAlias,
411 final boolean mandatory) {
413 // Check if we should use the alias for this field
414 String fieldToFind = fieldName;
415 if (fieldAlias != null) {
416 fieldToFind = fieldAlias;
419 // Get the event field
420 final JsonElement eventElement = jsonObject.get(fieldToFind);
421 if (eventElement == null) {
425 throw new ApexEventRuntimeException("mandatory field \"" + fieldToFind + "\" is missing");
433 * This method if a JSON object has a named field.
436 * the JSON object containing the JSON representation of the incoming event
438 * the field name to find in the event
439 * @return true if the field is present
440 * @throws ApexEventRuntimeException
441 * the apex event runtime exception
443 private boolean hasJSONField(final JsonObject jsonObject, final String fieldName) {
444 // check for the field
445 return jsonObject.has(fieldName);