2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.adapters.sdnc.sdncrest;
23 import org.openecomp.mso.adapters.sdncrest.SDNCEvent;
24 import org.openecomp.mso.logger.MsoLogger;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.xml.sax.InputSource;
29 import javax.xml.XMLConstants;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import java.io.StringReader;
32 import java.text.ParseException;
35 * SDNCConnector for "agnostic" API services.
37 public class SDNCEventParser {
38 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
40 // Instantiation is not allowed.
41 private SDNCEventParser() {
45 * Parses SDNC event XML. If the content can be parsed and contains all required
46 * elements, then an object is returned. Otherwise, a ParseException is thrown.
47 * This method performs no logging or alarming.
48 * @throws ParseException on error
50 public static SDNCEvent parse(String content) throws ParseException {
52 // Note: this document builder is not namespace-aware, so namespaces are ignored.
53 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
54 documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
55 InputSource source = new InputSource(new StringReader(content));
56 Document doc = documentBuilderFactory.newDocumentBuilder().parse(source);
58 // Find the configuration-event child under the root element.
59 // The root element is expected to be an "output" element, but we don't really care.
61 Element root = doc.getDocumentElement();
62 Element configurationEvent = null;
64 for (Element child : SDNCAdapterUtils.childElements(root)) {
65 if ("configuration-event".equals(child.getNodeName())) {
66 configurationEvent = child;
71 if (configurationEvent == null) {
72 throw new ParseException("No configuration-event element in SDNC event", 0);
75 // Process the children of configuration-event
77 String eventType = null;
78 String eventCorrelatorType = null;
79 String eventCorrelator = null;
80 Element eventParameters = null;
82 for (Element child : SDNCAdapterUtils.childElements(configurationEvent)) {
83 if ("event-type".equals(child.getNodeName())) {
84 eventType = child.getTextContent();
85 } else if ("event-correlator-type".equals(child.getNodeName())) {
86 eventCorrelatorType = child.getTextContent();
87 } else if ("event-correlator".equals(child.getNodeName())) {
88 eventCorrelator = child.getTextContent();
89 } else if ("event-parameters".equals(child.getNodeName())) {
90 eventParameters = child;
94 // event-type is mandatory.
96 if (eventType == null || eventType.isEmpty()) {
97 throw new ParseException("No event-type in SDNC event", 0);
100 // event-correlator-type is mandatory.
102 if (eventCorrelatorType == null || eventCorrelatorType.isEmpty()) {
103 throw new ParseException("No event-correlator-type in SDNC event", 0);
106 // event-correlator is mandatory.
108 if (eventCorrelator == null || eventCorrelator.isEmpty()) {
109 throw new ParseException("No event-correlator in SDNC event", 0);
112 // Create an event object.
114 SDNCEvent event = new SDNCEvent(eventType, eventCorrelatorType, eventCorrelator);
116 // event-parameters is an optional container element. If present,
117 // process its children, adding values to the event object.
119 if (eventParameters != null) {
120 for (Element element : SDNCAdapterUtils.childElements(eventParameters)) {
121 if (!"event-parameter".equals(element.getNodeName())) {
125 String tagName = null;
126 String tagValue = null;
128 for (Element child : SDNCAdapterUtils.childElements(element)) {
129 if ("tag-name".equals(child.getNodeName())) {
130 tagName = child.getTextContent();
131 } else if ("tag-value".equals(child.getNodeName())) {
132 tagValue = child.getTextContent();
136 // tag-name is mandatory
138 if (tagName == null) {
139 throw new ParseException("Missing tag-name in SDNC event parameter", 0);
142 // tag-value is optional. If absent, make it an empty string so we don't
143 // end up with null values in the parameter map.
145 if (tagValue == null) {
149 event.addParam(tagName, tagValue);
154 } catch (ParseException e) {
156 } catch (Exception e) {
157 LOGGER.debug("Exception:", e);
158 throw new ParseException("Failed to parse SDNC event:", 0 );