2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.introspection;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.json.simple.JSONObject;
25 import org.onap.aai.schema.enums.ObjectMetadata;
26 import org.onap.aai.schema.enums.PropertyMetadata;
28 import java.io.UnsupportedEncodingException;
29 import java.util.List;
32 import java.util.UUID;
34 public class JSONStrategy extends Introspector {
35 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JSONStrategy.class);
37 private JSONObject json = null;
38 private String namedType = "";
39 protected JSONStrategy(Object o) {
42 //Assumes you provide a wrapper
43 Set<String> keySet = json.keySet();
44 if (keySet.size() == 1) {
45 namedType = keySet.iterator().next();
46 json = (JSONObject)json.get(namedType);
48 throw new IllegalArgumentException("This object has no named type.");
52 protected JSONStrategy(Object o, String namedType) {
55 this.namedType = namedType;
60 public boolean hasProperty(String name) {
65 public Object getValue(String name) {
67 result = json.get(name);
73 public void setValue(String name, Object obj) {
78 public Object getUnderlyingObject() {
83 public Set<String> getProperties() {
84 Set<String> result = json.keySet();
89 public Set<String> getRequiredProperties() {
92 return this.getProperties();
96 public Set<String> getKeys() {
98 return this.getProperties();
102 public Set<String> getAllKeys() {
104 return this.getProperties();
108 public String getType(String name) {
110 Class<?> resultClass = this.getClass(name);
111 if (resultClass != null) {
112 result = resultClass.getName();
115 if (result.equals("org.json.simple.JSONArray")) {
116 result = "java.util.List";
123 public String getGenericType(String name) {
125 Class<?> resultClass = this.getGenericTypeClass(name);
126 if (resultClass != null) {
127 result = resultClass.getName();
133 public String getJavaClassName() {
134 return json.getClass().getName();
138 public Class<?> getClass(String name) {
139 Class<?> result = null;
140 result = json.get(name).getClass();
146 public Class<?> getGenericTypeClass(String name) {
147 Object resultObject = null;
148 Class<?> resultClass = null;
149 resultObject = this.getValue(name);
150 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
151 resultClass = ((List)resultObject).get(0).getClass();
158 public Object newInstanceOfProperty(String name) {
160 return this.getClass(name).newInstance();
161 } catch (InstantiationException | IllegalAccessException e) {
162 LOGGER.error(e.getMessage(),e);
168 public Object newInstanceOfNestedProperty(String name) {
170 return this.getGenericTypeClass(name).newInstance();
171 } catch (InstantiationException | IllegalAccessException e) {
172 LOGGER.error(e.getMessage(),e);
178 public boolean isComplexType(String name) {
179 String result = this.getType(name);
181 if (result.contains("JSONObject")) {
190 public boolean isComplexGenericType(String name) {
191 String result = this.getGenericType(name);
193 if (result.contains("JSONObject")) {
202 public boolean isListType(String name) {
203 String result = this.getType(name);
205 if (result.contains("java.util.List")) {
214 public boolean isContainer() {
215 Set<String> props = this.getProperties();
216 boolean result = false;
217 if (props.size() == 1 && this.isListType(props.iterator().next())) {
224 protected String findKey() {
229 public String getName() {
230 return this.namedType;
234 public String getDbName() {
235 return this.getName();
239 public String getURI() {
241 // use a UUID for now
242 return UUID.randomUUID().toString();
246 public String getGenericURI() {
248 //there is none defined for this
253 public String preProcessKey (String key) {
255 // don't do anything with it
261 public String marshal(MarshallerProperties properties) {
267 public Object clone() {
273 public String findEdgeName(String parent, String child) {
275 // Always has for now
281 public ModelType getModelType() {
282 return ModelType.JSON;
286 public Set<String> getIndexedProperties() {
287 // TODO Auto-generated method stub
292 public String getChildName() {
293 // TODO Auto-generated method stub
298 public boolean hasChild(Introspector child) {
299 // TODO Auto-generated method stub
304 public boolean isSimpleType(String name) {
305 // TODO Auto-generated method stub
310 public boolean isSimpleGenericType(String name) {
311 // TODO Auto-generated method stub
316 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
317 // TODO Auto-generated method stub
322 public String getMetadata(ObjectMetadata metadataName) {
323 // TODO Auto-generated method stub
328 public String getChildDBName() {
329 // TODO Auto-generated method stub
334 public String getFullGenericURI() {
335 // TODO Auto-generated method stub
340 protected Object get(String name) {
341 // TODO Auto-generated method stub
346 protected void set(String name, Object value) {
347 // TODO Auto-generated method stub
352 public String getObjectId() throws UnsupportedEncodingException {
353 // TODO Auto-generated method stub
358 public Version getVersion() {
359 // TODO Auto-generated method stub