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