2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.mso.adapters.sdnc.sdncrest;
22 import org.openecomp.mso.adapters.sdncrest.SDNCEvent;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.xml.sax.InputSource;
27 import javax.xml.XMLConstants;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import java.io.StringReader;
30 import java.text.ParseException;
33 * SDNCConnector for "agnostic" API services.
35 public class SDNCEventParser {
37 * Parses SDNC event XML. If the content can be parsed and contains all required
38 * elements, then an object is returned. Otherwise, a ParseException is thrown.
39 * This method performs no logging or alarming.
40 * @throws ParseException on error
42 public static SDNCEvent parse(String content) throws ParseException {
44 // Note: this document builder is not namespace-aware, so namespaces are ignored.
45 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
46 documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
47 InputSource source = new InputSource(new StringReader(content));
48 Document doc = documentBuilderFactory.newDocumentBuilder().parse(source);
50 // Find the configuration-event child under the root element.
51 // The root element is expected to be an "output" element, but we don't really care.
53 Element root = doc.getDocumentElement();
54 Element configurationEvent = null;
56 for (Element child : SDNCAdapterUtils.childElements(root)) {
57 if ("configuration-event".equals(child.getNodeName())) {
58 configurationEvent = child;
63 if (configurationEvent == null) {
64 throw new ParseException("No configuration-event element in SDNC event", 0);
67 // Process the children of configuration-event
69 String eventType = null;
70 String eventCorrelatorType = null;
71 String eventCorrelator = null;
72 Element eventParameters = null;
74 for (Element child : SDNCAdapterUtils.childElements(configurationEvent)) {
75 if ("event-type".equals(child.getNodeName())) {
76 eventType = child.getTextContent();
77 } else if ("event-correlator-type".equals(child.getNodeName())) {
78 eventCorrelatorType = child.getTextContent();
79 } else if ("event-correlator".equals(child.getNodeName())) {
80 eventCorrelator = child.getTextContent();
81 } else if ("event-parameters".equals(child.getNodeName())) {
82 eventParameters = (Element) child;
86 // event-type is mandatory.
88 if (eventType == null || eventType.isEmpty()) {
89 throw new ParseException("No event-type in SDNC event", 0);
92 // event-correlator-type is mandatory.
94 if (eventCorrelatorType == null || eventCorrelatorType.isEmpty()) {
95 throw new ParseException("No event-correlator-type in SDNC event", 0);
98 // event-correlator is mandatory.
100 if (eventCorrelator == null || eventCorrelator.isEmpty()) {
101 throw new ParseException("No event-correlator in SDNC event", 0);
104 // Create an event object.
106 SDNCEvent event = new SDNCEvent(eventType, eventCorrelatorType, eventCorrelator);
108 // event-parameters is an optional container element. If present,
109 // process its children, adding values to the event object.
111 if (eventParameters != null) {
112 for (Element element : SDNCAdapterUtils.childElements(eventParameters)) {
113 if (!"event-parameter".equals(element.getNodeName())) {
117 String tagName = null;
118 String tagValue = null;
120 for (Element child : SDNCAdapterUtils.childElements(element)) {
121 if ("tag-name".equals(child.getNodeName())) {
122 tagName = child.getTextContent();
123 } else if ("tag-value".equals(child.getNodeName())) {
124 tagValue = child.getTextContent();
128 // tag-name is mandatory
130 if (tagName == null) {
131 throw new ParseException("Missing tag-name in SDNC event parameter", 0);
134 // tag-value is optional. If absent, make it an empty string so we don't
135 // end up with null values in the parameter map.
137 if (tagValue == null) {
141 event.addParam(tagName, tagValue);
146 } catch (ParseException e) {
148 } catch (Exception e) {
149 throw new ParseException("Failed to parse SDNC event", 0);
153 // Instantiation is not allowed.
154 private SDNCEventParser() {