1 package org.onap.aaiclient.client.aai.entities.uri;
3 import java.lang.reflect.InvocationTargetException;
5 import java.util.Optional;
6 import java.util.stream.Collectors;
7 import org.onap.aaiclient.client.aai.AAIObjectType;
8 import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType;
9 import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
10 import com.google.common.base.CaseFormat;
12 public class AAIFluentTypeReverseLookup {
14 public AAIObjectType fromName(String name, String uri) {
16 String className = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
18 uri = uri.replaceFirst(".*?/v\\d+", "");
20 Class<? extends GraphInventoryFluentType.Info> clazz =
21 (Class<? extends GraphInventoryFluentType.Info>) Class
22 .forName("org.onap.aaiclient.client.generated.fluentbuilders." + className + "$Info");
24 GraphInventoryFluentType.Info type = clazz.getConstructor().newInstance();
26 Optional<String> parentTemplate = findParentPath(type, uri);
27 if (parentTemplate.isPresent()) {
28 return new AAIObjectType(parentTemplate.get(), type.getPartialUri(), type.getName(), false);
30 // fallback to enum lookup
31 return AAIObjectType.fromTypeName(name);
33 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
34 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
36 return AAIObjectType.UNKNOWN;
39 protected Optional<String> findParentPath(GraphInventoryFluentType.Info type, String uri) {
41 List<UriParserSpringImpl> parsers =
42 type.getPaths().stream().map(path -> new UriParserSpringImpl(path)).collect(Collectors.toList());
44 for (UriParserSpringImpl parser : parsers) {
45 if (parser.isMatch(uri)) {
46 String partialUriReplacer = type.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+");
47 return Optional.of(parser.getTemplate().replaceFirst(partialUriReplacer + "$", ""));
51 return Optional.empty();