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
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=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.entity.model;
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;
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.XMLConstants;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
45 public class ModelArtifactParser implements IModelParser {
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";
57 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
59 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
60 String payload = new String(artifactPayload);
61 List<Artifact> modelList = new ArrayList<Artifact>();
64 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
65 DocumentBuilder builder = factory.newDocumentBuilder();
66 InputSource is = new InputSource(new StringReader(payload));
67 Document doc = builder.parse(is);
69 ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
72 logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
73 + "Model-invariant-Id: "+ model.getModelInvariantId()
74 + " Model-Version-Id: "+ model.getModelVerId());
78 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
82 catch (Exception ex) {
83 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
89 private ModelArtifact parseModel(Node modelNode, String payload) {
90 ModelArtifact model = new ModelArtifact();
91 model.setPayload(payload);
93 Element e = (Element)modelNode;
94 model.setModelNamespace(e.getAttribute("xmlns"));
96 parseNode(modelNode, model);
98 if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
105 private void parseNode(Node node, ModelArtifact model) {
106 if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
107 model.setModelInvariantId(node.getTextContent().trim());
109 else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
110 model.setModelVerId(node.getTextContent().trim());
112 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
113 String dependentModelKey = parseRelationshipNode(node, model);
114 if (dependentModelKey != null) {
115 model.addDependentModelId(dependentModelKey);
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());
127 NodeList nodeList = node.getChildNodes();
129 for (int i = 0; i < nodeList.getLength(); i++) {
130 Node childNode = nodeList.item(i);
131 parseNode(childNode, model);
136 private String parseRelationshipNode(Node node, ModelArtifact model) {
137 String currentKey = null;
138 String currentValue = null;
139 String modelVersionIdValue = null;
140 String modelInvariantIdValue = null;
142 NodeList nodeList = node.getChildNodes();
143 for (int i = 0; i < nodeList.getLength(); i++) {
144 Node childNode = nodeList.item(i);
146 if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
147 NodeList relDataChildList = childNode.getChildNodes();
149 for (int j = 0; j < relDataChildList.getLength(); j++) {
150 Node relDataChildNode = relDataChildList.item(j);
152 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
153 currentKey = relDataChildNode.getTextContent().trim();
155 if (currentValue != null) {
156 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
157 modelVersionIdValue = currentValue;
159 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
160 modelInvariantIdValue = currentValue;
167 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
168 currentValue = relDataChildNode.getTextContent().trim();
170 if (currentKey != null) {
171 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
172 modelVersionIdValue = currentValue;
174 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
175 modelInvariantIdValue = currentValue;
186 if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
187 return modelInvariantIdValue + "|" + modelVersionIdValue;