Merge "Reorder modifiers"
[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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.openecomp.mso.adapters.sdnc.sdncrest;
22
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;
28
29 import javax.xml.XMLConstants;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import java.io.StringReader;
32 import java.text.ParseException;
33
34 /**
35  * SDNCConnector for "agnostic" API services.
36  */
37 public class SDNCEventParser {
38     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
39     
40  // Instantiation is not allowed.
41     private SDNCEventParser() {
42     }
43     
44     /**
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
49          */
50         public static SDNCEvent parse(String content) throws ParseException {
51                 try {
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);
57
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.
60
61                         Element root = doc.getDocumentElement();
62                         Element configurationEvent = null;
63
64                         for (Element child : SDNCAdapterUtils.childElements(root)) {
65                                 if ("configuration-event".equals(child.getNodeName())) {
66                                         configurationEvent = child;
67                                         break;
68                                 }
69                         }
70
71                         if (configurationEvent == null) {
72                                 throw new ParseException("No configuration-event element in SDNC event", 0);
73                         }
74
75                         // Process the children of configuration-event
76
77                         String eventType = null;
78                         String eventCorrelatorType = null;
79                         String eventCorrelator = null;
80                         Element eventParameters = null;
81
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;
91                                 }
92                         }
93
94                         // event-type is mandatory.
95
96                         if (eventType == null || eventType.isEmpty()) {
97                                 throw new ParseException("No event-type in SDNC event", 0);
98                         }
99
100                         // event-correlator-type is mandatory.
101
102                         if (eventCorrelatorType == null || eventCorrelatorType.isEmpty()) {
103                                 throw new ParseException("No event-correlator-type in SDNC event", 0);
104                         }
105
106                         // event-correlator is mandatory.
107
108                         if (eventCorrelator == null || eventCorrelator.isEmpty()) {
109                                 throw new ParseException("No event-correlator in SDNC event", 0);
110                         }
111
112                         // Create an event object.
113
114                         SDNCEvent event = new SDNCEvent(eventType, eventCorrelatorType, eventCorrelator);
115
116                         // event-parameters is an optional container element.  If present,
117                         // process its children, adding values to the event object.
118
119                         if (eventParameters != null) {
120                                 for (Element element : SDNCAdapterUtils.childElements(eventParameters)) {
121                                         if (!"event-parameter".equals(element.getNodeName())) {
122                                                 continue;
123                                         }
124
125                                         String tagName = null;
126                                         String tagValue = null;
127
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();
133                                                 }
134                                         }
135
136                                         // tag-name is mandatory
137
138                                         if (tagName == null) {
139                                                 throw new ParseException("Missing tag-name in SDNC event parameter", 0);
140                                         }
141
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.
144
145                                         if (tagValue == null) {
146                                                 tagValue = "";
147                                         }
148
149                                         event.addParam(tagName, tagValue);
150                                 }
151                         }
152
153                         return event;
154                 } catch (ParseException e) {
155                         throw e;
156                 } catch (Exception e) {
157                     LOGGER.debug("Exception:", e);
158                         throw new ParseException("Failed to parse SDNC event:", 0 );
159                 }
160         }
161 }