17bdd87240c938a87fb9d52948dfb188b42c7971
[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 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
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
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;
33
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37
38 public class ModelArtifactParser extends AbstractModelArtifactParser {
39
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;
46
47     private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName());
48
49     @Override
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);
56         } else {
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());
64                 }
65             }
66
67             parseChildNodes(node, model);
68         }
69     }
70
71     /**
72      * {@inheritDoc}
73      */
74     @Override
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());
80         }
81     }
82
83     /**
84      * {@inheritDoc}
85      */
86     @Override
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));
92     }
93
94     /**
95      * Find a relationship key and value pair from the children of the supplied node.
96      *
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.
100      */
101     private Pair<String, String> getRelationship(Node node) {
102         Objects.requireNonNull(node);
103         NodeList relDataChildList = node.getChildNodes();
104         Objects.requireNonNull(relDataChildList);
105
106         return IntStream.range(0, relDataChildList.getLength()).mapToObj(relDataChildList::item)
107                 .filter(this::filterRelationshipNode)
108                 .collect(Collector.of(Pair::new, applyRelationshipValue, (p, n) -> p));
109     }
110
111     /**
112      * This method is responsible for creating an instance of {@link ModelArtifactParser.ModelId}
113      *
114      * @return IModelId instance of {@link ModelArtifactParser.ModelId}
115      */
116     @Override
117     IModelId createModelIdInstance() {
118         return new ModelId();
119     }
120
121     private class ModelId implements IModelId {
122
123         private String modelInvariantIdValue;
124         private String modelVersionIdValue;
125
126         @Override
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();
132             }
133         }
134
135         @Override
136         public boolean defined() {
137             return modelInvariantIdValue != null && modelVersionIdValue != null;
138         }
139
140         @Override
141         public String toString() {
142             return modelInvariantIdValue + "|" + modelVersionIdValue;
143         }
144     }
145
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) {
151         return "Unable to parse legacy model artifact " + artifactName + ": " + localisedMessage;
152     }
153
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     IModelArtifact createModelArtifactInstance() {
159         return new ModelArtifact();
160     }
161
162     @Override
163     boolean modelIsValid(IModelArtifact model) {
164         return ((ModelArtifact) model).getModelInvariantId() != null && ((ModelArtifact) model).getModelVerId() != null;
165     }
166
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     boolean processParsedModel(List<Artifact> modelList, String artifactName, IModelArtifact model) {
172         boolean valid = false;
173
174         if (model != null) {
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);
179             valid = true;
180         } else {
181             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
182         }
183
184         return valid;
185     }
186
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     String getModelElementRelationshipKey() {
192         return null;
193     }
194
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     String getVersionIdNodeName() {
200         return null;
201     }
202 }