Update license date and text
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / ModelParserFactory.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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.onap.aai.modelloader.entity.model;
22
23 import java.io.StringReader;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27
28 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.xml.sax.InputSource;
34
35 public class ModelParserFactory {
36   private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName());
37   
38   private static String MODEL_ELEMENT = "model";
39   private static String NAMED_QUERY_ELEMENT = "named-query";
40   
41         public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) {
42           Document doc = null;
43
44           try {
45             String payload = new String(artifactPayload);
46             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
47             DocumentBuilder builder;
48             builder = factory.newDocumentBuilder();
49             InputSource is = new InputSource(new StringReader(payload));
50             doc = builder.parse(is);
51           } catch (Exception e) {
52             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
53             return null;
54           }
55
56     if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) {
57       return new NamedQueryArtifactParser();
58     }
59     
60     if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) {
61       logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName 
62           + ": Invalid root element: " + doc.getDocumentElement().getNodeName());
63       return null;
64     }
65     
66     Element e = doc.getDocumentElement();
67     String ns = e.getAttribute("xmlns");
68     String[] parts = ns.split("/");
69     
70     if (parts.length < 1) {
71       logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName 
72           + ": Could not parse namespace version");
73       return null;
74     }
75     
76     String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", "");
77     int version = Integer.parseInt(modelNamespaceVersion);
78     
79     if (version > 8) {
80       return new ModelArtifactParser();
81     }
82     
83     return new ModelV8ArtifactParser(); 
84         }
85 }