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