32b995ae52052ac126a7158abb16a22e4b947c46
[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 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.aai.modelloader.entity.model;
25
26 import java.io.StringReader;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30
31 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
32 import org.onap.aai.cl.api.Logger;
33 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.InputSource;
37
38 public class ModelParserFactory {
39   private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName());
40   
41   private static String MODEL_ELEMENT = "model";
42   private static String NAMED_QUERY_ELEMENT = "named-query";
43   
44         public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) {
45           Document doc = null;
46
47           try {
48             String payload = new String(artifactPayload);
49             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
50             DocumentBuilder builder;
51             builder = factory.newDocumentBuilder();
52             InputSource is = new InputSource(new StringReader(payload));
53             doc = builder.parse(is);
54           } catch (Exception e) {
55             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
56             return null;
57           }
58
59     if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) {
60       return new NamedQueryArtifactParser();
61     }
62     
63     if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) {
64       logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName 
65           + ": Invalid root element: " + doc.getDocumentElement().getNodeName());
66       return null;
67     }
68     
69     Element e = doc.getDocumentElement();
70     String ns = e.getAttribute("xmlns");
71     String[] parts = ns.split("/");
72     
73     if (parts.length < 1) {
74       logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName 
75           + ": Could not parse namespace version");
76       return null;
77     }
78     
79     String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", "");
80     int version = Integer.parseInt(modelNamespaceVersion);
81     
82     if (version > 8) {
83       return new ModelArtifactParser();
84     }
85     
86     return new ModelV8ArtifactParser(); 
87         }
88 }