Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / uri / URIToObject.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.parsers.uri;
21
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.introspection.Introspector;
24 import org.onap.aai.introspection.Loader;
25 import org.onap.aai.schema.enums.ObjectMetadata;
26 import org.onap.aai.edges.enums.EdgeType;
27 import org.onap.aai.setup.SchemaVersion;
28
29 import javax.ws.rs.core.MultivaluedMap;
30 import java.io.UnsupportedEncodingException;
31 import java.net.URI;
32 import java.util.HashMap;
33 import java.util.List;
34
35 /**
36  * Given a URI this class returns an object, or series of nested objects
37  * with their keys populated based off the values in the URI.
38  * 
39  * It populates the keys in the order they are listed in the model.
40  */
41 public class URIToObject implements Parsable {
42
43         
44         private Introspector topEntity = null;
45         
46         private String topEntityName = null;
47         
48         private String entityName = null;
49         
50         private Introspector entity = null;
51         
52         private Introspector previous = null;
53         
54         private List<Object> parentList = null;
55         
56         private SchemaVersion version = null;
57         private Loader loader = null;
58         private final HashMap<String, Introspector> relatedObjects;
59         
60         /**
61          * Instantiates a new URI to object.
62          *
63          * @param loader the loader
64          * @param uri the uri
65          * @throws IllegalArgumentException the illegal argument exception
66          * @throws AAIException the AAI exception
67          * @throws UnsupportedEncodingException the unsupported encoding exception
68          */
69         public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
70                 
71                 URIParser parser = new URIParser(loader, uri);
72                 this.relatedObjects = new HashMap<>();
73                 
74                 parser.parse(this);
75                 this.loader = parser.getLoader();
76                 this.version = loader.getVersion();
77         }
78         public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
79                 
80                 URIParser parser = new URIParser(loader, uri);
81                 this.relatedObjects = relatedObjects;
82
83                 parser.parse(this);
84                 this.loader = parser.getLoader();
85                 this.version = loader.getVersion();
86
87         }
88         
89         /**
90          * @{inheritDoc}
91          */
92         @Override
93         public void processNamespace(Introspector obj) {
94         
95         }
96         
97         /**
98          * @{inheritDoc}
99          */
100         @Override
101         public String getCloudRegionTransform() {
102                 return "add";
103         }
104         
105         /**
106          * @{inheritDoc}
107          */
108         @Override
109         public boolean useOriginalLoader() {
110                 // TODO Auto-generated method stub
111                 return false;
112         }
113
114         /**
115          * Gets the top entity.
116          *
117          * @return the top entity
118          */
119         public Introspector getTopEntity() {
120                 return this.topEntity;
121         }
122         
123         /**
124          * Gets the entity.
125          *
126          * @return the entity
127          */
128         public Introspector getEntity() {
129                 return this.entity;
130         }
131         
132         /**
133          * Gets the parent list.
134          *
135          * @return the parent list
136          */
137         public List<Object> getParentList() {
138                 return this.parentList;
139         }
140         
141         /**
142          * Gets the entity name.
143          *
144          * @return the entity name
145          */
146         public String getEntityName() {
147                 return this.entityName;
148         }
149         
150         /**
151          * Gets the top entity name.
152          *
153          * @return the top entity name
154          */
155         public String getTopEntityName() {
156                 return this.topEntityName;
157         }
158         
159         /**
160          * Gets the object version.
161          *
162          * @return the object version
163          */
164         public SchemaVersion getObjectVersion() {
165                 return this.loader.getVersion();
166         }
167         public Loader getLoader() {
168                 return this.loader;
169         }
170         @Override
171         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
172                         throws AAIException {
173
174                 if (this.entityName == null) {
175                         this.topEntityName = obj.getDbName();
176                         this.topEntity = obj;
177                 }
178                 this.entityName = obj.getDbName();
179                 this.entity = obj;
180                 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
181                 this.parentList.add(entity.getUnderlyingObject());
182                 
183                 for (String key : uriKeys.keySet()) {
184                         entity.setValue(key, uriKeys.getFirst(key));
185                 }
186                 try {
187                         if (relatedObjects.containsKey(entity.getObjectId())) {
188                                 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
189                                 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
190                                 if (nameProp == null) {
191                                         nameProp = "";
192                                 }
193                                 if (nameProp != null && !nameProp.equals("")) {
194                                         String[] nameProps = nameProp.split(",");
195                                         for (String prop : nameProps) {
196                                                 entity.setValue(prop, relatedObject.getValue(prop));
197                                         }
198                                 }
199                         }
200                 } catch (UnsupportedEncodingException e) {
201                 }
202                 this.previous = entity;
203         }
204         @Override
205         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
206                         boolean isFinalContainer) throws AAIException {
207                 this.previous = obj;
208
209                 if (this.entity != null) {
210                         this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
211                 } else {
212                         this.entity = obj;
213                         this.topEntity = obj;
214                 }
215         }
216 }