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