391142cce6869763c78d77fad252cf883a673354
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / genxsd / OxmFileProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.onap.aai.util.genxsd;
21
22 import java.io.File;
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;
30 import java.util.Map;
31 import java.util.Set;
32
33 import javax.xml.XMLConstants;
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import javax.xml.parsers.ParserConfigurationException;
37
38 import org.onap.aai.edges.EdgeIngestor;
39 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
40 import org.onap.aai.exceptions.AAIException;
41
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;
51
52 import org.xml.sax.InputSource;
53
54 public abstract class OxmFileProcessor {
55         EdgeIngestor ei;
56         NodeIngestor ni;
57         protected Set<String> namespaceFilter;
58         protected File oxmFile;
59         protected String xml;
60         protected SchemaVersion v;
61         protected Document doc = null;
62         protected String apiVersion = null;
63         protected SchemaVersions schemaVersions;
64         
65         
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
71         
72         
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;
77
78         protected Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
79     private Map<String, String> createJavaTypeDefinitions()
80     {
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;
99     }
100         static List<String> nodeFilter = createNodeFilter();
101     private static List<String> createNodeFilter()
102     {
103         List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes");
104         return list;
105     }
106
107     public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){
108         this.schemaVersions = schemaVersions;
109         this.ni = ni;
110                 this.ei = ei;
111         }
112     
113    
114
115         public void setOxmVersion(File oxmFile, SchemaVersion v) {
116                 this.oxmFile = oxmFile;
117                 this.v = v;
118         }
119
120         public void setXmlVersion(String xml, SchemaVersion v) {
121                 this.xml = xml;
122                 this.v = v;
123         }
124         
125         public void setVersion(SchemaVersion v) {
126                 this.oxmFile = null;
127                 this.v = v;
128         }
129         
130         public void setNodeIngestor(NodeIngestor ni) {
131                     this.ni = ni;
132         }
133         
134     public void setEdgeIngestor(EdgeIngestor ei) {
135             this.ei = ei;
136         }
137
138     public SchemaVersions getSchemaVersions() {
139                 return schemaVersions;
140         }
141
142         public void setSchemaVersions(SchemaVersions schemaVersions) {
143                 this.schemaVersions = schemaVersions;
144         }
145         
146         protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, EdgeRuleNotFoundException  {
147                 if(this.xml != null || this.oxmFile != null ) {                 
148                         createDocument();
149                 }
150                 if(this.doc == null) {
151                         this.doc = ni.getSchema(v);
152                 }
153                 namespaceFilter = new HashSet<>();
154                                 
155             NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings");
156                 Element bindingElement;
157                 NodeList javaTypesNodes;
158                 Element javaTypesElement;
159                 
160                 if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) {
161                         throw new AAIException("OXM file error: missing <binding-nodes> in " + oxmFile);
162                 }           
163                 
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);
168                 }
169                 javaTypesElement = (Element) javaTypesNodes.item(0);
170
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 );
174                 }
175         }
176
177         private void createDocument() throws ParserConfigurationException, SAXException, IOException, AAIException {
178                 DocumentBuilder dBuilder = null;
179                 try {   
180                     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
181                     dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
182                     dBuilder = dbFactory.newDocumentBuilder();
183                 } catch (ParserConfigurationException e) {
184                         throw e;
185                 }
186                 try {   
187                     if ( xml == null ) {
188                         doc = dBuilder.parse(oxmFile);
189                     } else {
190                             InputSource isInput = new InputSource(new StringReader(xml));
191                             doc = dBuilder.parse(isInput);
192                     }
193                 } catch (SAXException e) {
194                         throw e;
195                 } catch (IOException e) {
196                         throw e;
197                 }
198                 return;
199         }
200         public abstract String getDocumentHeader();
201         public abstract String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException ;
202         
203         public String getXMLRootElementName(Element javaTypeElement) {
204                 String xmlRootElementName=null;
205                 NamedNodeMap attributes;
206                 
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();
213
214             String attrValue = attr.getNodeValue();
215             if ( attrName.equals("name"))
216                 xmlRootElementName = attrValue;
217                 }
218                 return xmlRootElementName;
219         }
220         
221         public String getXmlRootElementName( String javaTypeName )
222         {
223                 String attrName, attrValue;
224                 Attr attr;
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();
240
241                             attrValue = attr.getNodeValue();
242                             if ( attrName.equals("name"))
243                                 return (attrValue);
244                                 }
245                     }
246                         }
247                 }
248                 return null;
249         }
250         
251         public Element getJavaTypeElementSwagger( String javaTypeName )
252         {
253                 
254                 String attrName, attrValue;
255                 Attr attr;
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;
266                         }
267                 }
268                 return (Element) null;
269         }
270         
271         public boolean versionSupportsSwaggerDiff( String version) {
272                 int ver = new Integer(version.substring(1)).intValue();
273                 if ( ver >= HTMLfromOXM.swaggerDiffStartVersion ) {
274                         return true;
275                 }
276                 return false;
277         }
278         
279         public boolean versionSupportsBasePathProperty( String version) {
280                 int ver = new Integer(version.substring(1)).intValue();
281                 if ( ver <= HTMLfromOXM.swaggerMinBasepath ) {
282                         return true;
283                 }
284                 return false;
285         }
286
287 }