2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.aai.modelloader.entity.model;
26 import org.onap.aai.modelloader.entity.Artifact;
27 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
28 import org.openecomp.cl.api.Logger;
29 import org.openecomp.cl.eelf.LoggerFactory;
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;
36 import java.io.StringReader;
37 import java.util.ArrayList;
38 import java.util.List;
40 import javax.xml.parsers.DocumentBuilder;
41 import javax.xml.parsers.DocumentBuilderFactory;
44 public class ModelV8ArtifactParser implements IModelParser {
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";
53 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelV8ArtifactParser.class.getName());
55 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
56 String payload = new String(artifactPayload);
57 List<Artifact> modelList = new ArrayList<Artifact>();
60 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
61 DocumentBuilder builder = factory.newDocumentBuilder();
62 InputSource is = new InputSource(new StringReader(payload));
63 Document doc = builder.parse(is);
65 ModelV8Artifact model = parseModel(doc.getDocumentElement(), payload);
68 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-Named-Version-Id: "+ model.getModelNameVersionId());
72 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName);
76 catch (Exception ex) {
77 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName + ": " + ex.getLocalizedMessage());
83 private ModelV8Artifact parseModel(Node modelNode, String payload) {
84 ModelV8Artifact model = new ModelV8Artifact();
85 model.setPayload(payload);
87 Element e = (Element)modelNode;
88 model.setModelNamespace(e.getAttribute("xmlns"));
90 parseNode(modelNode, model);
92 if (model.getModelNameVersionId() == null) {
99 private void parseNode(Node node, ModelV8Artifact model) {
100 if (node.getNodeName().equalsIgnoreCase(MODEL_NAME_VERSION_ID)) {
101 model.setModelNameVersionId(node.getTextContent().trim());
103 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
104 parseRelationshipNode(node, model);
107 NodeList nodeList = node.getChildNodes();
108 for (int i = 0; i < nodeList.getLength(); i++) {
109 Node childNode = nodeList.item(i);
110 parseNode(childNode, model);
115 private void parseRelationshipNode(Node node, ModelV8Artifact model) {
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();
125 else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
126 value = childNode.getTextContent().trim();
130 if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) {
132 model.addDependentModelId(value);