1 package org.onap.aaiclient.client.aai.entities.uri;
 
   4 import java.util.Optional;
 
   5 import java.util.stream.Collectors;
 
   6 import org.onap.aaiclient.client.aai.AAIObjectType;
 
   7 import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType;
 
   8 import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
 
   9 import com.google.common.base.CaseFormat;
 
  11 public class AAIFluentTypeReverseLookup {
 
  13     public AAIObjectType fromName(String name, String uri) {
 
  15         String className = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
 
  17         uri = uri.replaceFirst(".*?/v\\d+", "");
 
  19             Class<? extends GraphInventoryFluentType.Info> clazz =
 
  20                     (Class<? extends GraphInventoryFluentType.Info>) Class
 
  21                             .forName("org.onap.aaiclient.client.generated.fluentbuilders." + className + "$Info");
 
  23             GraphInventoryFluentType.Info type = clazz.newInstance();
 
  25             Optional<String> parentTemplate = findParentPath(type, uri);
 
  26             if (parentTemplate.isPresent()) {
 
  27                 return new AAIObjectType(parentTemplate.get(), type.getPartialUri(), type.getName(), false);
 
  29                 // fallback to enum lookup
 
  30                 return AAIObjectType.fromTypeName(name);
 
  32         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
 
  34         return AAIObjectType.UNKNOWN;
 
  37     protected Optional<String> findParentPath(GraphInventoryFluentType.Info type, String uri) {
 
  39         List<UriParserSpringImpl> parsers =
 
  40                 type.getPaths().stream().map(path -> new UriParserSpringImpl(path)).collect(Collectors.toList());
 
  42         for (UriParserSpringImpl parser : parsers) {
 
  43             if (parser.isMatch(uri)) {
 
  44                 String partialUriReplacer = type.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+");
 
  45                 return Optional.of(parser.getTemplate().replaceFirst(partialUriReplacer + "$", ""));
 
  49         return Optional.empty();