aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/spike/schema/GraphEventTransformer.java
blob: ced84bb2d6381ae49dfaa0c98386b5e6e2f08289 (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
/**
 * ============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.spike.schema;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.google.common.base.CaseFormat;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.eclipse.persistence.dynamic.DynamicType;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.XMLField;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.spike.event.incoming.GizmoEdge;
import org.onap.aai.spike.event.incoming.GizmoGraphEvent;
import org.onap.aai.spike.event.incoming.GizmoVertex;
import org.onap.aai.spike.exception.SpikeException;

/**
 * This class is responsible for transforming raw graph entities (such as vertices and edges) into
 * representations which correspond to the OXM models.
 */
public class GraphEventTransformer {

    private static org.onap.aai.cl.api.Logger logger =
            LoggerFactory.getInstance().getLogger(GraphEventTransformer.class.getName());
    private static final String AAI_UUID = "aai-uuid";

    /**
     * 
     * @param rawVertex
     * @throws SpikeException
     */
    public static void validateVertexModel(GizmoVertex rawVertex) throws SpikeException {

        validateVertexModel(OXMModelLoader.getLatestVersion(), rawVertex);
    }

    public static void populateUUID(GizmoGraphEvent event) throws SpikeException {
        try {
            if (event.getVertex() != null) {
                if (event.getVertex().getProperties().getAsJsonObject().has(AAI_UUID)) {
                    event.getVertex()
                            .setId(event.getVertex().getProperties().getAsJsonObject().get(AAI_UUID).getAsString());
                }
            } else if (event.getRelationship() != null) {
                if (event.getRelationship().getProperties().getAsJsonObject().has(AAI_UUID)) {
                    event.getRelationship().setId(
                            event.getRelationship().getProperties().getAsJsonObject().get(AAI_UUID).getAsString());
                }

                if (event.getRelationship().getSource().getProperties().getAsJsonObject().has(AAI_UUID)) {
                    event.getRelationship().getSource().setId(event.getRelationship().getSource().getProperties()
                            .getAsJsonObject().get(AAI_UUID).getAsString());
                }
                if (event.getRelationship().getTarget().getProperties().getAsJsonObject().has(AAI_UUID)) {
                    event.getRelationship().getTarget().setId(event.getRelationship().getTarget().getProperties()
                            .getAsJsonObject().get(AAI_UUID).getAsString());
                }
            }
        } catch (Exception ex) {
            throw new SpikeException("Unable to parse uuid in incoming event");
        }
    }

    /**
     * 
     * @param version
     * @param rawVertex
     * @throws SpikeException
     */
    public static void validateVertexModel(String version, GizmoVertex rawVertex) throws SpikeException {

        try {

            DynamicJAXBContext jaxbContext = OXMModelLoader.getContextForVersion(version);
            String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
                    CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, rawVertex.getType()));
            final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
            final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");

            Set<Map.Entry<String, JsonElement>> vertexEntriesSet =
                    rawVertex.getProperties().getAsJsonObject().entrySet();
            Map<String, JsonElement> vertexEntriesMap = new HashMap<String, JsonElement>();
            for (Map.Entry<String, JsonElement> entry : vertexEntriesSet) {
                vertexEntriesMap.put(entry.getKey(), entry.getValue());
            }

            JsonObject modelJsonElement = new JsonObject();
            // Iterate over all of the attributes specified in the model schema,
            // populating
            // our dynamic instance with the corresponding values supplied in
            // our raw vertex.
            for (DatabaseMapping mapping : modelObjectType.getDescriptor().getMappings()) {
                if (mapping.isAbstractDirectMapping()) {
                    DatabaseField f = mapping.getField();
                    String keyName = f.getName().substring(0, f.getName().indexOf("/"));

                    String defaultValue = mapping.getProperties().get("defaultValue") == null ? ""
                            : mapping.getProperties().get("defaultValue").toString();

                    if (((XMLField) f).isRequired() && !vertexEntriesMap.containsKey(keyName)
                            && !defaultValue.isEmpty()) {
                        modelJsonElement.addProperty(keyName, defaultValue);

                    }
                    // If this is a required field, but is not present in the
                    // raw vertex, reject this
                    // as an invalid input since we can't build a valid object
                    // from what we were provided.
                    if (((XMLField) f).isRequired() && !vertexEntriesMap.containsKey(keyName)
                            && defaultValue.isEmpty()) {
                        throw new SpikeException("Missing required field: " + keyName);
                    }

                    // If this is a non-required field, then set it if a value
                    // was provided in the
                    // raw vertex.
                    if (vertexEntriesMap.containsKey(keyName)) {
                        validateFieldType(vertexEntriesMap.get(keyName), f.getType());
                        modelJsonElement.add(keyName, vertexEntriesMap.get(keyName));
                    }
                }
            }

            // Ensure any of the reserved properties are added to the payload
            for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
                if (mapping.isAbstractDirectMapping()) {
                    DatabaseField field = mapping.getField();
                    String keyName = field.getName().substring(0, field.getName().indexOf("/"));

                    if (vertexEntriesMap.containsKey(keyName)) {
                        validateFieldType(vertexEntriesMap.get(keyName), field.getType());
                        modelJsonElement.add(keyName, vertexEntriesMap.get(keyName));
                    }
                }
            }

            rawVertex.setProperties(modelJsonElement);
        } catch (Exception e) {
            throw new SpikeException(e.getMessage());
        }
    }

    /**
     * 
     * @param rawEdge
     * @throws SpikeException
     */
    public static void validateEdgeModel(GizmoEdge rawEdge) throws SpikeException {

        validateEdgeModel(EdgeRulesLoader.getLatestSchemaVersion(), rawEdge);
    }

    /**
     * 
     * @param version
     * @param rawEdge
     * @throws SpikeException
     */
    public static void validateEdgeModel(String version, GizmoEdge rawEdge) throws SpikeException {

        if (logger.isDebugEnabled()) {
            logger.debug("Convert edge: " + rawEdge.toString() + " to model version: " + version);
        }

        // Get the relationship schema for the supplied version.
        RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);

        try {

            // Validate that our edge does have the necessary endpoints.
            if (rawEdge.getSource() == null || rawEdge.getTarget() == null) {
                throw new SpikeException("Source or target endpoint not specified");
            }

            // Create a key based on source:target:relationshipType
            String sourceNodeType = rawEdge.getSource().getType();
            String targetNodeType = rawEdge.getTarget().getType();
            String key = sourceNodeType + ":" + targetNodeType + ":" + rawEdge.getType();

            // Now, look up the specific schema model based on the key we just
            // constructed.
            Map<String, Class<?>> relationshipModel = schema.lookupRelation(key);
            if (relationshipModel == null || relationshipModel.isEmpty()) {
                throw new SpikeException("Invalid source/target/relationship type: " + key);
            }

            Set<Map.Entry<String, JsonElement>> edgeEntriesSet = rawEdge.getProperties().getAsJsonObject().entrySet();
            Map<String, JsonElement> edgeEntriesMap = new HashMap<String, JsonElement>();
            for (Map.Entry<String, JsonElement> entry : edgeEntriesSet) {
                edgeEntriesMap.put(entry.getKey(), entry.getValue());
            }

            JsonObject modelJsonElement = new JsonObject();

            for (String property : relationshipModel.keySet()) {

                if (!edgeEntriesMap.containsKey(property)) {
                    throw new SpikeException("Missing required field: " + property);
                }

                validateFieldType(edgeEntriesMap.get(property), relationshipModel.get(property));
                modelJsonElement.add(property, edgeEntriesMap.get(property));

            }

            rawEdge.setProperties(modelJsonElement);


        } catch (Exception ex) {
            throw new SpikeException(ex.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    public static Object validateFieldType(JsonElement value, Class clazz) throws SpikeException {
        try {
            if (clazz.isAssignableFrom(Integer.class)) {
                return value.getAsInt();
            } else if (clazz.isAssignableFrom(Long.class)) {
                return value.getAsLong();
            } else if (clazz.isAssignableFrom(Float.class)) {
                return value.getAsFloat();
            } else if (clazz.isAssignableFrom(Double.class)) {
                return value.getAsDouble();
            } else if (clazz.isAssignableFrom(Boolean.class)) {
                return value.getAsBoolean();
            } else {
                return value;
            }

        } catch (Exception e) {
            throw new SpikeException("Invalid property value: " + value);
        }
    }

    public static Object validateFieldType(String value, Class clazz) throws SpikeException {
        try {
            if (clazz.isAssignableFrom(Integer.class)) {
                return Integer.parseInt(value);
            } else if (clazz.isAssignableFrom(Long.class)) {
                return Long.parseLong(value);
            } else if (clazz.isAssignableFrom(Float.class)) {
                return Float.parseFloat(value);
            } else if (clazz.isAssignableFrom(Double.class)) {
                return Double.parseDouble(value);
            } else if (clazz.isAssignableFrom(Boolean.class)) {
                if (!value.equals("true") && !value.equals("false")) {
                    throw new SpikeException("Invalid property value: " + value);
                }
                return Boolean.parseBoolean(value);
            } else {
                return value;
            }
        } catch (Exception e) {
            throw new SpikeException("Invalid property value: " + value);
        }
    }

}