added schema validation tools
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / nodes / NodeIngestor.java
index 849ad57..bafc6b3 100644 (file)
@@ -1,14 +1,14 @@
-/**
+/** 
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *    http://www.apache.org/licenses/LICENSE-2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
+
 package org.onap.aai.nodes;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
+import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBException;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
 import org.eclipse.persistence.jaxb.JAXBContextProperties;
 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
@@ -39,6 +49,11 @@ import org.onap.aai.setup.ConfigTranslator;
 import org.onap.aai.setup.Version;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+import com.google.common.base.CaseFormat;
 
 @Component
 /**
@@ -47,6 +62,7 @@ import org.springframework.stereotype.Component;
 public class NodeIngestor {
        
        private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
+       private Map<Version, Set<String>> typesPerVersion = new EnumMap<>(Version.class);
        
        @Autowired
        /**
@@ -64,8 +80,9 @@ public class NodeIngestor {
                                List<String> files = verFiles.getValue();
                                final DynamicJAXBContext ctx = ingest(files);
                                versionContextMap.put(v, ctx);
+                               typesPerVersion.put(v, getAllNodeTypes(files));
                        }
-               } catch (FileNotFoundException | JAXBException e) {
+               } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
                        throw new ExceptionInInitializerError(e);
                }
        }
@@ -91,6 +108,28 @@ public class NodeIngestor {
                properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
                return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
        }
+       
+       private Set<String> getAllNodeTypes(List<String> files) throws ParserConfigurationException, SAXException, IOException {
+               Set<String> types = new HashSet<>();
+               
+               final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+               docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+               final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+               
+               for (String file : files) {
+                       InputStream inputStream = new FileInputStream(file);
+                       
+                       final Document doc = docBuilder.parse(inputStream);
+                       final NodeList list = doc.getElementsByTagName("java-type");
+                       
+                       for (int i = 0; i < list.getLength(); i++) {
+                               String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
+                               types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type));
+                       }
+               }
+               
+               return types;
+       }
 
        /**
         * Gets the DynamicJAXBContext for the given version
@@ -101,4 +140,15 @@ public class NodeIngestor {
        public DynamicJAXBContext getContextForVersion(Version v) {
                return versionContextMap.get(v);
        }
+       
+       /**
+        * Determines if the given version contains the given node type
+        * 
+        * @param String nodeType - node type to check, must be in lower hyphen form (ie "type-name")
+        * @param v
+        * @return
+        */
+       public boolean hasNodeType(String nodeType, Version v) {
+               return typesPerVersion.get(v).contains(nodeType);
+       }
 }