2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.onap.aai.util.genxsd;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.io.StringWriter;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
35 import javax.xml.XMLConstants;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
39 import javax.xml.transform.OutputKeys;
40 import javax.xml.transform.Transformer;
41 import javax.xml.transform.TransformerException;
42 import javax.xml.transform.TransformerFactory;
43 import javax.xml.transform.dom.DOMSource;
44 import javax.xml.transform.stream.StreamResult;
46 import org.onap.aai.edges.EdgeIngestor;
47 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
48 import org.onap.aai.exceptions.AAIException;
50 import org.onap.aai.setup.SchemaVersion;
51 import org.onap.aai.setup.SchemaVersions;
52 import org.onap.aai.nodes.NodeIngestor;
53 import org.w3c.dom.Attr;
54 import org.w3c.dom.Document;
55 import org.w3c.dom.Element;
56 import org.w3c.dom.NamedNodeMap;
57 import org.w3c.dom.Node;
58 import org.w3c.dom.NodeList;
59 import org.xml.sax.SAXException;
61 import org.xml.sax.InputSource;
63 public abstract class OxmFileProcessor {
65 public static final String LINE_SEPARATOR = System.getProperty("line.separator");
66 public static final String DOUBLE_LINE_SEPARATOR = System.getProperty("line.separator") + System.getProperty("line.separator");
70 protected Set<String> namespaceFilter;
71 protected File oxmFile;
73 protected SchemaVersion v;
74 protected Document doc = null;
75 protected String apiVersion = null;
76 protected SchemaVersions schemaVersions;
79 protected static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
80 protected static int annotationsMinVersion = 6; // lower versions support annotations in xsd
81 protected static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation
82 protected static int swaggerDiffStartVersion = 1; // minimum version to support difference
83 protected static int swaggerMinBasepath = 6; // minimum version to support difference
85 protected Map combinedJavaTypes;
88 protected String apiVersionFmt = null;
89 protected HashMap<String, String> generatedJavaType = new HashMap<String, String>();
90 protected HashMap<String, String> appliedPaths = new HashMap<String, String>();
91 protected NodeList javaTypeNodes = null;
93 protected Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
94 private Map<String, String> createJavaTypeDefinitions()
96 StringBuffer aaiInternal = new StringBuffer();
97 StringBuffer nodes = new StringBuffer();
98 Map<String,String> javaTypeDefinitions = new HashMap<String, String>();
99 aaiInternal.append(" aai-internal:\n");
100 aaiInternal.append(" properties:\n");
101 aaiInternal.append(" property-name:\n");
102 aaiInternal.append(" type: string\n");
103 aaiInternal.append(" property-value:\n");
104 aaiInternal.append(" type: string\n");
105 // javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
106 nodes.append(" nodes:\n");
107 nodes.append(" properties:\n");
108 nodes.append(" inventory-item-data:\n");
109 nodes.append(" type: array\n");
110 nodes.append(" items:\n");
111 nodes.append(" $ref: \"#/definitions/inventory-item-data\"\n");
112 javaTypeDefinitions.put("nodes", nodes.toString());
113 return javaTypeDefinitions;
115 static List<String> nodeFilter = createNodeFilter();
116 private static List<String> createNodeFilter()
118 List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes");
122 public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){
123 this.schemaVersions = schemaVersions;
130 public void setOxmVersion(File oxmFile, SchemaVersion v) {
131 this.oxmFile = oxmFile;
135 public void setXmlVersion(String xml, SchemaVersion v) {
140 public void setVersion(SchemaVersion v) {
145 public void setNodeIngestor(NodeIngestor ni) {
149 public void setEdgeIngestor(EdgeIngestor ei) {
153 public SchemaVersions getSchemaVersions() {
154 return schemaVersions;
157 public void setSchemaVersions(SchemaVersions schemaVersions) {
158 this.schemaVersions = schemaVersions;
161 protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, EdgeRuleNotFoundException {
162 if(this.xml != null || this.oxmFile != null ) {
165 if(this.doc == null) {
166 this.doc = ni.getSchema(v);
168 namespaceFilter = new HashSet<>();
170 NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings");
171 Element bindingElement;
172 NodeList javaTypesNodes;
173 Element javaTypesElement;
175 if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) {
176 throw new AAIException("OXM file error: missing <binding-nodes> in " + oxmFile);
179 bindingElement = (Element) bindingsNodes.item(0);
180 javaTypesNodes = bindingElement.getElementsByTagName("java-types");
181 if ( javaTypesNodes.getLength() < 1 ) {
182 throw new AAIException("OXM file error: missing <binding-nodes><java-types> in " + oxmFile);
184 javaTypesElement = (Element) javaTypesNodes.item(0);
186 javaTypeNodes = javaTypesElement.getElementsByTagName("java-type");
187 if ( javaTypeNodes.getLength() < 1 ) {
188 throw new AAIException("OXM file error: missing <binding-nodes><java-types><java-type> in " + oxmFile );
192 private void createDocument() throws ParserConfigurationException, SAXException, IOException, AAIException {
193 DocumentBuilder dBuilder = null;
195 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
196 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
197 dBuilder = dbFactory.newDocumentBuilder();
198 } catch (ParserConfigurationException e) {
203 doc = dBuilder.parse(oxmFile);
205 InputSource isInput = new InputSource(new StringReader(xml));
206 doc = dBuilder.parse(isInput);
208 } catch (SAXException e) {
210 } catch (IOException e) {
215 public abstract String getDocumentHeader();
216 public abstract String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException ;
218 public String getXMLRootElementName(Element javaTypeElement) {
219 String xmlRootElementName=null;
220 NamedNodeMap attributes;
222 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
223 Element valElement = (Element) valNodes.item(0);
224 attributes = valElement.getAttributes();
225 for ( int i = 0; i < attributes.getLength(); ++i ) {
226 Attr attr = (Attr) attributes.item(i);
227 String attrName = attr.getNodeName();
229 String attrValue = attr.getNodeValue();
230 if ( attrName.equals("name"))
231 xmlRootElementName = attrValue;
233 return xmlRootElementName;
236 public String getXmlRootElementName( String javaTypeName )
238 String attrName, attrValue;
240 Element javaTypeElement;
241 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
242 javaTypeElement = (Element) javaTypeNodes.item(i);
243 NamedNodeMap attributes = javaTypeElement.getAttributes();
244 for ( int j = 0; j < attributes.getLength(); ++j ) {
245 attr = (Attr) attributes.item(j);
246 attrName = attr.getNodeName();
247 attrValue = attr.getNodeValue();
248 if ( attrName.equals("name") && attrValue.equals(javaTypeName)) {
249 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
250 Element valElement = (Element) valNodes.item(0);
251 attributes = valElement.getAttributes();
252 for ( int k = 0; k < attributes.getLength(); ++k ) {
253 attr = (Attr) attributes.item(k);
254 attrName = attr.getNodeName();
256 attrValue = attr.getNodeValue();
257 if ( attrName.equals("name"))
266 public Map getCombinedJavaTypes() {
267 return combinedJavaTypes;
270 public void setCombinedJavaTypes(Map combinedJavaTypes) {
271 this.combinedJavaTypes = combinedJavaTypes;
274 public Element getJavaTypeElementSwagger( String javaTypeName )
277 String attrName, attrValue;
279 Element javaTypeElement;
281 List<Element> combineElementList = new ArrayList<Element>();
282 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
283 javaTypeElement = (Element) javaTypeNodes.item(i);
284 NamedNodeMap attributes = javaTypeElement.getAttributes();
285 for ( int j = 0; j < attributes.getLength(); ++j ) {
286 attr = (Attr) attributes.item(j);
287 attrName = attr.getNodeName();
288 attrValue = attr.getNodeValue();
289 if ( attrName.equals("name") && attrValue.equals(javaTypeName)) {
290 combineElementList.add(javaTypeElement);
294 if ( combineElementList.size() == 0 ) {
295 return (Element) null;
296 } else if ( combineElementList.size() > 1 ) {
297 return combineElements( javaTypeName, combineElementList);
299 return combineElementList.get(0);
302 public boolean versionSupportsSwaggerDiff( String version) {
303 int ver = new Integer(version.substring(1)).intValue();
304 if ( ver >= HTMLfromOXM.swaggerDiffStartVersion ) {
310 public boolean versionSupportsBasePathProperty( String version) {
311 int ver = new Integer(version.substring(1)).intValue();
312 if ( ver <= HTMLfromOXM.swaggerMinBasepath ) {
318 protected void updateParentXmlElements(Element parentElement, NodeList moreXmlElementNodes) {
323 Node refChild = null;
324 // find childNode with attributes and no children, insert children before that node
325 childNodes = parentElement.getChildNodes();
326 if ( childNodes == null || childNodes.getLength() == 0 ) {
327 // should not happen since the base parent was chosen if it had children
331 for ( int i = 0; i < childNodes.getLength(); ++i ) {
332 refChild = childNodes.item(i);
333 if ( refChild.hasAttributes() && !refChild.hasChildNodes()) {
339 for ( int i = 0; i < moreXmlElementNodes.getLength(); ++i ) {
340 xmlElement = (Element)moreXmlElementNodes.item(i);
341 childNode = xmlElement.cloneNode(true);
342 parentElement.insertBefore(childNode, refChild);
346 protected Node getXmlPropertiesNode(Element javaTypeElement ) {
347 NodeList nl = javaTypeElement.getChildNodes();
349 for ( int i = 0; i < nl.getLength(); ++i ) {
351 if ( "xml-properties".equals(child.getNodeName())) {
358 protected Node merge( NodeList nl, Node mergeNode ) {
359 NamedNodeMap nnm = mergeNode.getAttributes();
361 NamedNodeMap childNnm;
363 String mergeName = nnm.getNamedItem("name").getNodeValue();
364 String mergeValue = nnm.getNamedItem("value").getNodeValue();
367 for ( int j = 0; j < nl.getLength(); ++j ) {
368 childNode = nl.item(j);
369 if ( "xml-property".equals(childNode.getNodeName())) {
370 childNnm = childNode.getAttributes();
371 childName = childNnm.getNamedItem("name").getNodeValue();
372 childValue = childNnm.getNamedItem("value").getNodeValue();
373 if ( childName.equals(mergeName)) {
375 // keep, replace or update
376 if ( childValue.contains(mergeValue) ) {
379 if ( mergeValue.contains(childValue) ) {
380 childNnm.getNamedItem("value").setTextContent(mergeValue);
383 childNnm.getNamedItem("value").setTextContent(mergeValue + "," + childValue);
388 childNode = mergeNode.cloneNode(true);
392 protected void mergeXmlProperties(Node useChildProperties, NodeList propertiesToMerge ) {
393 NodeList nl = useChildProperties.getChildNodes();
396 for ( int i = 0; i < propertiesToMerge.getLength(); ++i ) {
397 childNode = propertiesToMerge.item(i);
398 if ( "xml-property".equals(childNode.getNodeName()) ) {
399 newNode = merge(nl, childNode);
400 if ( newNode != null ) {
401 useChildProperties.appendChild(newNode);
408 protected void combineXmlProperties(int useElement, List<Element> combineElementList) {
409 // add or update xml-properties to the referenced element from the combined list
410 Element javaTypeElement = combineElementList.get(useElement);
411 NodeList nl = javaTypeElement.getChildNodes();
412 Node useChildProperties = getXmlPropertiesNode( javaTypeElement);
414 Node childProperties;
415 if ( useChildProperties == null ) {
416 // find xml-properties to clone
417 for ( int i = 0; i < combineElementList.size(); ++i ) {
418 if ( i == useElement ) {
421 childProperties = getXmlPropertiesNode(combineElementList.get(i));
422 if ( childProperties != null ) {
423 useChildProperties = childProperties.cloneNode(true);
424 javaTypeElement.appendChild(useChildProperties);
430 // find other xml-properties
431 for ( int i = 0; i < combineElementList.size(); ++i ) {
432 if ( i == useElement|| ( cloneChild >= 0 && i <= cloneChild )) {
435 childProperties = getXmlPropertiesNode(combineElementList.get(i));
436 if ( childProperties == null ) {
439 cnl = childProperties.getChildNodes();
440 mergeXmlProperties( useChildProperties, cnl);
445 protected Element combineElements( String javaTypeName, List<Element> combineElementList ) {
446 Element javaTypeElement;
447 NodeList parentNodes;
448 Element parentElement = null;
449 NodeList xmlElementNodes;
452 if ( combinedJavaTypes.containsKey( javaTypeName) ) {
453 return combineElementList.get((int)combinedJavaTypes.get(javaTypeName));
455 for ( int i = 0; i < combineElementList.size(); ++i ) {
456 javaTypeElement = combineElementList.get(i);
457 parentNodes = javaTypeElement.getElementsByTagName("java-attributes");
458 if ( parentNodes.getLength() == 0 ) {
461 parentElement = (Element)parentNodes.item(0);
462 xmlElementNodes = parentElement.getElementsByTagName("xml-element");
463 if ( xmlElementNodes.getLength() <= 0 ) {
469 boolean doCombineElements = true;
470 if ( useElement < 0 ) {
472 doCombineElements = false;
473 } else if ( useElement == combineElementList.size() - 1) {
474 doCombineElements = false;
476 if ( doCombineElements ) {
477 // get xml-element from other javaTypeElements
478 Element otherParentElement = null;
479 for ( int i = 0; i < combineElementList.size(); ++i ) {
480 if ( i == useElement ) {
483 javaTypeElement = combineElementList.get(i);
484 parentNodes = javaTypeElement.getElementsByTagName("java-attributes");
485 if ( parentNodes.getLength() == 0 ) {
488 otherParentElement = (Element)parentNodes.item(0);
489 xmlElementNodes = otherParentElement.getElementsByTagName("xml-element");
490 if ( xmlElementNodes.getLength() <= 0 ) {
493 // xml-element that are not present
494 updateParentXmlElements( parentElement, xmlElementNodes);
498 // need to combine xml-properties
499 combineXmlProperties(useElement, combineElementList );
500 combinedJavaTypes.put( javaTypeName, useElement);
501 return combineElementList.get(useElement);
505 private static void prettyPrint(Node node, String tab)
509 // Set up the output transformer
510 TransformerFactory transfac = TransformerFactory.newInstance();
511 Transformer trans = transfac.newTransformer();
512 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
513 trans.setOutputProperty(OutputKeys.INDENT, "yes");
514 StringWriter sw = new StringWriter();
515 StreamResult result = new StreamResult(sw);
516 DOMSource source = new DOMSource(node);
517 trans.transform(source, result);
518 String xmlString = sw.toString();
519 System.out.println(xmlString);
521 catch (TransformerException e) {