summaryrefslogtreecommitdiffstats
path: root/adapters/mso-sdnc-adapter/src/main/java/org/openecomp/mso/adapters/sdnc/sdncrest/SDNCEventParser.java
blob: 9808f48e7eb57f20ff90349b5810dab9c3971ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.mso.adapters.sdnc.sdncrest;

import org.openecomp.mso.adapters.sdncrest.SDNCEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.text.ParseException;

/**
 * SDNCConnector for "agnostic" API services.
 */
public class SDNCEventParser {
	/**
	 * Parses SDNC event XML. If the content can be parsed and contains all required
	 * elements, then an object is returned. Otherwise, a ParseException is thrown.
	 * This method performs no logging or alarming.
	 * @throws ParseException on error
	 */
	public static SDNCEvent parse(String content) throws ParseException {
		try {
			// Note: this document builder is not namespace-aware, so namespaces are ignored.
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
			InputSource source = new InputSource(new StringReader(content));
			Document doc = documentBuilderFactory.newDocumentBuilder().parse(source);

			// Find the configuration-event child under the root element.
			// The root element is expected to be an "output" element, but we don't really care.

			Element root = doc.getDocumentElement();
			Element configurationEvent = null;

			for (Element child : SDNCAdapterUtils.childElements(root)) {
				if ("configuration-event".equals(child.getNodeName())) {
					configurationEvent = child;
					break;
				}
			}

			if (configurationEvent == null) {
				throw new ParseException("No configuration-event element in SDNC event", 0);
			}

			// Process the children of configuration-event

			String eventType = null;
			String eventCorrelatorType = null;
			String eventCorrelator = null;
			Element eventParameters = null;

			for (Element child : SDNCAdapterUtils.childElements(configurationEvent)) {
				if ("event-type".equals(child.getNodeName())) {
					eventType = child.getTextContent();
				} else if ("event-correlator-type".equals(child.getNodeName())) {
					eventCorrelatorType = child.getTextContent();
				} else if ("event-correlator".equals(child.getNodeName())) {
					eventCorrelator = child.getTextContent();
				} else if ("event-parameters".equals(child.getNodeName())) {
					eventParameters = (Element) child;
				}
			}

			// event-type is mandatory.

			if (eventType == null || eventType.isEmpty()) {
				throw new ParseException("No event-type in SDNC event", 0);
			}

			// event-correlator-type is mandatory.

			if (eventCorrelatorType == null || eventCorrelatorType.isEmpty()) {
				throw new ParseException("No event-correlator-type in SDNC event", 0);
			}

			// event-correlator is mandatory.

			if (eventCorrelator == null || eventCorrelator.isEmpty()) {
				throw new ParseException("No event-correlator in SDNC event", 0);
			}

			// Create an event object.

			SDNCEvent event = new SDNCEvent(eventType, eventCorrelatorType, eventCorrelator);

			// event-parameters is an optional container element.  If present,
			// process its children, adding values to the event object.

			if (eventParameters != null) {
				for (Element element : SDNCAdapterUtils.childElements(eventParameters)) {
					if (!"event-parameter".equals(element.getNodeName())) {
						continue;
					}

					String tagName = null;
					String tagValue = null;

					for (Element child : SDNCAdapterUtils.childElements(element)) {
						if ("tag-name".equals(child.getNodeName())) {
							tagName = child.getTextContent();
						} else if ("tag-value".equals(child.getNodeName())) {
							tagValue = child.getTextContent();
						}
					}

					// tag-name is mandatory

					if (tagName == null) {
						throw new ParseException("Missing tag-name in SDNC event parameter", 0);
					}

					// tag-value is optional.  If absent, make it an empty string so we don't
					// end up with null values in the parameter map.

					if (tagValue == null) {
						tagValue = "";
					}

					event.addParam(tagName, tagValue);
				}
			}

			return event;
		} catch (ParseException e) {
			throw e;
		} catch (Exception e) {
			throw new ParseException("Failed to parse SDNC event", 0);
		}
	}

	// Instantiation is not allowed.
	private SDNCEventParser() {
	}
}