2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.modelloader.entity.model;
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;
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.List;
37 import javax.xml.XMLConstants;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
42 public class ModelArtifactParser implements IModelParser {
44 private static String MODEL_VER = "model-ver";
45 private static String MODEL_VERSION_ID = "model-version-id";
46 private static String MODEL_INVARIANT_ID = "model-invariant-id";
47 private static String RELATIONSHIP = "relationship";
48 private static String RELATIONSHIP_DATA = "relationship-data";
49 private static String RELATIONSHIP_KEY = "relationship-key";
50 private static String RELATIONSHIP_VALUE = "relationship-value";
51 private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
52 private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id";
54 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
56 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
57 String payload = new String(artifactPayload);
58 List<Artifact> modelList = new ArrayList<Artifact>();
61 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62 DocumentBuilder builder = factory.newDocumentBuilder();
63 InputSource is = new InputSource(new StringReader(payload));
64 Document doc = builder.parse(is);
66 ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
69 logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
70 + "Model-invariant-Id: "+ model.getModelInvariantId()
71 + " Model-Version-Id: "+ model.getModelVerId());
75 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
79 catch (Exception ex) {
80 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
86 private ModelArtifact parseModel(Node modelNode, String payload) {
87 ModelArtifact model = new ModelArtifact();
88 model.setPayload(payload);
90 Element e = (Element)modelNode;
91 model.setModelNamespace(e.getAttribute("xmlns"));
93 parseNode(modelNode, model);
95 if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
102 private void parseNode(Node node, ModelArtifact model) {
103 if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
104 model.setModelInvariantId(node.getTextContent().trim());
106 else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
107 model.setModelVerId(node.getTextContent().trim());
109 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
110 String dependentModelKey = parseRelationshipNode(node, model);
111 if (dependentModelKey != null) {
112 model.addDependentModelId(dependentModelKey);
116 if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
117 model.setModelVer(node);
118 if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) {
119 Element e = (Element) node;
120 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace());
124 NodeList nodeList = node.getChildNodes();
126 for (int i = 0; i < nodeList.getLength(); i++) {
127 Node childNode = nodeList.item(i);
128 parseNode(childNode, model);
133 private String parseRelationshipNode(Node node, ModelArtifact model) {
134 String currentKey = null;
135 String currentValue = null;
136 String modelVersionIdValue = null;
137 String modelInvariantIdValue = null;
139 NodeList nodeList = node.getChildNodes();
140 for (int i = 0; i < nodeList.getLength(); i++) {
141 Node childNode = nodeList.item(i);
143 if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
144 NodeList relDataChildList = childNode.getChildNodes();
146 for (int j = 0; j < relDataChildList.getLength(); j++) {
147 Node relDataChildNode = relDataChildList.item(j);
149 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
150 currentKey = relDataChildNode.getTextContent().trim();
152 if (currentValue != null) {
153 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
154 modelVersionIdValue = currentValue;
156 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
157 modelInvariantIdValue = currentValue;
164 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
165 currentValue = relDataChildNode.getTextContent().trim();
167 if (currentKey != null) {
168 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
169 modelVersionIdValue = currentValue;
171 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
172 modelInvariantIdValue = currentValue;
183 if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
184 return modelInvariantIdValue + "|" + modelVersionIdValue;