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