Remove OpenECOMP from license file
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / ModelArtifactParser.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.XMLConstants;
42 import javax.xml.parsers.DocumentBuilder;
43 import javax.xml.parsers.DocumentBuilderFactory;
44
45
46 public class ModelArtifactParser implements IModelParser {
47
48         private static String MODEL_VER = "model-ver";
49         private static String MODEL_VERSION_ID = "model-version-id";
50         private static String MODEL_INVARIANT_ID = "model-invariant-id";
51         private static String RELATIONSHIP = "relationship";
52         private static String RELATIONSHIP_DATA = "relationship-data";
53         private static String RELATIONSHIP_KEY = "relationship-key";
54         private static String RELATIONSHIP_VALUE = "relationship-value";
55         private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
56         private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id";
57         
58         private  static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
59         
60         public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
61           String payload = new String(artifactPayload);
62           List<Artifact> modelList = new ArrayList<Artifact>();
63
64           try {
65             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66             DocumentBuilder builder = factory.newDocumentBuilder();
67             InputSource is = new InputSource(new StringReader(payload));
68             Document doc = builder.parse(is);
69
70             ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
71
72             if (model != null) {
73               logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
74                   + "Model-invariant-Id: "+ model.getModelInvariantId()
75                   + " Model-Version-Id: "+ model.getModelVerId());
76               modelList.add(model);
77             }
78             else {
79               logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
80               return null;
81             }
82           }
83           catch (Exception ex) {
84             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
85           }
86
87           return modelList;
88         }
89
90         private ModelArtifact parseModel(Node modelNode, String payload) {
91           ModelArtifact model = new ModelArtifact();
92           model.setPayload(payload);
93
94           Element e = (Element)modelNode;
95           model.setModelNamespace(e.getAttribute("xmlns"));
96
97           parseNode(modelNode, model);
98
99           if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
100             return null;
101           }
102
103           return model;
104         }
105
106         private void parseNode(Node node, ModelArtifact model) {
107           if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
108             model.setModelInvariantId(node.getTextContent().trim());
109           }
110           else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
111             model.setModelVerId(node.getTextContent().trim());
112           }
113           else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
114             String dependentModelKey = parseRelationshipNode(node, model);
115             if (dependentModelKey != null) {
116               model.addDependentModelId(dependentModelKey);
117             }
118           }
119           else {
120             if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
121               model.setModelVer(node);
122               if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) {
123                 Element e = (Element) node;
124                 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace());
125               }
126             }
127
128             NodeList nodeList = node.getChildNodes();
129
130             for (int i = 0; i < nodeList.getLength(); i++) {
131               Node childNode = nodeList.item(i); 
132               parseNode(childNode, model);
133             }
134           }
135         }
136
137         private String parseRelationshipNode(Node node, ModelArtifact model) {
138           String currentKey = null;
139           String currentValue = null;
140           String modelVersionIdValue = null;
141           String modelInvariantIdValue = null;
142
143           NodeList nodeList = node.getChildNodes();
144           for (int i = 0; i < nodeList.getLength(); i++) {
145             Node childNode = nodeList.item(i);
146             
147             if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
148               NodeList relDataChildList = childNode.getChildNodes();
149               
150               for (int j = 0; j < relDataChildList.getLength(); j++) {
151                 Node relDataChildNode = relDataChildList.item(j);
152                 
153                 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
154                   currentKey = relDataChildNode.getTextContent().trim();
155
156                   if (currentValue != null) {
157                     if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
158                       modelVersionIdValue = currentValue;
159                     }
160                     else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
161                       modelInvariantIdValue = currentValue;
162                     }
163                     
164                     currentKey = null;
165                     currentValue = null;
166                   }
167                 }
168                 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
169                   currentValue = relDataChildNode.getTextContent().trim();
170
171                   if (currentKey != null) {
172               if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
173                 modelVersionIdValue = currentValue;
174               }
175               else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
176                 modelInvariantIdValue = currentValue;
177               }
178               
179               currentKey = null;
180               currentValue = null;
181                   }
182                 }
183               }
184             }
185           }
186           
187           if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
188             return modelInvariantIdValue + "|" + modelVersionIdValue;
189           }
190           
191           return null;
192
193         }
194
195 }