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 com.google.common.base.CaseFormat;
25 import java.io.UnsupportedEncodingException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import java.util.Optional;
35 import java.util.stream.Collectors;
37 import org.apache.commons.lang3.ClassUtils;
38 import org.eclipse.persistence.exceptions.DynamicException;
39 import org.onap.aai.config.SpringContextAware;
40 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
41 import org.onap.aai.logging.ErrorLogHelper;
42 import org.onap.aai.logging.LogFormatTools;
43 import org.onap.aai.nodes.CaseFormatStore;
44 import org.onap.aai.nodes.NodeIngestor;
45 import org.onap.aai.restcore.MediaType;
46 import org.onap.aai.schema.enums.ObjectMetadata;
47 import org.onap.aai.schema.enums.PropertyMetadata;
48 import org.onap.aai.setup.SchemaVersion;
49 import org.onap.aai.workarounds.NamingExceptions;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
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);
320 } else if (indexedKeys != null) {
321 Arrays.stream(indexedKeys.split(",")).forEach(result::add);
323 this.dslStartNodeProperties = Collections.unmodifiableSet(result);
325 result = this.dslStartNodeProperties;
329 public Set<String> getUniqueProperties() {
330 Set<String> result = null;
331 if (this.uniqueProperties == null) {
332 String altKeys = this.getMetadata(ObjectMetadata.UNIQUE_PROPS);
333 result = new LinkedHashSet<>();
334 if (altKeys != null) {
335 String[] altKeysArray = altKeys.split(",");
336 for (String altKey : altKeysArray) {
340 this.uniqueProperties = Collections.unmodifiableSet(result);
343 result = this.uniqueProperties;
347 public Set<String> getDependentOn() {
348 String dependentOn = this.getMetadata(ObjectMetadata.DEPENDENT_ON);
349 if (dependentOn == null) {
350 return new LinkedHashSet<>();
352 return new LinkedHashSet<>(Arrays.asList(dependentOn.split(",")));
358 * @return the string name of the java class of the named property
360 public String getType(String name) {
361 Class<?> resultClass = this.getClass(name);
364 if (resultClass != null) {
365 result = resultClass.getName();
366 if (result.equals("java.util.ArrayList")) {
367 result = "java.util.List";
375 * This will returned the generic parameterized type of the underlying
376 * object if it exists
379 * @return the generic type of the java class of the underlying object
381 public String getGenericType(String name) {
382 Class<?> resultClass = this.getGenericTypeClass(name);
385 if (resultClass != null) {
386 result = resultClass.getName();
394 * @return the string name of the java class of the underlying object
396 public abstract String getJavaClassName();
400 * @param name the property name
401 * @return the Class object
403 public abstract Class<?> getClass(String name);
405 public abstract Class<?> getGenericTypeClass(String name);
409 * @param name the property name
410 * @return a new instance of the underlying type of this property
411 * @throws AAIUnknownObjectException
413 public Object newInstanceOfProperty(String name) throws AAIUnknownObjectException {
414 String type = this.getType(name);
415 return loader.objectFromName(type);
418 public Object newInstanceOfNestedProperty(String name) throws AAIUnknownObjectException {
419 String type = this.getGenericType(name);
420 return loader.objectFromName(type);
423 public Introspector newIntrospectorInstanceOfProperty(String name) throws AAIUnknownObjectException {
425 Introspector result = IntrospectorFactory.newInstance(this.getModelType(), this.newInstanceOfProperty(name));
431 public Introspector newIntrospectorInstanceOfNestedProperty(String name) throws AAIUnknownObjectException {
433 Introspector result =
434 IntrospectorFactory.newInstance(this.getModelType(), this.newInstanceOfNestedProperty(name));
441 * Is this type not a Java String or primitive
446 public boolean isComplexType(String name) {
447 String result = this.getType(name);
449 if (result.contains("aai") || result.equals("java.lang.Object")) {
456 public boolean isComplexGenericType(String name) {
457 String result = this.getGenericType(name);
459 if (result.contains("aai")) {
466 public boolean isSimpleType(String name) {
467 return !(this.isComplexType(name) || this.isListType(name));
470 public boolean isSimpleGenericType(String name) {
471 return !this.isComplexGenericType(name);
474 public boolean isListType(String name) {
475 String result = this.getType(name);
477 if (result.contains("java.util.List")) {
484 public boolean isContainer() {
485 Set<String> props = this.getProperties();
486 boolean result = false;
487 if (props.size() == 1 && this.isListType(props.iterator().next())) {
494 public abstract String getChildName();
496 public String getChildDBName() {
497 String result = this.getChildName();
499 result = namingException.getDBName(result);
503 public abstract String getName();
505 public String getDbName() {
506 String lowerHyphen = this.getName();
508 lowerHyphen = namingException.getDBName(lowerHyphen);
513 public abstract ModelType getModelType();
515 public boolean hasChild(Introspector child) {
516 boolean result = false;
517 // check all inheriting types for this child
518 if ("true".equals(this.getMetadata(ObjectMetadata.ABSTRACT))) {
519 String[] inheritors = this.getMetadata(ObjectMetadata.INHERITORS).split(",");
520 for (String inheritor : inheritors) {
522 Introspector temp = this.loader.introspectorFromName(inheritor);
523 result = temp.hasProperty(child.getName());
527 } catch (AAIUnknownObjectException e) {
529 "Skipping inheritor " + inheritor + " (Unknown Object) " + LogFormatTools.getStackTop(e));
533 result = this.hasProperty(child.getName());
538 public void setURIChain(String uri) {
542 public abstract String getObjectId() throws UnsupportedEncodingException;
544 public String getURI() throws UnsupportedEncodingException {
545 // String result = this.uriChain;
547 String namespace = this.getMetadata(ObjectMetadata.NAMESPACE);
548 String container = this.getMetadata(ObjectMetadata.CONTAINER);
549 if (this.isContainer()) {
550 result += "/" + this.getName();
553 if (container != null) {
554 result += "/" + container;
556 result += "/" + this.getDbName() + "/" + this.findKey();
558 if (namespace != null && !namespace.equals("")) {
559 result = "/" + namespace + result;
566 public String getGenericURI() {
568 if (this.isContainer()) {
569 result += "/" + this.getName();
571 result += "/" + this.getDbName();
572 for (String key : this.getKeys()) {
573 result += "/{" + this.getDbName() + "-" + key + "}";
580 public String getFullGenericURI() {
582 String namespace = this.getMetadata(ObjectMetadata.NAMESPACE);
583 String container = this.getMetadata(ObjectMetadata.CONTAINER);
584 if (this.isContainer()) {
585 result += "/" + this.getName();
588 if (container != null) {
589 result += "/" + container;
591 result += "/" + this.getDbName();
593 for (String key : this.getKeys()) {
594 result += "/{" + this.getDbName() + "-" + key + "}";
596 if (namespace != null && !namespace.equals("")) {
597 result = "/" + namespace + result;
605 public abstract String preProcessKey(String key);
607 protected abstract String findKey() throws UnsupportedEncodingException;
609 public abstract String marshal(MarshallerProperties properties);
611 public abstract Object getUnderlyingObject();
613 public String marshal(boolean formatted) {
614 MarshallerProperties properties =
615 new MarshallerProperties.Builder(MediaType.APPLICATION_JSON_TYPE).formatted(formatted).build();
617 return marshal(properties);
620 public String makeSingular(String word) {
622 String result = word;
623 result = result.replaceAll("(?:([ho])es|s)$", "");
625 if (result.equals("ClassesOfService")) {
626 result = "ClassOfService";
627 } else if (result.equals("CvlanTag")) {
628 result = "CvlanTagEntry";
629 } else if (result.equals("Metadata")) {
630 result = "Metadatum";
635 protected String makePlural(String word) {
636 String result = word;
638 if (result.equals("cvlan-tag-entry")) {
640 } else if (result.equals("class-of-service")) {
641 return "classes-of-service";
642 } else if (result.equals("metadatum")) {
645 result = result.replaceAll("([a-z])$", "$1s");
646 result = result.replaceAll("([hox])s$", "$1es");
648 * if (result.equals("classes-of-services")) {
649 * result = "classes-of-service";
656 public abstract String getMetadata(ObjectMetadata metadataName);
658 public abstract Map<PropertyMetadata, String> getPropertyMetadata(String propName);
660 public Optional<String> getPropertyMetadata(String propName, PropertyMetadata metadataName) {
661 final String resultValue = this.getPropertyMetadata(propName).getOrDefault(metadataName, "");
662 Optional<String> result = Optional.empty();
664 if (!resultValue.isEmpty()) {
665 result = Optional.of(resultValue);
671 public abstract SchemaVersion getVersion();
673 public Loader getLoader() {
677 public boolean isTopLevel() {
679 return this.getMetadata(ObjectMetadata.NAMESPACE) != null;