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