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.util.Arrays;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
33 import javax.xml.XMLConstants;
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import javax.xml.parsers.ParserConfigurationException;
38 import org.onap.aai.edges.EdgeIngestor;
39 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
40 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.setup.SchemaVersion;
43 import org.onap.aai.setup.SchemaVersions;
44 import org.onap.aai.nodes.NodeIngestor;
45 import org.w3c.dom.Attr;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.Element;
48 import org.w3c.dom.NamedNodeMap;
49 import org.w3c.dom.NodeList;
50 import org.xml.sax.SAXException;
52 import org.xml.sax.InputSource;
54 public abstract class OxmFileProcessor {
57 protected Set<String> namespaceFilter;
58 protected File oxmFile;
60 protected SchemaVersion v;
61 protected Document doc = null;
62 protected String apiVersion = null;
63 protected SchemaVersions schemaVersions;
66 protected static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
67 protected static int annotationsMinVersion = 6; // lower versions support annotations in xsd
68 protected static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation
69 protected static int swaggerDiffStartVersion = 1; // minimum version to support difference
70 protected static int swaggerMinBasepath = 6; // minimum version to support difference
73 protected String apiVersionFmt = null;
74 protected HashMap<String, String> generatedJavaType = new HashMap<String, String>();
75 protected HashMap<String, String> appliedPaths = new HashMap<String, String>();
76 protected NodeList javaTypeNodes = null;
78 protected Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
79 private Map<String, String> createJavaTypeDefinitions()
81 StringBuffer aaiInternal = new StringBuffer();
82 StringBuffer nodes = new StringBuffer();
83 Map<String,String> javaTypeDefinitions = new HashMap<String, String>();
84 aaiInternal.append(" aai-internal:\n");
85 aaiInternal.append(" properties:\n");
86 aaiInternal.append(" property-name:\n");
87 aaiInternal.append(" type: string\n");
88 aaiInternal.append(" property-value:\n");
89 aaiInternal.append(" type: string\n");
90 // javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
91 nodes.append(" nodes:\n");
92 nodes.append(" properties:\n");
93 nodes.append(" inventory-item-data:\n");
94 nodes.append(" type: array\n");
95 nodes.append(" items:\n");
96 nodes.append(" $ref: \"#/definitions/inventory-item-data\"\n");
97 javaTypeDefinitions.put("nodes", nodes.toString());
98 return javaTypeDefinitions;
100 static List<String> nodeFilter = createNodeFilter();
101 private static List<String> createNodeFilter()
103 List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes");
107 public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){
108 this.schemaVersions = schemaVersions;
115 public void setOxmVersion(File oxmFile, SchemaVersion v) {
116 this.oxmFile = oxmFile;
120 public void setXmlVersion(String xml, SchemaVersion v) {
125 public void setVersion(SchemaVersion v) {
130 public void setNodeIngestor(NodeIngestor ni) {
134 public void setEdgeIngestor(EdgeIngestor ei) {
138 public SchemaVersions getSchemaVersions() {
139 return schemaVersions;
142 public void setSchemaVersions(SchemaVersions schemaVersions) {
143 this.schemaVersions = schemaVersions;
146 protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, EdgeRuleNotFoundException {
147 if(this.xml != null || this.oxmFile != null ) {
150 if(this.doc == null) {
151 this.doc = ni.getSchema(v);
153 namespaceFilter = new HashSet<>();
155 NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings");
156 Element bindingElement;
157 NodeList javaTypesNodes;
158 Element javaTypesElement;
160 if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) {
161 throw new AAIException("OXM file error: missing <binding-nodes> in " + oxmFile);
164 bindingElement = (Element) bindingsNodes.item(0);
165 javaTypesNodes = bindingElement.getElementsByTagName("java-types");
166 if ( javaTypesNodes.getLength() < 1 ) {
167 throw new AAIException("OXM file error: missing <binding-nodes><java-types> in " + oxmFile);
169 javaTypesElement = (Element) javaTypesNodes.item(0);
171 javaTypeNodes = javaTypesElement.getElementsByTagName("java-type");
172 if ( javaTypeNodes.getLength() < 1 ) {
173 throw new AAIException("OXM file error: missing <binding-nodes><java-types><java-type> in " + oxmFile );
177 private void createDocument() throws ParserConfigurationException, SAXException, IOException, AAIException {
178 DocumentBuilder dBuilder = null;
180 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
181 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
182 dBuilder = dbFactory.newDocumentBuilder();
183 } catch (ParserConfigurationException e) {
188 doc = dBuilder.parse(oxmFile);
190 InputSource isInput = new InputSource(new StringReader(xml));
191 doc = dBuilder.parse(isInput);
193 } catch (SAXException e) {
195 } catch (IOException e) {
200 public abstract String getDocumentHeader();
201 public abstract String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException ;
203 public String getXMLRootElementName(Element javaTypeElement) {
204 String xmlRootElementName=null;
205 NamedNodeMap attributes;
207 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
208 Element valElement = (Element) valNodes.item(0);
209 attributes = valElement.getAttributes();
210 for ( int i = 0; i < attributes.getLength(); ++i ) {
211 Attr attr = (Attr) attributes.item(i);
212 String attrName = attr.getNodeName();
214 String attrValue = attr.getNodeValue();
215 if ( attrName.equals("name"))
216 xmlRootElementName = attrValue;
218 return xmlRootElementName;
221 public String getXmlRootElementName( String javaTypeName )
223 String attrName, attrValue;
225 Element javaTypeElement;
226 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
227 javaTypeElement = (Element) javaTypeNodes.item(i);
228 NamedNodeMap attributes = javaTypeElement.getAttributes();
229 for ( int j = 0; j < attributes.getLength(); ++j ) {
230 attr = (Attr) attributes.item(j);
231 attrName = attr.getNodeName();
232 attrValue = attr.getNodeValue();
233 if ( attrName.equals("name") && attrValue.equals(javaTypeName)) {
234 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
235 Element valElement = (Element) valNodes.item(0);
236 attributes = valElement.getAttributes();
237 for ( int k = 0; k < attributes.getLength(); ++k ) {
238 attr = (Attr) attributes.item(k);
239 attrName = attr.getNodeName();
241 attrValue = attr.getNodeValue();
242 if ( attrName.equals("name"))
251 public Element getJavaTypeElementSwagger( String javaTypeName )
254 String attrName, attrValue;
256 Element javaTypeElement;
257 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
258 javaTypeElement = (Element) javaTypeNodes.item(i);
259 NamedNodeMap attributes = javaTypeElement.getAttributes();
260 for ( int j = 0; j < attributes.getLength(); ++j ) {
261 attr = (Attr) attributes.item(j);
262 attrName = attr.getNodeName();
263 attrValue = attr.getNodeValue();
264 if ( attrName.equals("name") && attrValue.equals(javaTypeName))
265 return javaTypeElement;
268 return (Element) null;
271 public boolean versionSupportsSwaggerDiff( String version) {
272 int ver = new Integer(version.substring(1)).intValue();
273 if ( ver >= HTMLfromOXM.swaggerDiffStartVersion ) {
279 public boolean versionSupportsBasePathProperty( String version) {
280 int ver = new Integer(version.substring(1)).intValue();
281 if ( ver <= HTMLfromOXM.swaggerMinBasepath ) {