2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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 java.util.List;
24 import java.util.Objects;
25 import java.util.stream.Collector;
26 import java.util.stream.IntStream;
27 import javax.xml.XMLConstants;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.modelloader.entity.Artifact;
31 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
36 public class ModelArtifactParser extends AbstractModelArtifactParser {
38 public static final String MODEL_VER = "model-ver";
39 public static final String MODEL_VERSION_ID = "model-version-id";
40 public static final String MODEL_INVARIANT_ID = "model-invariant-id";
41 private static final String RELATIONSHIP = "relationship";
42 private static final String MODEL_ELEMENT_RELATIONSHIP_KEY = "model." + MODEL_INVARIANT_ID;
43 private static final String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = MODEL_VER + "." + MODEL_VERSION_ID;
45 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
48 void parseNode(Node node, IModelArtifact model) {
49 if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)
50 || node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
51 setVersionId(model, node);
52 } else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
53 parseRelationshipNode(node, model);
55 if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
56 ((ModelArtifact) model).setModelVer(node);
57 if ((((ModelArtifact) model).getModelNamespace() != null)
58 && (!((ModelArtifact) model).getModelNamespace().isEmpty())) {
59 Element e = (Element) node;
60 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns",
61 ((ModelArtifact) model).getModelNamespace());
65 parseChildNodes(node, model);
73 void setVersionId(IModelArtifact model, Node node) {
74 if (MODEL_INVARIANT_ID.equals(node.getNodeName())) {
75 ((ModelArtifact) model).setModelInvariantId(node.getTextContent().trim());
76 } else if (MODEL_VERSION_ID.equals(node.getNodeName())) {
77 ((ModelArtifact) model).setModelVerId(node.getTextContent().trim());
85 ModelId buildModelId(NodeList nodeList) {
86 return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item)
87 .filter(childNode -> childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA))
88 .map(this::getRelationship) //
89 .collect(Collector.of(ModelId::new, ModelId::setRelationship, (m, p) -> m));
93 * Find a relationship key and value pair from the children of the supplied node.
95 * @param node containing children storing relationship keys and values
96 * @return a pair containing a relationship key and its value. Note: if multiple relationships are found, existing
97 * values stored in the pair will be overwritten.
99 private Pair<String, String> getRelationship(Node node) {
100 Objects.requireNonNull(node);
101 NodeList relDataChildList = node.getChildNodes();
102 Objects.requireNonNull(relDataChildList);
104 return IntStream.range(0, relDataChildList.getLength()).mapToObj(relDataChildList::item)
105 .filter(this::filterRelationshipNode)
106 .collect(Collector.of(Pair::new, applyRelationshipValue, (p, n) -> p));
110 * This method is responsible for creating an instance of {@link ModelArtifactParser.ModelId}
112 * @return IModelId instance of {@link ModelArtifactParser.ModelId}
115 IModelId createModelIdInstance() {
116 return new ModelId();
119 private class ModelId implements IModelId {
121 private String modelInvariantIdValue;
122 private String modelVersionIdValue;
125 public void setRelationship(Pair<String, String> p) {
126 if (p.getKey().equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
127 modelVersionIdValue = p.getValue();
128 } else if (p.getKey().equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
129 modelInvariantIdValue = p.getValue();
134 public boolean defined() {
135 return modelInvariantIdValue != null && modelVersionIdValue != null;
139 public String toString() {
140 return modelInvariantIdValue + "|" + modelVersionIdValue;
148 String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) {
149 return "Unable to parse legacy model artifact " + artifactName + ": " + localisedMessage;
156 IModelArtifact createModelArtifactInstance() {
157 return new ModelArtifact();
161 boolean modelIsValid(IModelArtifact model) {
162 return ((ModelArtifact) model).getModelInvariantId() != null && ((ModelArtifact) model).getModelVerId() != null;
169 boolean processParsedModel(List<Artifact> modelList, String artifactName, IModelArtifact model) {
170 boolean valid = false;
173 ModelArtifact modelImpl = (ModelArtifact) model;
174 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-invariant-Id: "
175 + modelImpl.getModelInvariantId() + " Model-Version-Id: " + modelImpl.getModelVerId());
176 modelList.add(modelImpl);
179 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
189 String getModelElementRelationshipKey() {
197 String getVersionIdNodeName() {