X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fentity%2Fmodel%2FModelArtifactParser.java;h=adab6dfd645528fffba1facbbd47bc168d90478d;hb=c5aea4a8bc398fc1c6220875e55b9520fd7f7524;hp=a362fce2a87319b2acf9702781cb4b6299709ca4;hpb=059f42e1a2289ecb2cb5835b8b14a0d098c9e1d3;p=aai%2Fmodel-loader.git diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java index a362fce..adab6df 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java @@ -1,16 +1,15 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. - * Copyright © 2017 Amdocs - * All rights reserved. + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -18,177 +17,184 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. */ package org.onap.aai.modelloader.entity.model; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collector; +import java.util.stream.IntStream; +import javax.xml.XMLConstants; +import org.onap.aai.cl.api.Logger; +import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.service.ModelLoaderMsgs; -import org.openecomp.cl.api.Logger; -import org.openecomp.cl.eelf.LoggerFactory; -import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; - -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - - -public class ModelArtifactParser implements IModelParser { - - private static String MODEL_VER = "model-ver"; - private static String MODEL_VERSION_ID = "model-version-id"; - private static String MODEL_INVARIANT_ID = "model-invariant-id"; - private static String RELATIONSHIP = "relationship"; - private static String RELATIONSHIP_DATA = "relationship-data"; - private static String RELATIONSHIP_KEY = "relationship-key"; - private static String RELATIONSHIP_VALUE = "relationship-value"; - private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id"; - private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id"; - - private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName()); - - public List parse(byte[] artifactPayload, String artifactName) { - String payload = new String(artifactPayload); - List modelList = new ArrayList(); - - try { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(payload)); - Document doc = builder.parse(is); - - ModelArtifact model = parseModel(doc.getDocumentElement(), payload); - - if (model != null) { - logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " - + "Model-invariant-Id: "+ model.getModelInvariantId() - + " Model-Version-Id: "+ model.getModelVerId()); - modelList.add(model); - } - else { - logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName); - return null; - } - } - catch (Exception ex) { - logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage()); - } - - return modelList; - } - - private ModelArtifact parseModel(Node modelNode, String payload) { - ModelArtifact model = new ModelArtifact(); - model.setPayload(payload); - - Element e = (Element)modelNode; - model.setModelNamespace(e.getAttribute("xmlns")); - - parseNode(modelNode, model); - - if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){ - return null; - } - - return model; - } - - private void parseNode(Node node, ModelArtifact model) { - if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) { - model.setModelInvariantId(node.getTextContent().trim()); - } - else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) { - model.setModelVerId(node.getTextContent().trim()); - } - else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) { - String dependentModelKey = parseRelationshipNode(node, model); - if (dependentModelKey != null) { - model.addDependentModelId(dependentModelKey); - } - } - else { - if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) { - model.setModelVer(node); - if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) { - Element e = (Element) node; - e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace()); - } - } - - NodeList nodeList = node.getChildNodes(); - - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - parseNode(childNode, model); - } - } - } - - private String parseRelationshipNode(Node node, ModelArtifact model) { - String currentKey = null; - String currentValue = null; - String modelVersionIdValue = null; - String modelInvariantIdValue = null; - - NodeList nodeList = node.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - - if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { - NodeList relDataChildList = childNode.getChildNodes(); - - for (int j = 0; j < relDataChildList.getLength(); j++) { - Node relDataChildNode = relDataChildList.item(j); - - if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { - currentKey = relDataChildNode.getTextContent().trim(); - - if (currentValue != null) { - if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) { - modelVersionIdValue = currentValue; - } - else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) { - modelInvariantIdValue = currentValue; - } - - currentKey = null; - currentValue = null; - } - } - else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { - currentValue = relDataChildNode.getTextContent().trim(); - - if (currentKey != null) { - if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) { - modelVersionIdValue = currentValue; - } - else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) { - modelInvariantIdValue = currentValue; - } - - currentKey = null; - currentValue = null; - } - } - } - } - } - - if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) { - return modelInvariantIdValue + "|" + modelVersionIdValue; - } - - return null; - - } +public class ModelArtifactParser extends AbstractModelArtifactParser { + + public static final String MODEL_VER = "model-ver"; + public static final String MODEL_VERSION_ID = "model-version-id"; + public static final String MODEL_INVARIANT_ID = "model-invariant-id"; + private static final String RELATIONSHIP = "relationship"; + private static final String MODEL_ELEMENT_RELATIONSHIP_KEY = "model." + MODEL_INVARIANT_ID; + private static final String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = MODEL_VER + "." + MODEL_VERSION_ID; + + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName()); + + @Override + void parseNode(Node node, IModelArtifact model) { + if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID) + || node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) { + setVersionId(model, node); + } else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) { + parseRelationshipNode(node, model); + } else { + if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) { + ((ModelArtifact) model).setModelVer(node); + if (((ModelArtifact) model).getModelNamespace() != null + && !((ModelArtifact) model).getModelNamespace().isEmpty()) { + Element e = (Element) node; + e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", + ((ModelArtifact) model).getModelNamespace()); + } + } + + parseChildNodes(node, model); + } + } + + /** + * {@inheritDoc} + */ + @Override + void setVersionId(IModelArtifact model, Node node) { + if (MODEL_INVARIANT_ID.equals(node.getNodeName())) { + ((ModelArtifact) model).setModelInvariantId(node.getTextContent().trim()); + } else if (MODEL_VERSION_ID.equals(node.getNodeName())) { + ((ModelArtifact) model).setModelVerId(node.getTextContent().trim()); + } + } + + /** + * {@inheritDoc} + */ + @Override + ModelId buildModelId(NodeList nodeList) { + return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item) + .filter(childNode -> childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) + .map(this::getRelationship) // + .collect(Collector.of(ModelId::new, ModelId::setRelationship, (m, p) -> m)); + } + + /** + * Find a relationship key and value pair from the children of the supplied node. + * + * @param node containing children storing relationship keys and values + * @return a pair containing a relationship key and its value. Note: if multiple relationships are found, existing + * values stored in the pair will be overwritten. + */ + private Pair getRelationship(Node node) { + Objects.requireNonNull(node); + NodeList relDataChildList = node.getChildNodes(); + Objects.requireNonNull(relDataChildList); + + return IntStream.range(0, relDataChildList.getLength()).mapToObj(relDataChildList::item) + .filter(this::filterRelationshipNode) + .collect(Collector.of(Pair::new, applyRelationshipValue, (p, n) -> p)); + } + + /** + * This method is responsible for creating an instance of {@link ModelArtifactParser.ModelId} + * + * @return IModelId instance of {@link ModelArtifactParser.ModelId} + */ + @Override + IModelId createModelIdInstance() { + return new ModelId(); + } + + private class ModelId implements IModelId { + + private String modelInvariantIdValue; + private String modelVersionIdValue; + + @Override + public void setRelationship(Pair p) { + if (p.getKey().equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) { + modelVersionIdValue = p.getValue(); + } else if (p.getKey().equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) { + modelInvariantIdValue = p.getValue(); + } + } + + @Override + public boolean defined() { + return modelInvariantIdValue != null && modelVersionIdValue != null; + } + + @Override + public String toString() { + return modelInvariantIdValue + "|" + modelVersionIdValue; + } + } + + /** + * {@inheritDoc} + */ + @Override + String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) { + return "Unable to parse legacy model artifact " + artifactName + ": " + localisedMessage; + } + + /** + * {@inheritDoc} + */ + @Override + IModelArtifact createModelArtifactInstance() { + return new ModelArtifact(); + } + + @Override + boolean modelIsValid(IModelArtifact model) { + return ((ModelArtifact) model).getModelInvariantId() != null && ((ModelArtifact) model).getModelVerId() != null; + } + + /** + * {@inheritDoc} + */ + @Override + boolean processParsedModel(List modelList, String artifactName, IModelArtifact model) { + boolean valid = false; + + if (model != null) { + ModelArtifact modelImpl = (ModelArtifact) model; + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-invariant-Id: " + + modelImpl.getModelInvariantId() + " Model-Version-Id: " + modelImpl.getModelVerId()); + modelList.add(modelImpl); + valid = true; + } else { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName); + } + + return valid; + } + + /** + * {@inheritDoc} + */ + @Override + String getModelElementRelationshipKey() { + return null; + } + + /** + * {@inheritDoc} + */ + @Override + String getVersionIdNodeName() { + return null; + } }