2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.modelloader.entity.model;
23 import java.io.StringReader;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
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;
35 public class ModelParserFactory {
36 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName());
38 private static String MODEL_ELEMENT = "model";
39 private static String NAMED_QUERY_ELEMENT = "named-query";
41 public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) {
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);
56 if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) {
57 return new NamedQueryArtifactParser();
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());
66 Element e = doc.getDocumentElement();
67 String ns = e.getAttribute("xmlns");
68 String[] parts = ns.split("/");
70 if (parts.length < 1) {
71 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName
72 + ": Could not parse namespace version");
76 String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", "");
77 int version = Integer.parseInt(modelNamespaceVersion);
80 return new ModelArtifactParser();
83 return new ModelV8ArtifactParser();