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=========================================================
21 package org.onap.aai.introspection;
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.LinkedHashSet;
29 import java.util.List;
31 import java.util.Optional;
33 import java.util.stream.Collectors;
35 import org.apache.commons.lang3.ClassUtils;
36 import org.eclipse.persistence.exceptions.DynamicException;
37 import org.onap.aai.config.SpringContextAware;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.logging.ErrorLogHelper;
40 import org.onap.aai.logging.LogFormatTools;
41 import org.onap.aai.nodes.CaseFormatStore;
42 import org.onap.aai.nodes.NodeIngestor;
43 import org.onap.aai.restcore.MediaType;
44 import org.onap.aai.schema.enums.ObjectMetadata;
45 import org.onap.aai.schema.enums.PropertyMetadata;
46 import org.onap.aai.setup.SchemaVersion;
47 import org.onap.aai.workarounds.NamingExceptions;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.base.CaseFormat;
53 public abstract class Introspector implements Cloneable {
55 private static final Logger LOGGER = LoggerFactory.getLogger(Introspector.class);
57 protected String className;
58 protected String uriChain = "";
59 protected Loader loader;
60 protected final NamingExceptions namingException = NamingExceptions.getInstance();
61 private Set<String> uniqueProperties = null;
62 private Set<String> indexedProperties = null;
63 private Set<String> allKeys = null;
64 private Set<String> dslStartNodeProperties = null;
66 protected CaseFormatStore caseFormatStore = null;
67 protected NodeIngestor nodeIngestor;
69 protected Introspector(Object obj) {
70 this.nodeIngestor = SpringContextAware.getBean(NodeIngestor.class);
71 this.caseFormatStore = nodeIngestor.getCaseFormatStore();
74 public abstract boolean hasProperty(String name);
76 protected String convertPropertyName(String name) {
77 return caseFormatStore.fromLowerHyphenToLowerCamel(name).orElseGet(() -> {
78 LOGGER.debug("Unable to find {} in the store from lower hyphen to lower camel", name);
79 return CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name);
83 protected abstract Object get(String name);
85 protected abstract void set(String name, Object value);
89 * @param name the property name you'd like to retrieve the value for
90 * @return the value of the property
92 public <T> T getValue(String name) {
93 String convertedName = convertPropertyName(name);
96 if (this.hasProperty(name)) {
97 result = this.get(convertedName);
99 /* property not found - slightly ambiguous */
103 Class<?> clazz = this.getClass(name);
104 if (this.isListType(name) && result == null) {
106 this.set(convertedName, clazz.newInstance());
107 result = this.get(convertedName);
108 } catch (DynamicException | InstantiationException | IllegalAccessException e) {
109 LOGGER.warn(e.getMessage(), e);
116 public Introspector getWrappedValue(String name) {
117 String convertedName = convertPropertyName(name);
120 if (this.hasProperty(name)) {
121 value = this.get(convertedName);
123 /* property not found - slightly ambiguous */
127 Class<?> clazz = this.getClass(name);
128 if (this.isListType(name) && value == null) {
130 this.set(convertedName, clazz.newInstance());
131 value = this.get(convertedName);
132 } catch (DynamicException | InstantiationException | IllegalAccessException e) {
133 LOGGER.warn(e.getMessage(), e);
137 return IntrospectorFactory.newInstance(this.getModelType(), value);
145 public List<Introspector> getWrappedListValue(String name) {
146 String convertedName = convertPropertyName(name);
148 List<Introspector> resultList = new ArrayList<>();
149 if (this.hasProperty(name)) {
150 value = this.get(convertedName);
152 /* property not found - slightly ambiguous */
155 boolean isListType = this.isListType(name);
156 if (!this.isListType(name)) {
159 Class<?> clazz = this.getClass(name);
160 if (isListType && value == null) {
162 this.set(convertedName, clazz.newInstance());
163 value = this.get(convertedName);
164 } catch (DynamicException | InstantiationException | IllegalAccessException e) {
165 LOGGER.warn(e.getMessage(), e);
169 List<Object> valueList = (List<Object>) value;
171 for (Object item : valueList) {
172 resultList.add(IntrospectorFactory.newInstance(this.getModelType(), item));
179 public Object castValueAccordingToSchema(String name, Object obj) {
181 Class<?> nameClass = this.getClass(name);
182 if (nameClass == null) {
183 throw new IllegalArgumentException("property: " + name + " does not exist on " + this.getDbName());
188 if (! (obj.getClass().getCanonicalName().equals(nameClass.getCanonicalName()))) {
189 if (nameClass.isPrimitive()) {
190 nameClass = ClassUtils.primitiveToWrapper(nameClass);
191 result = nameClass.getConstructor(String.class).newInstance(obj.toString());
193 if (obj instanceof String) {
194 result = nameClass.getConstructor(String.class).newInstance(obj);
195 } else if (!this.isListType(name) && !this.isComplexType(name)) {
196 // box = obj.toString();
197 result = nameClass.getConstructor(String.class).newInstance(obj.toString());
200 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
201 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
202 ErrorLogHelper.logError("AAI_4017", e.getMessage());
208 public List<Object> castValueAccordingToSchema(String name, List<?> objs) {
209 List<Object> result = new ArrayList<>();
211 for (Object item : objs) {
212 result.add(this.castValueAccordingToSchema(name, item));
221 * @param name the property name you'd like to set the value of
222 * @param obj the value to be set
225 public void setValue(String name, Object obj) {
226 Object box = this.castValueAccordingToSchema(name, obj);
228 name = convertPropertyName(name);
234 * @return a list of all the properties available on the object
236 public abstract Set<String> getProperties();
238 public Set<String> getProperties(PropertyPredicate<Introspector, String> p) {
239 final Set<String> temp = new LinkedHashSet<>();
240 this.getProperties().stream().filter(item -> p.test(this, item)).forEach(temp::add);
241 return Collections.unmodifiableSet(temp);
244 public Set<String> getSimpleProperties(PropertyPredicate<Introspector, String> p) {
245 return this.getProperties().stream().filter(item -> p.test(this, item)).filter(this::isSimpleType)
246 .collect(Collectors.toSet());
251 * @return a list of the required properties on the object
253 public abstract Set<String> getRequiredProperties();
257 * @return a list of the properties that can be used to query the object in the db
259 public abstract Set<String> getKeys();
263 * @return a list of the all key properties for this object
265 public Set<String> getAllKeys() {
266 Set<String> result = null;
267 if (this.allKeys == null) {
268 Set<String> keys = this.getKeys();
269 result = new LinkedHashSet<>();
271 String altKeys = this.getMetadata(ObjectMetadata.ALTERNATE_KEYS_1);
272 if (altKeys != null) {
273 String[] altKeysArray = altKeys.split(",");
274 for (String altKey : altKeysArray) {
278 result = Collections.unmodifiableSet(result);
279 this.allKeys = result;
281 result = this.allKeys;
285 public Set<String> getIndexedProperties() {
286 Set<String> result = null;
288 if (this.indexedProperties == null) {
289 result = new LinkedHashSet<>();
290 Set<String> keys = this.getKeys();
292 String altKeys = this.getMetadata(ObjectMetadata.INDEXED_PROPS);
293 if (altKeys != null) {
294 String[] altKeysArray = altKeys.split(",");
295 for (String altKey : altKeysArray) {
299 this.indexedProperties = Collections.unmodifiableSet(result);
301 result = this.indexedProperties;
305 public Set<String> getDslStartNodeProperties() {
306 Set<String> result = null;
308 if (this.dslStartNodeProperties == null) {
310 * The dslStartNodeProperties will have keys by default
311 * If dslStartNodeProps exist in the oxm use it
312 * if not use the indexedProps
314 result = new LinkedHashSet<>(this.getKeys());
316 String dslKeys = this.getMetadata(ObjectMetadata.DSL_START_NODE_PROPS);
317 String indexedKeys = this.getMetadata(ObjectMetadata.INDEXED_PROPS);
318 if (dslKeys != null) {
319 Arrays.stream(dslKeys.split(",")).forEach(result::add);
321 else if(indexedKeys != null){
322 Arrays.stream(indexedKeys.split(",")).forEach(result::add);
324 this.dslStartNodeProperties = Collections.unmodifiableSet(result);
326 result = this.dslStartNodeProperties;
330 public Set<String> getUniqueProperties() {
331 Set<String> result = null;
332 if (this.uniqueProperties == null) {
333 String altKeys = this.getMetadata(ObjectMetadata.UNIQUE_PROPS);
334 result = new LinkedHashSet<>();
335 if (altKeys != null) {
336 String[] altKeysArray = altKeys.split(",");
337 for (String altKey : altKeysArray) {
341 this.uniqueProperties = Collections.unmodifiableSet(result);
344 result = this.uniqueProperties;
348 public Set<String> getDependentOn() {
349 String dependentOn = this.getMetadata(ObjectMetadata.DEPENDENT_ON);
350 if (dependentOn == null) {
351 return new LinkedHashSet<>();
353 return new LinkedHashSet<>(Arrays.asList(dependentOn.split(",")));
359 * @return the string name of the java class of the named property
361 public String getType(String name) {
362 Class<?> resultClass = this.getClass(name);
365 if (resultClass != null) {
366 result = resultClass.getName();
367 if (result.equals("java.util.ArrayList")) {
368 result = "java.util.List";
376 * This will returned the generic parameterized type of the underlying
377 * object if it exists
380 * @return the generic type of the java class of the underlying object
382 public String getGenericType(String name) {
383 Class<?> resultClass = this.getGenericTypeClass(name);
386 if (resultClass != null) {
387 result = resultClass.getName();
395 * @return the string name of the java class of the underlying object
397 public abstract String getJavaClassName();
401 * @param name the property name
402 * @return the Class object
404 public abstract Class<?> getClass(String name);
406 public abstract Class<?> getGenericTypeClass(String name);
410 * @param name the property name
411 * @return a new instance of the underlying type of this property
412 * @throws AAIUnknownObjectException
414 public Object newInstanceOfProperty(String name) throws AAIUnknownObjectException {
415 String type = this.getType(name);
416 return loader.objectFromName(type);
419 public Object newInstanceOfNestedProperty(String name) throws AAIUnknownObjectException {
420 String type = this.getGenericType(name);
421 return loader.objectFromName(type);
424 public Introspector newIntrospectorInstanceOfProperty(String name) throws AAIUnknownObjectException {
426 Introspector result = IntrospectorFactory.newInstance(this.getModelType(), this.newInstanceOfProperty(name));
432 public Introspector newIntrospectorInstanceOfNestedProperty(String name) throws AAIUnknownObjectException {
434 Introspector result =
435 IntrospectorFactory.newInstance(this.getModelType(), this.newInstanceOfNestedProperty(name));
442 * Is this type not a Java String or primitive
447 public boolean isComplexType(String name) {
448 String result = this.getType(name);
450 if (result.contains("aai") || result.equals("java.lang.Object")) {
457 public boolean isComplexGenericType(String name) {
458 String result = this.getGenericType(name);
460 if (result.contains("aai")) {
467 public boolean isSimpleType(String name) {
468 return !(this.isComplexType(name) || this.isListType(name));
471 public boolean isSimpleGenericType(String name) {
472 return !this.isComplexGenericType(name);
475 public boolean isListType(String name) {
476 String result = this.getType(name);
478 if (result.contains("java.util.List")) {
485 public boolean isContainer() {
486 Set<String> props = this.getProperties();
487 boolean result = false;
488 if (props.size() == 1 && this.isListType(props.iterator().next())) {
495 public abstract String getChildName();
497 public String getChildDBName() {
498 String result = this.getChildName();
500 result = namingException.getDBName(result);
504 public abstract String getName();
506 public String getDbName() {
507 String lowerHyphen = this.getName();
509 lowerHyphen = namingException.getDBName(lowerHyphen);
514 public abstract ModelType getModelType();
516 public boolean hasChild(Introspector child) {
517 boolean result = false;
518 // check all inheriting types for this child
519 if ("true".equals(this.getMetadata(ObjectMetadata.ABSTRACT))) {
520 String[] inheritors = this.getMetadata(ObjectMetadata.INHERITORS).split(",");
521 for (String inheritor : inheritors) {
523 Introspector temp = this.loader.introspectorFromName(inheritor);
524 result = temp.hasProperty(child.getName());
528 } catch (AAIUnknownObjectException e) {
530 "Skipping inheritor " + inheritor + " (Unknown Object) " + LogFormatTools.getStackTop(e));
534 result = this.hasProperty(child.getName());
539 public void setURIChain(String uri) {
543 public abstract String getObjectId() throws UnsupportedEncodingException;
545 public String getURI() throws UnsupportedEncodingException {
546 // String result = this.uriChain;
548 String namespace = this.getMetadata(ObjectMetadata.NAMESPACE);
549 String container = this.getMetadata(ObjectMetadata.CONTAINER);
550 if (this.isContainer()) {
551 result += "/" + this.getName();
554 if (container != null) {
555 result += "/" + container;
557 result += "/" + this.getDbName() + "/" + this.findKey();
559 if (namespace != null && !namespace.equals("")) {
560 result = "/" + namespace + result;
567 public String getGenericURI() {
569 if (this.isContainer()) {
570 result += "/" + this.getName();
572 result += "/" + this.getDbName();
573 for (String key : this.getKeys()) {
574 result += "/{" + this.getDbName() + "-" + key + "}";
581 public String getFullGenericURI() {
583 String namespace = this.getMetadata(ObjectMetadata.NAMESPACE);
584 String container = this.getMetadata(ObjectMetadata.CONTAINER);
585 if (this.isContainer()) {
586 result += "/" + this.getName();
589 if (container != null) {
590 result += "/" + container;
592 result += "/" + this.getDbName();
594 for (String key : this.getKeys()) {
595 result += "/{" + this.getDbName() + "-" + key + "}";
597 if (namespace != null && !namespace.equals("")) {
598 result = "/" + namespace + result;
606 public abstract String preProcessKey(String key);
608 protected abstract String findKey() throws UnsupportedEncodingException;
610 public abstract String marshal(MarshallerProperties properties);
612 public abstract Object getUnderlyingObject();
614 public String marshal(boolean formatted) {
615 MarshallerProperties properties =
616 new MarshallerProperties.Builder(MediaType.APPLICATION_JSON_TYPE).formatted(formatted).build();
618 return marshal(properties);
621 public String makeSingular(String word) {
623 String result = word;
624 result = result.replaceAll("(?:([ho])es|s)$", "");
626 if (result.equals("ClassesOfService")) {
627 result = "ClassOfService";
628 } else if (result.equals("CvlanTag")) {
629 result = "CvlanTagEntry";
630 } else if (result.equals("Metadata")) {
631 result = "Metadatum";
636 protected String makePlural(String word) {
637 String result = word;
639 if (result.equals("cvlan-tag-entry")) {
641 } else if (result.equals("class-of-service")) {
642 return "classes-of-service";
643 } else if (result.equals("metadatum")) {
646 result = result.replaceAll("([a-z])$", "$1s");
647 result = result.replaceAll("([hox])s$", "$1es");
649 * if (result.equals("classes-of-services")) {
650 * result = "classes-of-service";
657 public abstract String getMetadata(ObjectMetadata metadataName);
659 public abstract Map<PropertyMetadata, String> getPropertyMetadata(String propName);
661 public Optional<String> getPropertyMetadata(String propName, PropertyMetadata metadataName) {
662 final String resultValue = this.getPropertyMetadata(propName).getOrDefault(metadataName, "");
663 Optional<String> result = Optional.empty();
665 if (!resultValue.isEmpty()) {
666 result = Optional.of(resultValue);
672 public abstract SchemaVersion getVersion();
674 public Loader getLoader() {
678 public boolean isTopLevel() {
680 return this.getMetadata(ObjectMetadata.NAMESPACE) != null;