2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.aaiclient.client.aai;
23 import java.io.Serializable;
25 import java.util.HashMap;
28 import java.util.regex.Pattern;
29 import org.onap.aai.annotations.Metadata;
30 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName;
31 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup;
33 import org.reflections.Reflections;
34 import org.reflections.scanners.SubTypesScanner;
35 import org.reflections.util.ClasspathHelper;
36 import org.reflections.util.ConfigurationBuilder;
37 import com.google.common.base.CaseFormat;
40 * Types are accessed through AAIFluentTypeBuilder and should no longer be added as static members
43 public class AAIObjectType implements AAIObjectBase, AAIObjectName, GraphInventoryObjectType, Serializable {
45 private static final long serialVersionUID = -2877184776691514600L;
46 private static Map<String, AAIObjectType> map = new HashMap<>();
48 public static final AAIObjectType GENERIC_QUERY = new AAIObjectType("/search", "/generic-query", "generic-query");
49 public static final AAIObjectType BULK_PROCESS = new AAIObjectType("/bulkprocess", "", "bulkprocess");
50 public static final AAIObjectType SINGLE_TRANSACTION =
51 new AAIObjectType("/bulk/single-transaction", "", "single-transaction");
52 public static final AAIObjectType NODES_QUERY = new AAIObjectType("/search", "/nodes-query", "nodes-query");
53 public static final AAIObjectType CUSTOM_QUERY = new AAIObjectType("/query", "", "query");
54 public static final AAIObjectType UNKNOWN = new AAIObjectType("", "", "unknown") {
56 private static final long serialVersionUID = 9208984071038447607L;
59 public boolean passThrough() {
63 public static final AAIObjectType DSL = new AAIObjectType("/dsl", "", "dsl");
64 public static final AAIObjectType SUB_L_INTERFACE = new AAIObjectType(
65 "/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{interface-name}",
66 "/l-interfaces/l-interface/{sub-interface-name}", "sub-l-interface");
68 private final String uriTemplate;
69 private final String parentUri;
70 private final String partialUri;
71 private final Class<?> aaiObjectClass;
72 private final String name;
75 /* Locate any AAIObjectTypes on the classpath and add them to our map */
76 java.util.Collection<URL> packages = ClasspathHelper.forPackage("");
78 new Reflections(new ConfigurationBuilder().setUrls(packages).setScanners(new SubTypesScanner()));
80 Set<Class<? extends AAIObjectType>> resources = r.getSubTypesOf(AAIObjectType.class);
82 for (Class<? extends AAIObjectType> customTypeClass : resources) {
83 AAIObjectType customType;
85 customType = customTypeClass.newInstance();
86 } catch (InstantiationException | IllegalAccessException e) {
91 protected AAIObjectType() {
92 this.parentUri = null;
93 this.partialUri = null;
94 this.uriTemplate = null;
95 this.aaiObjectClass = null;
99 protected AAIObjectType(String parentUri, String partialUri, String name) {
100 this(parentUri, partialUri, name, true);
103 public AAIObjectType(String parentUri, String partialUri, String name, boolean register) {
104 this.parentUri = parentUri;
105 this.partialUri = partialUri;
106 this.uriTemplate = parentUri + partialUri;
107 this.aaiObjectClass = null;
109 if (register && !AAIObjectType.map.containsKey(name)) {
110 AAIObjectType.map.put(name, this);
114 protected AAIObjectType(String parentUri, Class<?> aaiObjectClass) {
115 this.parentUri = parentUri;
116 this.partialUri = removeParentUri(aaiObjectClass, parentUri);
117 this.uriTemplate = parentUri + partialUri;
118 this.aaiObjectClass = aaiObjectClass;
119 this.name = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, aaiObjectClass.getSimpleName());
120 if (!AAIObjectType.map.containsKey(name)) {
121 AAIObjectType.map.put(name, this);
126 public String toString() {
127 return this.uriTemplate();
130 public static AAIObjectType fromTypeName(String name, String uri) {
132 return new AAIFluentTypeReverseLookup().fromName(name, uri);
135 public static AAIObjectType fromTypeName(String name) {
136 if (map.containsKey(name)) {
137 return map.get(name);
139 return AAIObjectType.UNKNOWN;
144 public String typeName() {
145 return this.typeName(CaseFormat.LOWER_HYPHEN);
149 public String typeName(CaseFormat format) {
150 return CaseFormat.LOWER_HYPHEN.to(format, this.name.replace("default-", ""));
154 public String uriTemplate() {
155 return this.uriTemplate;
159 public String partialUri() {
160 return this.partialUri;
164 public int hashCode() {
165 return this.typeName().hashCode();
169 public boolean equals(Object o) {
171 if (o instanceof GraphInventoryObjectName) {
172 return this.typeName().equals(((GraphInventoryObjectName) o).typeName());
178 protected String removeParentUri(Class<?> aaiObjectClass, String parentUri) {
179 return aaiObjectClass.getAnnotation(Metadata.class).uriTemplate().replaceFirst(Pattern.quote(parentUri), "");