Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / transform / TinkerpopChampformer.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.openecomp.aai.champ.transform;
23
24 import java.util.Iterator;
25
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.apache.tinkerpop.gremlin.structure.Property;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
30 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
31 import org.openecomp.aai.champ.model.ChampObject;
32 import org.openecomp.aai.champ.model.ChampRelationship;
33 import org.openecomp.aai.champ.model.fluent.object.ObjectBuildOrPropertiesStep;
34 import org.openecomp.aai.champ.model.fluent.relationship.RelationshipBuildOrPropertiesStep;
35
36 public final class TinkerpopChampformer implements Champformer<Vertex, Edge> {
37
38         @Override
39         public Vertex marshallObject(ChampObject object) throws ChampUnmarshallingException {
40                 throw new UnsupportedOperationException("Cannot marshall object to Tinkerpop Vertex without adding it to a graph");
41         }
42
43         @Override
44         public Edge marshallRelationship(ChampRelationship relationship) throws ChampUnmarshallingException {
45                 throw new UnsupportedOperationException("Cannot marshall relationships to Tinkerpop Edge without adding it to a graph");
46         }
47
48         @Override
49         public ChampObject unmarshallObject(Vertex vertex) throws ChampUnmarshallingException {
50                 final String type = vertex.label();
51                 final ObjectBuildOrPropertiesStep aaiObjBuilder = ChampObject.create()
52                                                                                                                         .ofType(type)
53                                                                                                                         .withKey(vertex.id());
54                 final Iterator<VertexProperty<Object>> properties = vertex.properties();
55
56                 while (properties.hasNext()) {
57                         final VertexProperty<Object> property = properties.next();
58
59                         if (ChampObject.ReservedPropertyKeys.contains(property.key()) ||
60                                 ChampObject.IgnoreOnReadPropertyKeys.contains(property.key())) continue;
61                         
62                         aaiObjBuilder.withProperty(property.key(), property.value());
63                 }
64
65                 return aaiObjBuilder.build();
66         }
67
68         @Override
69         public ChampRelationship unmarshallRelationship(Edge edge) throws ChampUnmarshallingException {
70                 final ChampObject source = unmarshallObject(edge.outVertex());
71                 final ChampObject target = unmarshallObject(edge.inVertex());
72                 final String type = edge.label();
73                 final RelationshipBuildOrPropertiesStep aaiRelBuilder = ChampRelationship.create()
74                                                                                                                                                         .ofType(type)
75                                                                                                                                                         .withKey(edge.id())
76                                                                                                                                                         .withSource()
77                                                                                                                                                                 .from(source)
78                                                                                                                                                                 .build()
79                                                                                                                                                         .withTarget()
80                                                                                                                                                                 .from(target)
81                                                                                                                                                                 .build();
82                 final Iterator<Property<Object>> properties = edge.properties();
83                 
84                 while (properties.hasNext()) {
85                         final Property<Object> property = properties.next();
86                         
87                         if (ChampRelationship.ReservedPropertyKeys.contains(property.key())) continue;
88
89                         aaiRelBuilder.withProperty(property.key(), property.value());
90                 }
91                 
92                 return aaiRelBuilder.build();
93         }
94 }