Improve the performance of resoures microservice
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / JSONStrategy.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.introspection;
21
22 import org.json.simple.JSONObject;
23 import org.onap.aai.schema.enums.ObjectMetadata;
24 import org.onap.aai.schema.enums.PropertyMetadata;
25 import org.onap.aai.setup.SchemaVersion;
26
27 import java.io.UnsupportedEncodingException;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.UUID;
32
33 public class JSONStrategy extends Introspector {
34
35         private JSONObject json = null;
36         private String namedType = "";
37         protected JSONStrategy(Object o) {
38                 super(o);
39                 json = (JSONObject)o;
40                 //Assumes you provide a wrapper
41                 Set<String> keySet = json.keySet();
42                 if (keySet.size() == 1) {
43                         namedType = keySet.iterator().next();
44                         json = (JSONObject)json.get(namedType);
45                 } else {
46                         throw new IllegalArgumentException("This object has no named type.");
47                 }
48         }
49
50         protected JSONStrategy(Object o, String namedType) {
51                 super(o);
52                 json = (JSONObject)o;
53                 this.namedType = namedType;
54
55         }
56
57         @Override
58         public boolean hasProperty(String name) {
59                 //TODO
60                 return true;
61         }
62         @Override
63         public Object getValue(String name) {
64                 Object result = "";
65                 result = json.get(name);
66
67                 return result;
68         }
69
70         @Override
71         public void setValue(String name, Object obj) {
72                 json.put(name, obj);
73
74         }
75         @Override
76         public Object getUnderlyingObject() {
77                 return this.json;
78         }
79
80         @Override
81         public Set<String> getProperties() {
82                 Set<String> result = json.keySet();
83                 return result;
84         }
85
86         @Override
87         public Set<String> getRequiredProperties() {
88                 //unknowable
89
90                 return this.getProperties();
91         }
92
93         @Override
94         public Set<String> getKeys() {
95                 //unknowable
96                 return this.getProperties();
97         }
98
99         @Override
100         public Set<String> getAllKeys() {
101                 //unknowable
102                 return this.getProperties();
103         }
104
105         @Override
106         public String getType(String name) {
107                 String result = "";
108                 Class<?> resultClass = this.getClass(name);
109                 if (resultClass != null) {
110                         result = resultClass.getName();
111                 }
112
113                 if (result.equals("org.json.simple.JSONArray")) {
114                         result = "java.util.List";
115                 }
116
117                 return result;
118         }
119
120         @Override
121         public String getGenericType(String name) {
122                 String result = "";
123                 Class<?> resultClass = this.getGenericTypeClass(name);
124                 if (resultClass != null) {
125                         result = resultClass.getName();
126                 }
127                 return result;
128         }
129
130         @Override
131         public String getJavaClassName() {
132                 return json.getClass().getName();
133         }
134
135         @Override
136         public Class<?> getClass(String name) {
137                 Class<?> result = null;
138                 result = json.get(name).getClass();
139
140                 return result;
141         }
142
143         @Override
144         public Class<?> getGenericTypeClass(String name) {
145                 Object resultObject = null;
146                 Class<?> resultClass = null;
147                 resultObject = this.getValue(name);
148                 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
149                         resultClass = ((List)resultObject).get(0).getClass();
150                 }
151
152                 return resultClass;
153         }
154
155         @Override
156         public Object newInstanceOfProperty(String name) {
157                 try {
158                         return this.getClass(name).newInstance();
159                 } catch (InstantiationException | IllegalAccessException e) {
160                         return null;
161                 }
162         }
163
164         @Override
165         public Object newInstanceOfNestedProperty(String name) {
166                 try {
167                         return this.getGenericTypeClass(name).newInstance();
168                 } catch (InstantiationException | IllegalAccessException e) {
169                         return null;
170                 }
171         }
172
173         @Override
174         public boolean isComplexType(String name) {
175                 String result = this.getType(name);
176
177                 if (result.contains("JSONObject")) {
178                         return true;
179                 } else {
180                         return false;
181                 }
182
183         }
184
185         @Override
186         public boolean isComplexGenericType(String name) {
187                 String result = this.getGenericType(name);
188
189                 if (result.contains("JSONObject")) {
190                         return true;
191                 } else {
192                         return false;
193                 }
194
195         }
196
197         @Override
198         public boolean isListType(String name) {
199                 String result = this.getType(name);
200
201                 if (result.contains("java.util.List")) {
202                         return true;
203                 } else {
204                         return false;
205                 }
206
207         }
208
209         @Override
210         public boolean isContainer() {
211                 Set<String> props = this.getProperties();
212                 boolean result = false;
213                 if (props.size() == 1 && this.isListType(props.iterator().next())) {
214                         result = true;
215                 }
216
217                 return result;
218         }
219         @Override
220         protected String findKey() {
221                 return "";
222         }
223
224         @Override
225         public String getName() {
226                 return this.namedType;
227         }
228
229         @Override
230         public String getDbName() {
231                 return this.getName();
232         }
233
234         @Override
235         public String getURI() {
236
237                 // use a UUID for now
238                 return UUID.randomUUID().toString();
239         }
240
241         @Override
242         public String getGenericURI() {
243
244                 //there is none defined for this
245                 return "";
246         }
247
248         @Override
249         public String preProcessKey (String key) {
250
251                 // don't do anything with it
252                 return key;
253
254         }
255
256         @Override
257         public String marshal(MarshallerProperties properties) {
258                 //TODO
259                 return null;
260         }
261
262         @Override
263         public Object clone() {
264                 //TODO
265                 return null;
266         }
267
268         /*@Override
269         public String findEdgeName(String parent, String child) {
270
271                 // Always has for now
272                 return "has";
273
274         }*/
275
276         @Override
277         public ModelType getModelType() {
278                 return ModelType.JSON;
279         }
280
281         @Override
282         public Set<String> getIndexedProperties() {
283                 // TODO Auto-generated method stub
284                 return null;
285         }
286
287         @Override
288         public String getChildName() {
289                 // TODO Auto-generated method stub
290                 return null;
291         }
292
293         @Override
294         public boolean hasChild(Introspector child) {
295                 // TODO Auto-generated method stub
296                 return false;
297         }
298
299         @Override
300         public boolean isSimpleType(String name) {
301                 // TODO Auto-generated method stub
302                 return false;
303         }
304
305         @Override
306         public boolean isSimpleGenericType(String name) {
307                 // TODO Auto-generated method stub
308                 return false;
309         }
310
311         @Override
312         public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
313                 // TODO Auto-generated method stub
314                 return null;
315         }
316
317         @Override
318         public String getMetadata(ObjectMetadata metadataName) {
319                 // TODO Auto-generated method stub
320                 return null;
321         }
322
323         @Override
324         public String getChildDBName() {
325                 // TODO Auto-generated method stub
326                 return null;
327         }
328
329         @Override
330         public String getFullGenericURI() {
331                 // TODO Auto-generated method stub
332                 return null;
333         }
334
335         @Override
336         protected Object get(String name) {
337                 // TODO Auto-generated method stub
338                 return null;
339         }
340
341         @Override
342         protected void set(String name, Object value) {
343                 // TODO Auto-generated method stub
344
345         }
346
347         @Override
348         public String getObjectId() throws UnsupportedEncodingException {
349                 // TODO Auto-generated method stub
350                 return null;
351         }
352
353         @Override
354         public SchemaVersion getVersion() {
355                 // TODO Auto-generated method stub
356                 return null;
357         }
358
359 }