Change the header to SO
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / sdncrest / SDNCEventParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.adapters.sdnc.sdncrest;
21
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;
26
27 import javax.xml.XMLConstants;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import java.io.StringReader;
30 import java.text.ParseException;
31
32 /**
33  * SDNCConnector for "agnostic" API services.
34  */
35 public class SDNCEventParser {
36         /**
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
41          */
42         public static SDNCEvent parse(String content) throws ParseException {
43                 try {
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);
49
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.
52
53                         Element root = doc.getDocumentElement();
54                         Element configurationEvent = null;
55
56                         for (Element child : SDNCAdapterUtils.childElements(root)) {
57                                 if ("configuration-event".equals(child.getNodeName())) {
58                                         configurationEvent = child;
59                                         break;
60                                 }
61                         }
62
63                         if (configurationEvent == null) {
64                                 throw new ParseException("No configuration-event element in SDNC event", 0);
65                         }
66
67                         // Process the children of configuration-event
68
69                         String eventType = null;
70                         String eventCorrelatorType = null;
71                         String eventCorrelator = null;
72                         Element eventParameters = null;
73
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;
83                                 }
84                         }
85
86                         // event-type is mandatory.
87
88                         if (eventType == null || eventType.isEmpty()) {
89                                 throw new ParseException("No event-type in SDNC event", 0);
90                         }
91
92                         // event-correlator-type is mandatory.
93
94                         if (eventCorrelatorType == null || eventCorrelatorType.isEmpty()) {
95                                 throw new ParseException("No event-correlator-type in SDNC event", 0);
96                         }
97
98                         // event-correlator is mandatory.
99
100                         if (eventCorrelator == null || eventCorrelator.isEmpty()) {
101                                 throw new ParseException("No event-correlator in SDNC event", 0);
102                         }
103
104                         // Create an event object.
105
106                         SDNCEvent event = new SDNCEvent(eventType, eventCorrelatorType, eventCorrelator);
107
108                         // event-parameters is an optional container element.  If present,
109                         // process its children, adding values to the event object.
110
111                         if (eventParameters != null) {
112                                 for (Element element : SDNCAdapterUtils.childElements(eventParameters)) {
113                                         if (!"event-parameter".equals(element.getNodeName())) {
114                                                 continue;
115                                         }
116
117                                         String tagName = null;
118                                         String tagValue = null;
119
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();
125                                                 }
126                                         }
127
128                                         // tag-name is mandatory
129
130                                         if (tagName == null) {
131                                                 throw new ParseException("Missing tag-name in SDNC event parameter", 0);
132                                         }
133
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.
136
137                                         if (tagValue == null) {
138                                                 tagValue = "";
139                                         }
140
141                                         event.addParam(tagName, tagValue);
142                                 }
143                         }
144
145                         return event;
146                 } catch (ParseException e) {
147                         throw e;
148                 } catch (Exception e) {
149                         throw new ParseException("Failed to parse SDNC event", 0);
150                 }
151         }
152
153         // Instantiation is not allowed.
154         private SDNCEventParser() {
155         }
156 }