Update license date and text
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / ModelArtifactParser.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  */
21 package org.onap.aai.modelloader.entity.model;
22
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;
32
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import javax.xml.XMLConstants;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40
41
42 public class ModelArtifactParser implements IModelParser {
43
44         private static String MODEL_VER = "model-ver";
45         private static String MODEL_VERSION_ID = "model-version-id";
46         private static String MODEL_INVARIANT_ID = "model-invariant-id";
47         private static String RELATIONSHIP = "relationship";
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         private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id";
53         
54         private  static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.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             ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
67
68             if (model != null) {
69               logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
70                   + "Model-invariant-Id: "+ model.getModelInvariantId()
71                   + " Model-Version-Id: "+ model.getModelVerId());
72               modelList.add(model);
73             }
74             else {
75               logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
76               return null;
77             }
78           }
79           catch (Exception ex) {
80             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
81           }
82
83           return modelList;
84         }
85
86         private ModelArtifact parseModel(Node modelNode, String payload) {
87           ModelArtifact model = new ModelArtifact();
88           model.setPayload(payload);
89
90           Element e = (Element)modelNode;
91           model.setModelNamespace(e.getAttribute("xmlns"));
92
93           parseNode(modelNode, model);
94
95           if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
96             return null;
97           }
98
99           return model;
100         }
101
102         private void parseNode(Node node, ModelArtifact model) {
103           if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
104             model.setModelInvariantId(node.getTextContent().trim());
105           }
106           else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
107             model.setModelVerId(node.getTextContent().trim());
108           }
109           else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
110             String dependentModelKey = parseRelationshipNode(node, model);
111             if (dependentModelKey != null) {
112               model.addDependentModelId(dependentModelKey);
113             }
114           }
115           else {
116             if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
117               model.setModelVer(node);
118               if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) {
119                 Element e = (Element) node;
120                 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace());
121               }
122             }
123
124             NodeList nodeList = node.getChildNodes();
125
126             for (int i = 0; i < nodeList.getLength(); i++) {
127               Node childNode = nodeList.item(i); 
128               parseNode(childNode, model);
129             }
130           }
131         }
132
133         private String parseRelationshipNode(Node node, ModelArtifact model) {
134           String currentKey = null;
135           String currentValue = null;
136           String modelVersionIdValue = null;
137           String modelInvariantIdValue = null;
138
139           NodeList nodeList = node.getChildNodes();
140           for (int i = 0; i < nodeList.getLength(); i++) {
141             Node childNode = nodeList.item(i);
142             
143             if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
144               NodeList relDataChildList = childNode.getChildNodes();
145               
146               for (int j = 0; j < relDataChildList.getLength(); j++) {
147                 Node relDataChildNode = relDataChildList.item(j);
148                 
149                 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
150                   currentKey = relDataChildNode.getTextContent().trim();
151
152                   if (currentValue != null) {
153                     if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
154                       modelVersionIdValue = currentValue;
155                     }
156                     else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
157                       modelInvariantIdValue = currentValue;
158                     }
159                     
160                     currentKey = null;
161                     currentValue = null;
162                   }
163                 }
164                 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
165                   currentValue = relDataChildNode.getTextContent().trim();
166
167                   if (currentKey != null) {
168               if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
169                 modelVersionIdValue = currentValue;
170               }
171               else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
172                 modelInvariantIdValue = currentValue;
173               }
174               
175               currentKey = null;
176               currentValue = null;
177                   }
178                 }
179               }
180             }
181           }
182           
183           if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
184             return modelInvariantIdValue + "|" + modelVersionIdValue;
185           }
186           
187           return null;
188
189         }
190
191 }