summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java
blob: 69e59719a8ebc90f8d11c6acace37262f72c5e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ===================================================================
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
package org.onap.aai.modelloader.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.modelloader.gizmo.GizmoBulkPayload;
import org.onap.aai.modelloader.gizmo.GizmoEdge;
import org.onap.aai.modelloader.gizmo.GizmoEdgeOperation;
import org.onap.aai.modelloader.gizmo.GizmoVertex;
import org.onap.aai.modelloader.gizmo.GizmoVertexOperation;
import org.onap.aai.modelloader.service.ModelLoaderMsgs;
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 org.xml.sax.SAXException;

public class GizmoTranslator {
    
    private enum NodeType {
        VERTEX,
        ATTRIBUTE,
        CONTAINER,
        RELATIONSHIP_LIST,
        RELATIONSHIP,
        RELATED_TO,
        RELATIONSHIP_DATA,
        RELATIONSHIP_KEY,
        RELATIONSHIP_VALUE,
        MODEL_ELEMENT_VERTEX,
        NQ_ELEMENT_VERTEX,
        UNKNOWN
    }
    
    private static Logger logger = LoggerFactory.getInstance().getLogger(GizmoTranslator.class.getName());
    
    public static String translate(String xmlPayload) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xmlPayload));
        Document doc = builder.parse(is);
        
        GizmoBulkPayload gizmoPayload = new GizmoBulkPayload();
        
        processNode(doc.getDocumentElement(), null, null, gizmoPayload);
        
        return gizmoPayload.toJson();
    }

    private static void processNode(Node node, Node parentNode, GizmoVertexOperation parentVertexOp, GizmoBulkPayload gizmoPayload) {
        if (!(node instanceof Element)) {
            return;
        }

        Node newParent = null;
        NodeType nodeType = getNodeType(node);
        
        switch (nodeType) {
        case VERTEX: 
        case MODEL_ELEMENT_VERTEX:
        case NQ_ELEMENT_VERTEX:
            parentVertexOp = createGizmoVertexOp(node, GizmoBulkPayload.ADD_OP);
            gizmoPayload.addVertexOperation(parentVertexOp);
            if (parentNode != null) {
                gizmoPayload.addEdgeOperation(createGizmoEdgeOp(node, parentNode));
            }
            newParent = node;
            break;
        case RELATIONSHIP:
            processRelationship((Element)node, parentVertexOp, gizmoPayload);
            newParent = parentNode;
            break;
        default:
            newParent = parentNode;
            break;
        }
        
        NodeList childNodes = node.getChildNodes();
        for (int ix = 0; ix < childNodes.getLength(); ix++) {
            processNode(childNodes.item(ix), newParent, parentVertexOp, gizmoPayload);
        }
    }

    private static void processRelationship(Element relationshipNode, GizmoVertexOperation sourceNode, GizmoBulkPayload gizmoPayload) {
        NodeList relatedToList = relationshipNode.getElementsByTagName("related-to");
        if (relatedToList.getLength() != 1) {
            logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship");
            return;
        }
        
        GizmoVertex targetVertex = new GizmoVertex();
        targetVertex.setType(relatedToList.item(0).getTextContent().trim());
        
        NodeList relationData = relationshipNode.getElementsByTagName("relationship-data");
        for (int ix = 0; ix < relationData.getLength(); ix++) {
            Element relationNode = (Element)relationData.item(ix);
            NodeList keyList = relationNode.getElementsByTagName("relationship-key");
            NodeList valueList = relationNode.getElementsByTagName("relationship-value");
            
            if ( (keyList.getLength() != 1) || (valueList.getLength() != 1) ) {
                logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship.  Missing key/value.");
                return;
            }
            
            String[] keyBits = keyList.item(0).getTextContent().trim().split("\\.");
            String value = valueList.item(0).getTextContent().trim();
            
            if (keyBits[0].equalsIgnoreCase(targetVertex.getType())) {
                targetVertex.setProperty(keyBits[1], value);
            }
        }
        
        gizmoPayload.addVertexOperation(new GizmoVertexOperation(GizmoBulkPayload.EXISTS_OP, getVertexId(targetVertex), targetVertex));
        
        GizmoEdge edge = new GizmoEdge();
        
        edge.setSource("$" + getVertexId(sourceNode.getVertex()));
        edge.setTarget("$" + getVertexId(targetVertex));
        
        gizmoPayload.addEdgeOperation(new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge));
    }

    private static GizmoEdgeOperation createGizmoEdgeOp(Node node, Node parentNode) {
        GizmoEdge edge = new GizmoEdge();
        
        edge.setSource("$" + getVertexId(createGizmoVertex(node)));
        edge.setTarget("$" + getVertexId(createGizmoVertex(parentNode)));
        
        GizmoEdgeOperation edgeOp = new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge);
        
        return edgeOp;
    }

    private static GizmoVertexOperation createGizmoVertexOp(Node node, String operationType) {
        GizmoVertex vertex = createGizmoVertex(node);  
        GizmoVertexOperation addOp = new GizmoVertexOperation(operationType, getVertexId(vertex), vertex);
        return addOp;
    }

    private static String getVertexId(GizmoVertex vertex) {
        StringBuilder sb = new StringBuilder();
        sb.append(vertex.getType());
        for (Map.Entry<String, String> entry : vertex.getProperties().entrySet()) {
            sb.append("-" + entry.getValue());
        }
        
        return sb.toString();
    }

    private static GizmoVertex createGizmoVertex(Node node) {
        GizmoVertex vertex = new GizmoVertex();
        vertex.setType(node.getNodeName().trim());

        NodeList childNodes = node.getChildNodes();

        for (int ix = 0; ix < childNodes.getLength(); ix++) {
            if (getNodeType(childNodes.item(ix)).equals(NodeType.ATTRIBUTE)) {
                vertex.setProperty(childNodes.item(ix).getNodeName().trim(), childNodes.item(ix).getTextContent().trim());
            }
        }
        
        // Special case for model-element, where we need to generate an id field
        if (getNodeType(node).equals(NodeType.MODEL_ELEMENT_VERTEX)) {
            vertex.setProperty("model-element-uuid", generateModelElementId((Element)node));
        }
        
        // Special case for nq-element, where we need to generate an id field
        if (getNodeType(node).equals(NodeType.NQ_ELEMENT_VERTEX)) {
            vertex.setProperty("named-query-element-uuid", generateModelElementId((Element)node));
        }

        return vertex;
    }
    
    // Generate a unique hash to store as the id for this node
    private static String generateModelElementId(Element node) {
        Set<String> elemSet = new HashSet<String>();
        
        NodeList childNodes = node.getElementsByTagName("*");
        for (int ix = 0; ix < childNodes.getLength(); ix++) {
            NodeType nt = getNodeType(childNodes.item(ix));
            if ( nt.equals(NodeType.ATTRIBUTE) || nt.equals(NodeType.RELATIONSHIP_KEY) || nt.equals(NodeType.RELATIONSHIP_VALUE) ) {
                elemSet.add(childNodes.item(ix).getTextContent().trim());
            }
        }
                
        return Integer.toString(elemSet.hashCode());
    }

    private static NodeType getNodeType(Node node) {
        if (!(node instanceof Element)) {
            return NodeType.UNKNOWN;
        }
        
        if (node.getNodeName().equalsIgnoreCase("relationship-list")) {
            return NodeType.RELATIONSHIP_LIST;
        }
        
        if (node.getNodeName().equalsIgnoreCase("relationship")) {
            return NodeType.RELATIONSHIP;
        }
        
        if (node.getNodeName().equalsIgnoreCase("relationship-data")) {
            return NodeType.RELATIONSHIP_DATA;
        }
        
        if (node.getNodeName().equalsIgnoreCase("related-to")) {
            return NodeType.RELATED_TO;
        }
        
        if (node.getNodeName().equalsIgnoreCase("relationship-key")) {
            return NodeType.RELATIONSHIP_KEY;
        }
        
        if (node.getNodeName().equalsIgnoreCase("relationship-value")) {
            return NodeType.RELATIONSHIP_VALUE;
        }
        
        if (node.getNodeName().equalsIgnoreCase("model-element")) {
            return NodeType.MODEL_ELEMENT_VERTEX;
        }
        
        if (node.getNodeName().equalsIgnoreCase("named-query-element")) {
            return NodeType.NQ_ELEMENT_VERTEX;
        }
        
        NodeList childNodes = node.getChildNodes();
        int childElements = countChildElements(childNodes);
        
        if ( (childElements == 0) && (node.getTextContent() != null) && (!node.getTextContent().trim().isEmpty()) ) {
            return NodeType.ATTRIBUTE;
        }
        
        for (int ix = 0; ix < childNodes.getLength(); ix++) {
            if (getNodeType(childNodes.item(ix)) == NodeType.ATTRIBUTE) {
                return NodeType.VERTEX;
            }
        }
        
        if (childElements > 0) {
            return NodeType.CONTAINER;
        }
        
        return NodeType.UNKNOWN;
    }
    
    static int countChildElements(NodeList nodes) {
        int count = 0;
        for (int ix = 0; ix < nodes.getLength(); ix++) {
            if (nodes.item(ix) instanceof Element) {
                count++;
            }
        }
        
        return count;   
    }
}