Allow handling of legact model artifacts
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / ModelArtifactParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Model Loader
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  * http://www.apache.org/licenses/LICENSE-2.0
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP and OpenECOMP are trademarks
21  * and service marks of AT&T Intellectual Property.
22  */
23 package org.openecomp.modelloader.entity.model;
24
25 import org.openecomp.cl.api.Logger;
26 import org.openecomp.cl.eelf.LoggerFactory;
27 import org.openecomp.modelloader.entity.Artifact;
28 import org.openecomp.modelloader.service.ModelLoaderMsgs;
29
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.XMLConstants;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43
44
45 public class ModelArtifactParser implements IModelParser {
46
47         private static String MODEL_VER = "model-ver";
48         private static String MODEL_VERSION_ID = "model-version-id";
49         private static String MODEL_INVARIANT_ID = "model-invariant-id";
50         private static String RELATIONSHIP = "relationship";
51         private static String RELATIONSHIP_DATA = "relationship-data";
52         private static String RELATIONSHIP_KEY = "relationship-key";
53         private static String RELATIONSHIP_VALUE = "relationship-value";
54         private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
55         private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id";
56         
57         private  static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
58         
59         public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
60           String payload = new String(artifactPayload);
61           List<Artifact> modelList = new ArrayList<Artifact>();
62
63           try {
64             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
65             DocumentBuilder builder = factory.newDocumentBuilder();
66             InputSource is = new InputSource(new StringReader(payload));
67             Document doc = builder.parse(is);
68
69             ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
70
71             if (model != null) {
72               logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
73                   + "Model-invariant-Id: "+ model.getModelInvariantId()
74                   + " Model-Version-Id: "+ model.getModelVerId());
75               modelList.add(model);
76             }
77             else {
78               logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
79               return null;
80             }
81           }
82           catch (Exception ex) {
83             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
84           }
85
86           return modelList;
87         }
88
89         private ModelArtifact parseModel(Node modelNode, String payload) {
90           ModelArtifact model = new ModelArtifact();
91           model.setPayload(payload);
92
93           Element e = (Element)modelNode;
94           model.setModelNamespace(e.getAttribute("xmlns"));
95
96           parseNode(modelNode, model);
97
98           if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
99             return null;
100           }
101
102           return model;
103         }
104
105         private void parseNode(Node node, ModelArtifact model) {
106           if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
107             model.setModelInvariantId(node.getTextContent().trim());
108           }
109           else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
110             model.setModelVerId(node.getTextContent().trim());
111           }
112           else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
113             String dependentModelKey = parseRelationshipNode(node, model);
114             if (dependentModelKey != null) {
115               model.addDependentModelId(dependentModelKey);
116             }
117           }
118           else {
119             if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
120               model.setModelVer(node);
121               if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) {
122                 Element e = (Element) node;
123                 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace());
124               }
125             }
126
127             NodeList nodeList = node.getChildNodes();
128
129             for (int i = 0; i < nodeList.getLength(); i++) {
130               Node childNode = nodeList.item(i); 
131               parseNode(childNode, model);
132             }
133           }
134         }
135
136         private String parseRelationshipNode(Node node, ModelArtifact model) {
137           String currentKey = null;
138           String currentValue = null;
139           String modelVersionIdValue = null;
140           String modelInvariantIdValue = null;
141
142           NodeList nodeList = node.getChildNodes();
143           for (int i = 0; i < nodeList.getLength(); i++) {
144             Node childNode = nodeList.item(i);
145             
146             if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
147               NodeList relDataChildList = childNode.getChildNodes();
148               
149               for (int j = 0; j < relDataChildList.getLength(); j++) {
150                 Node relDataChildNode = relDataChildList.item(j);
151                 
152                 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
153                   currentKey = relDataChildNode.getTextContent().trim();
154
155                   if (currentValue != null) {
156                     if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
157                       modelVersionIdValue = currentValue;
158                     }
159                     else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
160                       modelInvariantIdValue = currentValue;
161                     }
162                     
163                     currentKey = null;
164                     currentValue = null;
165                   }
166                 }
167                 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
168                   currentValue = relDataChildNode.getTextContent().trim();
169
170                   if (currentKey != null) {
171               if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
172                 modelVersionIdValue = currentValue;
173               }
174               else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
175                 modelInvariantIdValue = currentValue;
176               }
177               
178               currentKey = null;
179               currentValue = null;
180                   }
181                 }
182               }
183             }
184           }
185           
186           if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
187             return modelInvariantIdValue + "|" + modelVersionIdValue;
188           }
189           
190           return null;
191
192         }
193
194 }