aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeSerializer.java
blob: 0c58717e06c9e157c5e300dd85668412e19bec83 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.serialization.db;

import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.edges.EdgeRule;
import org.onap.aai.edges.EdgeRuleQuery;
import org.onap.aai.edges.enums.EdgeField;
import org.onap.aai.edges.enums.EdgeProperty;
import org.onap.aai.edges.enums.EdgeType;
import org.onap.aai.edges.enums.MultiplicityRule;
import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException;
import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EdgeSerializer {

    @Autowired
    private EdgeIngestor edgerules;

    public EdgeSerializer(EdgeIngestor ei) {
        this.edgerules = ei;
    }

    /**
     * Adds the tree edge.
     *
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return the edge
     * @throws AAIException the AAI exception
     */
    public Edge addTreeEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
        return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, false, null);
    }

    /**
     * Adds the edge.
     *
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return the edge
     * @throws AAIException the AAI exception
     */
    public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
        return this.addEdge(traversalSource, aVertex, bVertex, null);
    }

    public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label)
            throws AAIException {
        return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label);
    }

    public Edge addPrivateEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label)
            throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
        return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label, true);
    }

    private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex,
            boolean isBestEffort, String label, boolean isPrivateEdge)
            throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {

        EdgeRule rule = null;

        String aType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
        String bType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
        EdgeRuleQuery edgeQuery =
                new EdgeRuleQuery.Builder(aType, bType).label(label).setPrivate(isPrivateEdge).build();

        rule = edgerules.getRule(edgeQuery);

        if (rule.isPrivateEdge() != isPrivateEdge) {
            return null;
        }

        Edge e = null;

        Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex);

        if (message.isPresent() && !isBestEffort) {
            throw new EdgeMultiplicityException(message.get());
        }
        if (!message.isPresent()) {
            if (rule.getDirection().equals(Direction.OUT)) {
                e = aVertex.addEdge(rule.getLabel(), bVertex);
            } else if (rule.getDirection().equals(Direction.IN)) {
                e = bVertex.addEdge(rule.getLabel(), aVertex);
            }

            this.addProperties(e, rule);
        }
        return e;
    }

    /**
     * Adds the tree edge.
     *
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return the edge
     * @throws AAIException the AAI exception
     */
    public Edge addTreeEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex)
            throws AAIException {
        return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, true, null);
    }

    /**
     * Adds the edge.
     *
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return the edge
     * @throws AAIException the AAI exception
     */
    public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex)
            throws AAIException {
        return this.addEdgeIfPossible(traversalSource, aVertex, bVertex, null);
    }

    public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label)
            throws AAIException {
        return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, true, label);
    }

    /**
     * Adds the edge.
     *
     * @param type the type
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return the edge
     * @throws AAIException the AAI exception
     */
    private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex,
            boolean isBestEffort, String label) throws AAIException {
        String aNodeType = (String) aVertex.property(AAIProperties.NODE_TYPE).value();
        String bNodeType = (String) bVertex.property(AAIProperties.NODE_TYPE).value();
        EdgeRuleQuery q = new EdgeRuleQuery.Builder(aNodeType, bNodeType).label(label).edgeType(type).build();
        EdgeRule rule;
        try {
            rule = edgerules.getRule(q);
        } catch (EdgeRuleNotFoundException e1) {
            throw new NoEdgeRuleFoundException(e1);
        } catch (AmbiguousRuleChoiceException e1) {
            throw new MultipleEdgeRuleFoundException(e1);
        }

        Edge e = null;

        Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex);

        if (message.isPresent() && !isBestEffort) {
            throw new EdgeMultiplicityException(message.get());
        }
        if (!message.isPresent()) {
            if (rule.getDirection().equals(Direction.OUT)) {
                e = aVertex.addEdge(rule.getLabel(), bVertex);
            } else if (rule.getDirection().equals(Direction.IN)) {
                e = bVertex.addEdge(rule.getLabel(), aVertex);
            }

            this.addProperties(e, rule);
        }
        return e;
    }

    /**
     * Adds the properties.
     *
     * @param edge the edge
     * @param rule the rule
     */
    public void addProperties(Edge edge, EdgeRule rule) {
        edge.property(EdgeProperty.CONTAINS.toString(), rule.getContains());
        edge.property(EdgeProperty.DELETE_OTHER_V.toString(), rule.getDeleteOtherV());
        edge.property(EdgeProperty.PREVENT_DELETE.toString(), rule.getPreventDelete());
        edge.property(EdgeField.PRIVATE.toString(), rule.isPrivateEdge());
        edge.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
    }

    /**
     * Validate multiplicity.
     *
     * @param rule the rule
     * @param aVertex the out vertex
     * @param bVertex the in vertex
     * @return true, if successful
     * @throws AAIException the AAI exception
     */
    private Optional<String> validateMultiplicity(EdgeRule rule, GraphTraversalSource traversalSource, Vertex aVertex,
            Vertex bVertex) {

        Vertex a = aVertex;
        Vertex b = bVertex;

        if (rule.getDirection().equals(Direction.OUT)) {
            a = aVertex;
            b = bVertex;
        } else if (rule.getDirection().equals(Direction.IN)) {
            a = bVertex;
            b = aVertex;
        }

        String aVertexType = a.<String>property(AAIProperties.NODE_TYPE).orElse(null);
        String bVertexType = b.<String>property(AAIProperties.NODE_TYPE).orElse(null);
        String label = rule.getLabel();

        MultiplicityRule multiplicityRule = rule.getMultiplicityRule();

        String detail = "";
        final String msg = "multiplicity rule violated: only one edge can exist with label: ";

        if (multiplicityRule.equals(MultiplicityRule.ONE2ONE)) {
            Long outEdgesCnt = traversalSource.V(a).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next();
            Long inEdgesCnt = traversalSource.V(b).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next();
            if (aVertexType.equals(bVertexType)) {
                inEdgesCnt = inEdgesCnt
                        + traversalSource.V(a).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next();
                outEdgesCnt = outEdgesCnt
                        + traversalSource.V(b).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next();
            }
            if ((inEdgesCnt != 0) || (outEdgesCnt != 0)) {
                detail = msg + label + " between " + aVertexType + " and " + bVertexType;
            }
        } else if (multiplicityRule.equals(MultiplicityRule.ONE2MANY)) {
            Long inEdgesCnt = traversalSource.V(b).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next();
            if (inEdgesCnt != 0) {
                detail = msg + label + " between " + aVertexType + " and " + bVertexType;
            }
        } else if (multiplicityRule.equals(MultiplicityRule.MANY2ONE)) {
            Long outEdgesCnt = traversalSource.V(a).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next();
            if (outEdgesCnt != 0) {
                detail = msg + label + " between " + aVertexType + " and " + bVertexType;
            }
        }

        if (!"".equals(detail)) {
            return Optional.of(detail);
        } else {
            return Optional.empty();
        }
    }
}