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 org.onap.aai.modelloader.entity.Artifact;
24 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 import org.xml.sax.InputSource;
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.List;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
41 public class NamedQueryArtifactParser implements IModelParser {
43 private static String NAMED_QUERY_VERSION_ID = "named-query-uuid";
44 private static String RELATIONSHIP_DATA = "relationship-data";
45 private static String RELATIONSHIP_KEY = "relationship-key";
46 private static String RELATIONSHIP_VALUE = "relationship-value";
47 private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
50 private static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName());
52 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
53 String payload = new String(artifactPayload);
54 List<Artifact> modelList = new ArrayList<Artifact>();
57 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
58 DocumentBuilder builder = factory.newDocumentBuilder();
59 InputSource is = new InputSource(new StringReader(payload));
60 Document doc = builder.parse(is);
62 NamedQueryArtifact model = parseModel(doc.getDocumentElement(), payload);
65 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Named-Query parsed =====>>>> " + "Named-Query-UUID: "+ model.getNamedQueryUuid());
69 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName);
73 catch (Exception ex) {
74 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName + ": " + ex.getLocalizedMessage());
80 private NamedQueryArtifact parseModel(Node modelNode, String payload) {
81 NamedQueryArtifact model = new NamedQueryArtifact();
82 model.setPayload(payload);
84 Element e = (Element)modelNode;
85 model.setModelNamespace(e.getAttribute("xmlns"));
87 parseNode(modelNode, model);
89 if (model.getNamedQueryUuid() == null) {
96 private void parseNode(Node node, NamedQueryArtifact model) {
97 if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) {
98 model.setNamedQueryUuid(node.getTextContent().trim());
100 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
101 parseRelationshipNode(node, model);
104 NodeList nodeList = node.getChildNodes();
105 for (int i = 0; i < nodeList.getLength(); i++) {
106 Node childNode = nodeList.item(i);
107 parseNode(childNode, model);
112 private void parseRelationshipNode(Node node, NamedQueryArtifact model) {
116 NodeList nodeList = node.getChildNodes();
117 for (int i = 0; i < nodeList.getLength(); i++) {
118 Node childNode = nodeList.item(i);
119 if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
120 key = childNode.getTextContent().trim();
122 else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
123 value = childNode.getTextContent().trim();
127 if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) {
129 model.addDependentModelId(value);