Remove OpenECOMP from license file
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / NamedQueryArtifactParser.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.openecomp.modelloader.entity.model;
25
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;
30
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;
36
37 import java.io.StringReader;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43
44
45 public class NamedQueryArtifactParser implements IModelParser {
46
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";
52
53         
54         private  static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName());
55         
56         public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
57           String payload = new String(artifactPayload);
58           List<Artifact> modelList = new ArrayList<Artifact>();
59
60           try {
61             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
62             DocumentBuilder builder = factory.newDocumentBuilder();
63             InputSource is = new InputSource(new StringReader(payload));
64             Document doc = builder.parse(is);
65
66             NamedQueryArtifact model = parseModel(doc.getDocumentElement(), payload);
67
68             if (model != null) {
69               logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Named-Query parsed =====>>>> " + "Named-Query-UUID: "+ model.getNamedQueryUuid());
70               modelList.add(model);
71             }
72             else {
73               logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName);
74               return null;
75             }
76           }
77           catch (Exception ex) {
78             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName + ": " + ex.getLocalizedMessage());
79           }
80
81           return modelList;
82         }
83
84         private NamedQueryArtifact parseModel(Node modelNode, String payload) {
85           NamedQueryArtifact model = new NamedQueryArtifact();
86           model.setPayload(payload);
87
88           Element e = (Element)modelNode;
89           model.setModelNamespace(e.getAttribute("xmlns"));
90
91           parseNode(modelNode, model);
92
93     if (model.getNamedQueryUuid() == null) {
94       return null;
95     }
96
97           return model;
98         }
99
100         private void parseNode(Node node, NamedQueryArtifact model) {
101           if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) {
102             model.setNamedQueryUuid(node.getTextContent().trim());
103           }
104           else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
105             parseRelationshipNode(node, model);
106           }
107           else {
108             NodeList nodeList = node.getChildNodes();
109             for (int i = 0; i < nodeList.getLength(); i++) {
110               Node childNode = nodeList.item(i);
111               parseNode(childNode, model);
112             }
113           }
114         }
115
116         private void parseRelationshipNode(Node node, NamedQueryArtifact model) {
117     String key = null;
118     String value = null;
119     
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();
125       }
126       else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
127         value = childNode.getTextContent().trim();
128       }
129     }
130     
131     if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) {
132       if (value != null) {
133         model.addDependentModelId(value);
134       }
135     }
136         }
137 }