2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.introspection;
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;
30 import java.io.UnsupportedEncodingException;
31 import java.util.List;
34 import java.util.UUID;
36 public class JSONStrategy extends Introspector {
37 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JSONStrategy.class);
39 private JSONObject json = null;
40 private String namedType = "";
41 protected JSONStrategy(Object 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);
50 throw new IllegalArgumentException("This object has no named type.");
54 protected JSONStrategy(Object o, String namedType) {
57 this.namedType = namedType;
62 public boolean hasProperty(String name) {
67 public Object getValue(String name) {
69 result = json.get(name);
75 public void setValue(String name, Object obj) {
80 public Object getUnderlyingObject() {
85 public Set<String> getProperties() {
86 Set<String> result = json.keySet();
91 public Set<String> getRequiredProperties() {
94 return this.getProperties();
98 public Set<String> getKeys() {
100 return this.getProperties();
104 public Set<String> getAllKeys() {
106 return this.getProperties();
110 public String getType(String name) {
112 Class<?> resultClass = this.getClass(name);
113 if (resultClass != null) {
114 result = resultClass.getName();
117 if (result.equals("org.json.simple.JSONArray")) {
118 result = "java.util.List";
125 public String getGenericType(String name) {
127 Class<?> resultClass = this.getGenericTypeClass(name);
128 if (resultClass != null) {
129 result = resultClass.getName();
135 public String getJavaClassName() {
136 return json.getClass().getName();
140 public Class<?> getClass(String name) {
141 Class<?> result = null;
142 result = json.get(name).getClass();
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();
160 public Object newInstanceOfProperty(String name) {
162 return this.getClass(name).newInstance();
163 } catch (InstantiationException | IllegalAccessException e) {
164 LOGGER.error(e.getMessage(),e);
170 public Object newInstanceOfNestedProperty(String name) {
172 return this.getGenericTypeClass(name).newInstance();
173 } catch (InstantiationException | IllegalAccessException e) {
174 LOGGER.error(e.getMessage(),e);
180 public boolean isComplexType(String name) {
181 String result = this.getType(name);
183 if (result.contains("JSONObject")) {
192 public boolean isComplexGenericType(String name) {
193 String result = this.getGenericType(name);
195 if (result.contains("JSONObject")) {
204 public boolean isListType(String name) {
205 String result = this.getType(name);
207 if (result.contains("java.util.List")) {
216 public boolean isContainer() {
217 Set<String> props = this.getProperties();
218 boolean result = false;
219 if (props.size() == 1 && this.isListType(props.iterator().next())) {
226 protected String findKey() {
231 public String getName() {
232 return this.namedType;
236 public String getDbName() {
237 return this.getName();
241 public String getURI() {
243 // use a UUID for now
244 return UUID.randomUUID().toString();
248 public String getGenericURI() {
250 //there is none defined for this
255 public String preProcessKey (String key) {
257 // don't do anything with it
263 public String marshal(MarshallerProperties properties) {
269 public Object clone() {
275 public String findEdgeName(String parent, String child) {
277 // Always has for now
283 public ModelType getModelType() {
284 return ModelType.JSON;
288 public Set<String> getIndexedProperties() {
289 // TODO Auto-generated method stub
294 public String getChildName() {
295 // TODO Auto-generated method stub
300 public boolean hasChild(Introspector child) {
301 // TODO Auto-generated method stub
306 public boolean isSimpleType(String name) {
307 // TODO Auto-generated method stub
312 public boolean isSimpleGenericType(String name) {
313 // TODO Auto-generated method stub
318 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
319 // TODO Auto-generated method stub
324 public String getMetadata(ObjectMetadata metadataName) {
325 // TODO Auto-generated method stub
330 public String getChildDBName() {
331 // TODO Auto-generated method stub
336 public String getFullGenericURI() {
337 // TODO Auto-generated method stub
342 protected Object get(String name) {
343 // TODO Auto-generated method stub
348 protected void set(String name, Object value) {
349 // TODO Auto-generated method stub
354 public String getObjectId() throws UnsupportedEncodingException {
355 // TODO Auto-generated method stub
360 public Version getVersion() {
361 // TODO Auto-generated method stub