Convert project from AJSC to Spring Boot
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / ModelArtifactParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.modelloader.entity.model;
22
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;
35
36 public class ModelArtifactParser extends AbstractModelArtifactParser {
37
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;
44
45     private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
46
47     @Override
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);
54         } else {
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());
62                 }
63             }
64
65             parseChildNodes(node, model);
66         }
67     }
68
69     /**
70      * {@inheritDoc}
71      */
72     @Override
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());
78         }
79     }
80
81     /**
82      * {@inheritDoc}
83      */
84     @Override
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));
90     }
91
92     /**
93      * Find a relationship key and value pair from the children of the supplied node.
94      *
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.
98      */
99     private Pair<String, String> getRelationship(Node node) {
100         Objects.requireNonNull(node);
101         NodeList relDataChildList = node.getChildNodes();
102         Objects.requireNonNull(relDataChildList);
103
104         return IntStream.range(0, relDataChildList.getLength()).mapToObj(relDataChildList::item)
105                 .filter(this::filterRelationshipNode)
106                 .collect(Collector.of(Pair::new, applyRelationshipValue, (p, n) -> p));
107     }
108
109     /**
110      * This method is responsible for creating an instance of {@link ModelArtifactParser.ModelId}
111      *
112      * @return IModelId instance of {@link ModelArtifactParser.ModelId}
113      */
114     @Override
115     IModelId createModelIdInstance() {
116         return new ModelId();
117     }
118
119     private class ModelId implements IModelId {
120
121         private String modelInvariantIdValue;
122         private String modelVersionIdValue;
123
124         @Override
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();
130             }
131         }
132
133         @Override
134         public boolean defined() {
135             return modelInvariantIdValue != null && modelVersionIdValue != null;
136         }
137
138         @Override
139         public String toString() {
140             return modelInvariantIdValue + "|" + modelVersionIdValue;
141         }
142     }
143
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) {
149         return "Unable to parse legacy model artifact " + artifactName + ": " + localisedMessage;
150     }
151
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     IModelArtifact createModelArtifactInstance() {
157         return new ModelArtifact();
158     }
159
160     @Override
161     boolean modelIsValid(IModelArtifact model) {
162         return ((ModelArtifact) model).getModelInvariantId() != null && ((ModelArtifact) model).getModelVerId() != null;
163     }
164
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     boolean processParsedModel(List<Artifact> modelList, String artifactName, IModelArtifact model) {
170         boolean valid = false;
171
172         if (model != null) {
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);
177             valid = true;
178         } else {
179             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
180         }
181
182         return valid;
183     }
184
185     /**
186      * {@inheritDoc}
187      */
188     @Override
189     String getModelElementRelationshipKey() {
190         return null;
191     }
192
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     String getVersionIdNodeName() {
198         return null;
199     }
200 }