2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.event.protocol.xml;
25 import java.io.ByteArrayInputStream;
26 import java.io.StringWriter;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map.Entry;
30 import javax.xml.XMLConstants;
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBElement;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.transform.stream.StreamSource;
37 import javax.xml.validation.SchemaFactory;
38 import org.onap.policy.apex.plugins.event.protocol.xml.jaxb.ObjectFactory;
39 import org.onap.policy.apex.plugins.event.protocol.xml.jaxb.XMLApexEvent;
40 import org.onap.policy.apex.plugins.event.protocol.xml.jaxb.XMLApexEventData;
41 import org.onap.policy.apex.service.engine.event.ApexEvent;
42 import org.onap.policy.apex.service.engine.event.ApexEventException;
43 import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter;
44 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
45 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.xml.sax.SAXException;
50 * The Class Apex2XMLEventConverter converts {@link ApexEvent} instances into string instances of {@link XMLApexEvent}
51 * that are XML representations of Apex events defined in JAXB.
53 * @author Liam Fallon (liam.fallon@ericsson.com)
55 public final class Apex2XmlEventConverter implements ApexEventProtocolConverter {
57 private static final String MODEL_SCHEMA_NAME = "xml/apex-event.xsd";
59 // XML Unmarshaller and marshaller and object factory for events
60 private Unmarshaller unmarshaller;
61 private Marshaller marshaller;
62 private ObjectFactory objectFactory = new ObjectFactory();
65 * Constructor to create the Apex to XML converter.
67 * @throws ApexEventException the apex event exception
69 public Apex2XmlEventConverter() throws ApexEventException {
71 final var schemaUrl = ResourceUtils.getUrlResource(MODEL_SCHEMA_NAME);
72 final var apexEventSchema =
73 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaUrl);
75 final var jaxbContext = JAXBContext.newInstance(XMLApexEvent.class);
77 // Set up the unmarshaller to carry out validation
78 unmarshaller = jaxbContext.createUnmarshaller();
79 unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
80 unmarshaller.setSchema(apexEventSchema);
82 // Set up the marshaller
83 marshaller = jaxbContext.createMarshaller();
84 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
85 marshaller.setSchema(apexEventSchema);
86 } catch (JAXBException | SAXException e) {
87 throw new ApexEventException("Unable to set up marshalling and unmarshalling for XML events", e);
95 public void init(final EventProtocolParameters parameters) {
96 // No initialization necessary on this class
103 public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
104 // Check the XML event
105 if (eventObject == null) {
106 throw new ApexEventException("event processing failed, XML event is null");
109 // Cast the event to a string, if our conversion is correctly configured, this cast should always work
110 String xmlEventString = null;
112 xmlEventString = (String) eventObject;
113 } catch (final Exception e) {
114 final String errorMessage = "error converting event \"" + eventObject + "\" to a string";
115 throw new ApexEventRuntimeException(errorMessage, e);
119 XMLApexEvent xmlApexEvent = null;
121 // Use JAXB to read and verify the event from the XML string
123 final var source = new StreamSource(new ByteArrayInputStream(xmlEventString.getBytes()));
124 final JAXBElement<XMLApexEvent> rootElement = unmarshaller.unmarshal(source, XMLApexEvent.class);
125 xmlApexEvent = rootElement.getValue();
126 } catch (final JAXBException e) {
127 throw new ApexEventException("Unable to unmarshal Apex XML event\n" + xmlEventString, e);
130 // Create the Apex event
131 // FIXME: Introduce new AxEvent field for APEX to Xml conversion
132 final var apexEvent = new ApexEvent(xmlApexEvent.getName(), xmlApexEvent.getVersion(),
133 xmlApexEvent.getNameSpace(), xmlApexEvent.getSource(), xmlApexEvent.getTarget(), "");
135 // Set the data on the apex event
136 for (final XMLApexEventData xmlData : xmlApexEvent.getData()) {
137 apexEvent.put(xmlData.getKey(), xmlData.getValue());
140 // Return the event in a single element
141 final ArrayList<ApexEvent> eventList = new ArrayList<>();
142 eventList.add(apexEvent);
150 public String fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
151 // Check the Apex event
152 if (apexEvent == null) {
153 throw new ApexEventException("event processing failed, Apex event is null");
156 // Get the Apex event data
157 final List<XMLApexEventData> xmlDataList = new ArrayList<>();
160 for (final Entry<String, Object> apexDataEntry : apexEvent.entrySet()) {
161 // Add an XML event data item
162 if (apexDataEntry.getValue() != null) {
163 xmlDataList.add(new XMLApexEventData(apexDataEntry.getKey(), apexDataEntry.getValue().toString()));
165 xmlDataList.add(new XMLApexEventData(apexDataEntry.getKey(), ""));
168 } catch (final Exception e) {
169 throw new ApexEventException("Unable to transfer Apex event data to XML\n" + apexEvent, e);
172 // Create the XML event
173 final var xmlApexEvent = new XMLApexEvent(apexEvent.getName(), apexEvent.getVersion(),
174 apexEvent.getNameSpace(), apexEvent.getSource(), apexEvent.getTarget(), xmlDataList);
176 // Write the event into a DOM document
178 // Marshal the event into XML
179 final var writer = new StringWriter();
180 marshaller.marshal(objectFactory.createXmlApexEvent(xmlApexEvent), writer);
182 // Return the event as XML in a string
183 return writer.toString();
184 } catch (final JAXBException e) {
185 throw new ApexEventException("Unable to unmarshal Apex event to XML\n" + apexEvent, e);