2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.modelloader.entity.model;
26 import org.openecomp.cl.api.Logger;
27 import org.openecomp.cl.eelf.LoggerFactory;
28 import org.openecomp.modelloader.entity.Artifact;
29 import org.openecomp.modelloader.service.ModelLoaderMsgs;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.xml.sax.InputSource;
37 import java.io.StringReader;
38 import java.util.ArrayList;
39 import java.util.List;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
45 public class NamedQueryArtifactParser implements IModelParser {
47 private static String NAMED_QUERY_VERSION_ID = "named-query-uuid";
48 private static String RELATIONSHIP_DATA = "relationship-data";
49 private static String RELATIONSHIP_KEY = "relationship-key";
50 private static String RELATIONSHIP_VALUE = "relationship-value";
51 private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
54 private static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName());
56 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
57 String payload = new String(artifactPayload);
58 List<Artifact> modelList = new ArrayList<Artifact>();
61 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62 DocumentBuilder builder = factory.newDocumentBuilder();
63 InputSource is = new InputSource(new StringReader(payload));
64 Document doc = builder.parse(is);
66 NamedQueryArtifact model = parseModel(doc.getDocumentElement(), payload);
69 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Named-Query parsed =====>>>> " + "Named-Query-UUID: "+ model.getNamedQueryUuid());
73 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName);
77 catch (Exception ex) {
78 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName + ": " + ex.getLocalizedMessage());
84 private NamedQueryArtifact parseModel(Node modelNode, String payload) {
85 NamedQueryArtifact model = new NamedQueryArtifact();
86 model.setPayload(payload);
88 Element e = (Element)modelNode;
89 model.setModelNamespace(e.getAttribute("xmlns"));
91 parseNode(modelNode, model);
93 if (model.getNamedQueryUuid() == null) {
100 private void parseNode(Node node, NamedQueryArtifact model) {
101 if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) {
102 model.setNamedQueryUuid(node.getTextContent().trim());
104 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
105 parseRelationshipNode(node, model);
108 NodeList nodeList = node.getChildNodes();
109 for (int i = 0; i < nodeList.getLength(); i++) {
110 Node childNode = nodeList.item(i);
111 parseNode(childNode, model);
116 private void parseRelationshipNode(Node node, NamedQueryArtifact model) {
120 NodeList nodeList = node.getChildNodes();
121 for (int i = 0; i < nodeList.getLength(); i++) {
122 Node childNode = nodeList.item(i);
123 if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
124 key = childNode.getTextContent().trim();
126 else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
127 value = childNode.getTextContent().trim();
131 if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) {
133 model.addDependentModelId(value);