Allow handling of legact model artifacts
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / ModelV8ArtifactParser.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.parsers.DocumentBuilder;
41 import javax.xml.parsers.DocumentBuilderFactory;
42
43
44 public class ModelV8ArtifactParser implements IModelParser {
45
46         private static String MODEL_NAME_VERSION_ID = "model-name-version-id";
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-name-version-id";
51
52         
53         private  static Logger logger = LoggerFactory.getInstance().getLogger(ModelV8ArtifactParser.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             ModelV8Artifact model = parseModel(doc.getDocumentElement(), payload);
66
67             if (model != null) {
68               logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-Named-Version-Id: "+ model.getModelNameVersionId());
69               modelList.add(model);
70             }
71             else {
72               logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName);
73               return null;
74             }
75           }
76           catch (Exception ex) {
77             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName + ": " + ex.getLocalizedMessage());
78           }
79
80           return modelList;
81         }
82
83         private ModelV8Artifact parseModel(Node modelNode, String payload) {
84           ModelV8Artifact model = new ModelV8Artifact();
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.getModelNameVersionId() == null) {
93       return null;
94     }
95
96           return model;
97         }
98
99         private void parseNode(Node node, ModelV8Artifact model) {
100           if (node.getNodeName().equalsIgnoreCase(MODEL_NAME_VERSION_ID)) {
101             model.setModelNameVersionId(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, ModelV8Artifact 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 }