bd9f4c713f71fab2c7803b541952bc1721f208d1
[so.git] /
1 package org.onap.aaiclient.client.aai.entities.uri;
2
3 import java.util.List;
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;
10
11 public class AAIFluentTypeReverseLookup {
12
13     public AAIObjectType fromName(String name, String uri) {
14
15         String className = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
16
17         uri = uri.replaceFirst(".*?/v\\d+", "");
18         try {
19             Class<? extends GraphInventoryFluentType.Info> clazz =
20                     (Class<? extends GraphInventoryFluentType.Info>) Class
21                             .forName("org.onap.aaiclient.client.generated.fluentbuilders." + className + "$Info");
22
23             GraphInventoryFluentType.Info type = clazz.newInstance();
24
25             Optional<String> parentTemplate = findParentPath(type, uri);
26             if (parentTemplate.isPresent()) {
27                 return new AAIObjectType(parentTemplate.get(), type.getPartialUri(), type.getName(), false);
28             } else {
29                 // fallback to enum lookup
30                 return AAIObjectType.fromTypeName(name);
31             }
32         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
33         }
34         return AAIObjectType.UNKNOWN;
35     }
36
37     protected Optional<String> findParentPath(GraphInventoryFluentType.Info type, String uri) {
38
39         List<UriParserSpringImpl> parsers =
40                 type.getPaths().stream().map(path -> new UriParserSpringImpl(path)).collect(Collectors.toList());
41
42         for (UriParserSpringImpl parser : parsers) {
43             if (parser.isMatch(uri)) {
44                 String partialUriReplacer = type.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+");
45                 return Optional.of(parser.getTemplate().replaceFirst(partialUriReplacer + "$", ""));
46             }
47         }
48
49         return Optional.empty();
50     }
51 }