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 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;
29 import org.onap.aai.modelloader.entity.Artifact;
30 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
38 public class ModelArtifactParser extends AbstractModelArtifactParser {
40 public static final String MODEL_VER = "model-ver";
41 public static final String MODEL_VERSION_ID = "model-version-id";
42 public static final String MODEL_INVARIANT_ID = "model-invariant-id";
43 private static final String RELATIONSHIP = "relationship";
44 private static final String MODEL_ELEMENT_RELATIONSHIP_KEY = "model." + MODEL_INVARIANT_ID;
45 private static final String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = MODEL_VER + "." + MODEL_VERSION_ID;
47 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
50 void parseNode(Node node, IModelArtifact model) {
51 if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)
52 || node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) {
53 setVersionId(model, node);
54 } else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) {
55 parseRelationshipNode(node, model);
57 if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) {
58 ((ModelArtifact) model).setModelVer(node);
59 if ((((ModelArtifact) model).getModelNamespace() != null)
60 && (!((ModelArtifact) model).getModelNamespace().isEmpty())) {
61 Element e = (Element) node;
62 e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns",
63 ((ModelArtifact) model).getModelNamespace());
67 parseChildNodes(node, model);
75 void setVersionId(IModelArtifact model, Node node) {
76 if (MODEL_INVARIANT_ID.equals(node.getNodeName())) {
77 ((ModelArtifact) model).setModelInvariantId(node.getTextContent().trim());
78 } else if (MODEL_VERSION_ID.equals(node.getNodeName())) {
79 ((ModelArtifact) model).setModelVerId(node.getTextContent().trim());
87 ModelId buildModelId(NodeList nodeList) {
88 return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item)
89 .filter(childNode -> childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA))
90 .map(this::getRelationship) //
91 .collect(Collector.of(ModelId::new, ModelId::setRelationship, (m, p) -> m));
95 * Find a relationship key and value pair from the children of the supplied node.
97 * @param node containing children storing relationship keys and values
98 * @return a pair containing a relationship key and its value. Note: if multiple relationships are found, existing
99 * values stored in the pair will be overwritten.
101 private Pair<String, String> getRelationship(Node node) {
102 Objects.requireNonNull(node);
103 NodeList relDataChildList = node.getChildNodes();
104 Objects.requireNonNull(relDataChildList);
106 return IntStream.range(0, relDataChildList.getLength()).mapToObj(relDataChildList::item)
107 .filter(this::filterRelationshipNode)
108 .collect(Collector.of(Pair::new, applyRelationshipValue, (p, n) -> p));
112 * This method is responsible for creating an instance of {@link ModelArtifactParser.ModelId}
114 * @return IModelId instance of {@link ModelArtifactParser.ModelId}
117 IModelId createModelIdInstance() {
118 return new ModelId();
121 private class ModelId implements IModelId {
123 private String modelInvariantIdValue;
124 private String modelVersionIdValue;
127 public void setRelationship(Pair<String, String> p) {
128 if (p.getKey().equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) {
129 modelVersionIdValue = p.getValue();
130 } else if (p.getKey().equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) {
131 modelInvariantIdValue = p.getValue();
136 public boolean defined() {
137 return modelInvariantIdValue != null && modelVersionIdValue != null;
141 public String toString() {
142 return modelInvariantIdValue + "|" + modelVersionIdValue;
150 String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) {
151 return "Unable to parse legacy model artifact " + artifactName + ": " + localisedMessage;
158 IModelArtifact createModelArtifactInstance() {
159 return new ModelArtifact();
163 boolean modelIsValid(IModelArtifact model) {
164 return ((ModelArtifact) model).getModelInvariantId() != null && ((ModelArtifact) model).getModelVerId() != null;
171 boolean processParsedModel(List<Artifact> modelList, String artifactName, IModelArtifact model) {
172 boolean valid = false;
175 ModelArtifact modelImpl = (ModelArtifact) model;
176 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-invariant-Id: "
177 + modelImpl.getModelInvariantId() + " Model-Version-Id: " + modelImpl.getModelVerId());
178 modelList.add(modelImpl);
181 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
191 String getModelElementRelationshipKey() {
199 String getVersionIdNodeName() {