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.openecomp.modelloader.entity.model;
26 import org.openecomp.cl.api.Logger;
27 import org.openecomp.cl.eelf.LoggerFactory;
28 import org.openecomp.modelloader.entity.Artifact;
29 import org.openecomp.modelloader.service.ModelLoaderMsgs;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.xml.sax.InputSource;
37 import java.io.StringReader;
38 import java.util.ArrayList;
39 import java.util.List;
41 import javax.xml.XMLConstants;
42 import javax.xml.parsers.DocumentBuilder;
43 import javax.xml.parsers.DocumentBuilderFactory;
46 public class ModelArtifactParser implements IModelParser {
48 private static String MODEL_VER = "model-ver";
49 private static String MODEL_VERSION_ID = "model-version-id";
50 private static String MODEL_INVARIANT_ID = "model-invariant-id";
51 private static String RELATIONSHIP = "relationship";
52 private static String RELATIONSHIP_DATA = "relationship-data";
53 private static String RELATIONSHIP_KEY = "relationship-key";
54 private static String RELATIONSHIP_VALUE = "relationship-value";
55 private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id";
56 private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id";
58 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
60 public List<Artifact> parse(byte[] artifactPayload, String artifactName) {
61 String payload = new String(artifactPayload);
62 List<Artifact> modelList = new ArrayList<Artifact>();
65 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66 DocumentBuilder builder = factory.newDocumentBuilder();
67 InputSource is = new InputSource(new StringReader(payload));
68 Document doc = builder.parse(is);
70 ModelArtifact model = parseModel(doc.getDocumentElement(), payload);
73 logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> "
74 + "Model-invariant-Id: "+ model.getModelInvariantId()
75 + " Model-Version-Id: "+ model.getModelVerId());
79 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
83 catch (Exception ex) {
84 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage());
90 private ModelArtifact parseModel(Node modelNode, String payload) {
91 ModelArtifact model = new ModelArtifact();
92 model.setPayload(payload);
94 Element e = (Element)modelNode;
95 model.setModelNamespace(e.getAttribute("xmlns"));
97 parseNode(modelNode, model);
99 if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){
106 private void parseNode(Node node, ModelArtifact model) {
107 if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) {
108 model.setModelInvariantId(node.getTextContent().trim());
110 else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
111 model.setModelVerId(node.getTextContent().trim());
113 else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
114 String dependentModelKey = parseRelationshipNode(node, model);
115 if (dependentModelKey != null) {
116 model.addDependentModelId(dependentModelKey);
120 if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
121 model.setModelVer(node);
122 if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) {
123 Element e = (Element) node;
124 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace());
128 NodeList nodeList = node.getChildNodes();
130 for (int i = 0; i < nodeList.getLength(); i++) {
131 Node childNode = nodeList.item(i);
132 parseNode(childNode, model);
137 private String parseRelationshipNode(Node node, ModelArtifact model) {
138 String currentKey = null;
139 String currentValue = null;
140 String modelVersionIdValue = null;
141 String modelInvariantIdValue = null;
143 NodeList nodeList = node.getChildNodes();
144 for (int i = 0; i < nodeList.getLength(); i++) {
145 Node childNode = nodeList.item(i);
147 if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) {
148 NodeList relDataChildList = childNode.getChildNodes();
150 for (int j = 0; j < relDataChildList.getLength(); j++) {
151 Node relDataChildNode = relDataChildList.item(j);
153 if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) {
154 currentKey = relDataChildNode.getTextContent().trim();
156 if (currentValue != null) {
157 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
158 modelVersionIdValue = currentValue;
160 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
161 modelInvariantIdValue = currentValue;
168 else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) {
169 currentValue = relDataChildNode.getTextContent().trim();
171 if (currentKey != null) {
172 if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
173 modelVersionIdValue = currentValue;
175 else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
176 modelInvariantIdValue = currentValue;
187 if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) {
188 return modelInvariantIdValue + "|" + modelVersionIdValue;